python QQTableView中嵌入复选框CheckBox四种方法
搜索了一下,QTableView中嵌入复选框CheckBox方法有四种:
第一种不能之前显示,必须双击/选中后才能显示,不适用。
第二种比较简单,通常用这种方法。
第三种只适合静态显示静态数据用
第四种比较适合扩展,它除了可以嵌入复选框,还可以通过paint()绘制其它控件,图片等自定义风格。
第一种方法是:编辑委托法
这种方法直接利用委托中重载createEditor(),激活QCheckBox,这个缺点是必须双击/选中,才能显示CheckBox控件。一般不满足我们实际中的直接显示的需要。可以参考Qt中的QSpinBoxDelegate例子。
第二种方法是:设置QAbstractTableModel的flags()函数法。
- 2.在StudentInfoModel .h头文件中的主要代码:
- class StudentInfoModel : public QAbstractTableModel
- {
- Q_OBJECT
- public:
- StudentInfoModel(const int totalColumn, const int aColumnNumWithChechBox = 0, QObject *parent = 0)
- :totalColumn(totalColumn),colNumberWithCheckBox(aColumnNumWithChechBox),
- QAbstractTableModel(parent) {rowCheckStateMap.clear();};
- public:
- int rowCount(const QModelIndex &parent = QModelIndex()) const;
- int columnCount(const QModelIndex &parent = QModelIndex()) const;
- QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
- QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
- Qt::ItemFlags flags(const QModelIndex &index) const;
- bool setData(const QModelIndex &index, const QVariant &value, int role);
- public:
- void AddStudentInfo(const StudentInfo &studentInfo);
- signals:
- void StudentInfoIsChecked(const StudentInfo &studentInfo);
- private:
- typedef QVector<StudentInfo> StudentInfos;
- StudentInfos studentInfos;
- int totalColumn;
- int colNumberWithCheckBox;
- QMap<int, Qt::CheckState> rowCheckStateMap;
- };
- 3.在StudentInfoModel.cpp文件中的主要代码如下:
- QVariant StudentInfoModel::data( const QModelIndex &index, int role ) const
- {
- if (role == Qt::DisplayRole)
- {
- if (index.column() == 0)
- return QString::number(index.row()+1);
- if (index.column() == 1)
- return studentInfos[index.row()].stuNumber;
- if (index.column() == 2)
- return studentInfos[index.row()].stuName;
- if (index.column() == 3)
- return studentInfos[index.row()].stuID;
- if (index.column() == 4)
- return studentInfos[index.row()].stuPhoneNumber;
- if (index.column() == 5)
- return studentInfos[index.row()].department;
- if (index.column() == 6)
- return studentInfos[index.row()].stuDescription;
- }
- if (role == Qt::CheckStateRole)
- {
- if (index.column() == colNumberWithCheckBox)
- {
- if (rowCheckStateMap.contains(index.row()))
- return rowCheckStateMap[index.row()] == Qt::Checked ? Qt::Checked : Qt::Unchecked; return Qt::Unchecked;
- }
- }
- return QVariant();
- }
- Qt::ItemFlags StudentInfoModel::flags( const QModelIndex &index ) const
- {
- if
- (!index.isValid())
- return 0;
- if (index.column() == colNumberWithCheckBox)
- return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable;
- return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
- }
- bool StudentInfoModel::setData( const QModelIndex &index, const QVariant &value, int role )
- {
- if(!index.isValid())
- return false;
- if (role == Qt::CheckStateRole && index.column() == colNumberWithCheckBox)
- {
- if (value == Qt::Checked) //
- {
- rowCheckStateMap[index.row()] = Qt::Checked;
- if(studentInfos.size() > index.row())
- emit StudentInfoIsChecked(studentInfos[index.row()]);
- }
- else
- {
- rowCheckStateMap[index.row()] = Qt::Unchecked;
- }
- }
- return true;
- }

第三种方法是:用QTableView中的方法void setIndexWidget(const QModelIndex &index, QWidget *widget)来设置QCheckBox。
代码:setIndexWidget(index, new QTextEdit);
Qt Assistant 写道关于setIndexWidget()
If index is invalid (e.g., if you pass the root index), this function will do nothing.
The given widget's autoFillBackground property must be set to true, otherwise the widget's background will be transparent, showing both the model data and the item at the given index.
If index widget A is replaced with index widget B, index widget A will be deleted. For example, in the code snippet below, the QLineEdit object will be deleted.
setIndexWidget(index, new QLineEdit);
...
setIndexWidget(index, new QTextEdit);
This function should only be used to display static content within the visible area corresponding to an item of data. If you want to display custom dynamic content or implement a custom editor widget, subclass QItemDelegate instead.
此功能只应该用来显示可视区域内对应一个数据项的静态内容。如果你想显示自定义的动态内容或执行自定义编辑器部件,子类化QItemDelegate代替。
就是说这个只适合用来做不变数据的显示,而不适合做一些会产生插入,更新,删除这样的操作的数据显示.
第四种方法是:实现QAbstractItemDelegate的paint()函数。
python QQTableView中嵌入复选框CheckBox四种方法的更多相关文章
- QTableView中嵌入复选框CheckBox 的四种方法总结
搜索了一下,QTableView中嵌入复选框CheckBox方法有四种: 第一种不能之前显示,必须双击/选中后才能显示,不适用. 第二种比较简单,通常用这种方法. 第三种只适合静态显示静态数据用 第四 ...
- qt QTableView中嵌入复选框CheckBox 的四种方法总结
第一种不能之前显示,必须双击/选中后才能显示,不适用. 第二种比较简单,通常用这种方法. 第三种只适合静态显示静态数据用 第四种比较适合扩展,它除了可以嵌入复选框,还可以通过paint()绘制其它控件 ...
- PyQt(Python+Qt)学习随笔:复选框checkBox的tristate属性
在Qt Designer中,tristate属性是复选框checkBox相比较于QAbstractButton多出来的唯一属性. tristate属性表示复选框是三种状态还是两种状态,如果trista ...
- [原创]纯JS实现网页中多选复选框checkbox和单选radio的美化效果
图片素材: 最终效果图: <html><title> 纯JS实现网页中多选复选框checkbox和单选radio的美化效果</title><head>& ...
- php 判断复选框checkbox是否被选中
php 判断复选框checkbox是否被选中 复选框checkbox在php表单提交中经常被使用到,本文章通过实例向大家介绍php如何判断复选框checkbox中的值是否被选中,需要的朋友可以参考 ...
- 3.Android之单选按钮RadioGroup和复选框Checkbox学习
单选按钮和复选框在实际中经常看到,今天就简单梳理下. 首先,我们在工具中拖进单选按钮RadioGroup和复选框Checkbox,如图: xml对应的源码: <?xml version=&quo ...
- 在word中做复选框打对勾钩
在word中做复选框打对勾钩 现在终于搞明白正确的操作方法 一.你在word里输入2610,按alt+X就能出 空checkbox 你在word里输入2611,按alt+X就能出 打了勾的checkb ...
- nodetree中 前面复选框禁用插件
nodetree中 前面复选框的去掉插件 extendTreeCheck.js /** * tree方法扩展 * 作者:小雪转中雪 */ $.extend($.fn.tree.methods, { / ...
- 使用CSS3美化复选框checkbox
我们知道HTML默认的复选框样式十分简陋,而以图片代替复选框的美化方式会给页面表单的处理带来麻烦,那么本文将结合实例带您一起了解一下使用CSS3将复选框checkbox进行样式美化,并且带上超酷的滑动 ...
随机推荐
- css学习_css三大特性
css三大特性 1.层叠性(就近原则) 2.继承性(和文字有关的会继承) 3.优先级 (权重问题) 权重:0,0,0,0 0001 ---标签选择器(注意:即使有20个标签选择器也不会比一个伪类选 ...
- React子组件怎么改变父组件的state
React子组件怎么改变父组件的state 1.父组件 class Father extends React.Component { construtor(props){ super(props); ...
- Exception 02 : java.lang.ClassNotFoundException: Could not load requested class : com.mysql.jdbc.Driver
异常名称 java.lang.ClassNotFoundException: Could not load requested class : com.mysql.jdbc.Driver 异常详细信息 ...
- Chap3:区块链的衍生技术[《区块链中文词典》维京&甲子]
- 最全的MonkeyRunner自动化测试从入门到精通(2)
一.Python环境变量的配置 步骤一:在官网进行下载python安装包,官网下载的路径:https://www.python.org/,如图所示: 步骤二:下载完成后,双击安装包,进行如下安装的界面 ...
- 为单实例数据库配置ASM
环境配置沿用搭建RAC的环境配置 配置ASM可以在数据库软件安装之前进行,也可以在安装完数据库软件配置数据库前进行 [root@rac01 Packages]# cd /etc/yum.repos.d ...
- Appium入门(5)__ Appium测试用例(1)
步骤为:启动AVD.启动Appium.写用例(python).执行 一.启动Android模拟器 二.启动Appium Server 双击appium图标启动,配置 ...
- Ubuntu 下Anaconda3出现 conda:command not found(未找到命令)
问题:anaconda: command not found解决方案:打开Terminal 1.使用命令:sudo apt install vim 安装vim文本编辑器2.使用命令:vim ~/.ba ...
- 洛谷P3295 萌萌哒 [SCOI2016] 倍增+并查集
正解:倍增+并查集 解题报告: 传送门! 首先不难想到暴力?就考虑把区间相等转化成对应点对相等,然后直接对应点连边,最后求有几个连通块就好辣 然后看下复杂度,修改是O(n2)查询是O(n),就比较容易 ...
- 洛谷P1315 观光公交 [noip2011D2T3] 贪心
正解:贪心 解题报告: 这里是链接! 唔我觉得还是很容易想到是贪心的,这个难就难在怎么贪心 下面列一下常见的几个贪心思想: 1)根据车上的人数排序,人最多的那条路用加速器 错误,人数多并不意味着加速的 ...