搜索了一下,QTableView中嵌入复选框CheckBox方法有四种

第一种不能之前显示,必须双击/选中后才能显示,不适用。

第二种比较简单,通常用这种方法。

第三种只适合静态显示静态数据用

第四种比较适合扩展,它除了可以嵌入复选框,还可以通过paint()绘制其它控件,图片等自定义风格。

 

第一种方法是:编辑委托法

这种方法直接利用委托中重载createEditor(),激活QCheckBox,这个缺点是必须双击/选中,才能显示CheckBox控件。一般不满足我们实际中的直接显示的需要。可以参考Qt中的QSpinBoxDelegate例子。

第二种方法是:设置QAbstractTableModel的flags()函数法。

通过Delegate创建QCheckBox来实现的Check列,只有在该列进入编辑模式时才能够Check/Uncheck。这显然不是我们想要的,网上翻来翻去,在一个国外论坛中看到了无需Delegate的实现方法,只需重写Model即可:
 
主要是修改两个函数:
//设置某一列为可选角色,绘画出QCheckBox
Qt::ItemFlags flags(const QModelIndex &index) const; 
//根据界面选择QCheckbox,修改Model中的数据
 bool setData(const QModelIndex &index, const QVariant &value, int role);

  1. 2.在StudentInfoModel .h头文件中的主要代码:
  2. class StudentInfoModel : public QAbstractTableModel
  3. {
  4. Q_OBJECT
  5. public:
  6. StudentInfoModel(const int totalColumn, const int aColumnNumWithChechBox = 0, QObject *parent = 0)
  7. :totalColumn(totalColumn),colNumberWithCheckBox(aColumnNumWithChechBox),
  8. QAbstractTableModel(parent) {rowCheckStateMap.clear();};
  9. public:
  10. int rowCount(const QModelIndex &parent = QModelIndex()) const;
  11. int columnCount(const QModelIndex &parent = QModelIndex()) const;
  12. QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
  13. QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
  14. Qt::ItemFlags flags(const QModelIndex &index) const;
  15. bool setData(const QModelIndex &index, const QVariant &value, int role);
  16. public:
  17. void AddStudentInfo(const StudentInfo &studentInfo);
  18. signals:
  19. void StudentInfoIsChecked(const StudentInfo &studentInfo);
  20. private:
  21. typedef QVector<StudentInfo> StudentInfos;
  22. StudentInfos studentInfos;
  23. int totalColumn;
  24. int colNumberWithCheckBox;
  25. QMap<int, Qt::CheckState> rowCheckStateMap;
  26. };
  27. 3.在StudentInfoModel.cpp文件中的主要代码如下:
  28. QVariant StudentInfoModel::data( const QModelIndex &index, int role ) const
  29. {
  30. if (role == Qt::DisplayRole)
  31. {
  32. if (index.column() == 0)
  33. return QString::number(index.row()+1);
  34. if (index.column() == 1)
  35. return studentInfos[index.row()].stuNumber;
  36. if (index.column() == 2)
  37. return studentInfos[index.row()].stuName;
  38. if (index.column() == 3)
  39. return studentInfos[index.row()].stuID;
  40. if (index.column() == 4)
  41. return studentInfos[index.row()].stuPhoneNumber;
  42. if (index.column() == 5)
  43. return studentInfos[index.row()].department;
  44. if (index.column() == 6)
  45. return studentInfos[index.row()].stuDescription;
  46. }
  47. if (role == Qt::CheckStateRole)
  48. {
  49. if (index.column() == colNumberWithCheckBox)
  50. {
  51. if (rowCheckStateMap.contains(index.row()))
  52. return rowCheckStateMap[index.row()] == Qt::Checked ? Qt::Checked : Qt::Unchecked; return Qt::Unchecked;
  53. }
  54. }
  55. return QVariant();
  56. }
  57. Qt::ItemFlags StudentInfoModel::flags( const QModelIndex &index ) const
  58. {
  59. if
  60. (!index.isValid())
  61. return 0;
  62. if (index.column() == colNumberWithCheckBox)
  63. return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable;
  64. return  Qt::ItemIsEnabled | Qt::ItemIsSelectable;
  65. }
  66. bool StudentInfoModel::setData( const QModelIndex &index, const QVariant &value, int role )
  67. {
  68. if(!index.isValid())
  69. return false;
  70. if (role == Qt::CheckStateRole && index.column() == colNumberWithCheckBox)
  71. {
  72. if (value == Qt::Checked) //
  73. {
  74. rowCheckStateMap[index.row()] = Qt::Checked;
  75. if(studentInfos.size() > index.row())
  76. emit StudentInfoIsChecked(studentInfos[index.row()]);
  77. }
  78. else
  79. {
  80. rowCheckStateMap[index.row()] = Qt::Unchecked;
  81. }
  82. }
  83. return true;
  84. }

第三种方法是:用QTableView中的方法void setIndexWidget(const QModelIndex &index, QWidget *widget)来设置QCheckBox。

代码:setIndexWidget(index, new QTextEdit);

 Qt Assistant 写道
The items shown in a table view, like those in the other item views, are rendered and edited using standard delegates. However, for some tasks it is sometimes useful to be able to insert widgets in a table instead. Widgets are set for particular indexes with the setIndexWidget() function, and later retrieved with indexWidget().

Qt Assistant 写道关于setIndexWidget()

Sets the given widget on the item at the given index, passing the ownership of the widget to the viewport.
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四种方法的更多相关文章

  1. QTableView中嵌入复选框CheckBox 的四种方法总结

    搜索了一下,QTableView中嵌入复选框CheckBox方法有四种: 第一种不能之前显示,必须双击/选中后才能显示,不适用. 第二种比较简单,通常用这种方法. 第三种只适合静态显示静态数据用 第四 ...

  2. qt QTableView中嵌入复选框CheckBox 的四种方法总结

    第一种不能之前显示,必须双击/选中后才能显示,不适用. 第二种比较简单,通常用这种方法. 第三种只适合静态显示静态数据用 第四种比较适合扩展,它除了可以嵌入复选框,还可以通过paint()绘制其它控件 ...

  3. PyQt(Python+Qt)学习随笔:复选框checkBox的tristate属性

    在Qt Designer中,tristate属性是复选框checkBox相比较于QAbstractButton多出来的唯一属性. tristate属性表示复选框是三种状态还是两种状态,如果trista ...

  4. [原创]纯JS实现网页中多选复选框checkbox和单选radio的美化效果

    图片素材: 最终效果图: <html><title> 纯JS实现网页中多选复选框checkbox和单选radio的美化效果</title><head>& ...

  5. php 判断复选框checkbox是否被选中

    php 判断复选框checkbox是否被选中   复选框checkbox在php表单提交中经常被使用到,本文章通过实例向大家介绍php如何判断复选框checkbox中的值是否被选中,需要的朋友可以参考 ...

  6. 3.Android之单选按钮RadioGroup和复选框Checkbox学习

    单选按钮和复选框在实际中经常看到,今天就简单梳理下. 首先,我们在工具中拖进单选按钮RadioGroup和复选框Checkbox,如图: xml对应的源码: <?xml version=&quo ...

  7. 在word中做复选框打对勾钩

    在word中做复选框打对勾钩 现在终于搞明白正确的操作方法 一.你在word里输入2610,按alt+X就能出 空checkbox 你在word里输入2611,按alt+X就能出 打了勾的checkb ...

  8. nodetree中 前面复选框禁用插件

    nodetree中 前面复选框的去掉插件 extendTreeCheck.js /** * tree方法扩展 * 作者:小雪转中雪 */ $.extend($.fn.tree.methods, { / ...

  9. 使用CSS3美化复选框checkbox

    我们知道HTML默认的复选框样式十分简陋,而以图片代替复选框的美化方式会给页面表单的处理带来麻烦,那么本文将结合实例带您一起了解一下使用CSS3将复选框checkbox进行样式美化,并且带上超酷的滑动 ...

随机推荐

  1. [No0000168]Excle只允许用户输入纯文本,禁止用户修改单元格样式、格式等

    背景:自己的模板给别人,让他填完信息上传到系统里,但别人经常不按模板的格式来填写,导致无法程序自动化.能不能在模板上把格式锁住,只允许输入纯文本,但不能改格式? 方法: 步骤一,创建你要的模板 其中, ...

  2. 多线程 ForkJoinPool

    阅读目录 使用 背景:ForkJoinPool的优势在于,可以充分利用多cpu,多核cpu的优势,把一个任务拆分成多个“小任务”,把多个“小任务”放到多个处理器核心上并行执行:当多个“小任务”执行完成 ...

  3. angular 表单元素的验证清除问题

    项目中利用了前些时候写的弹出dialog的方式,验证方式用了控件angular-validation(http://www.cnblogs.com/FineDay/p/7255689.html) 验证 ...

  4. DbGridEh 一个单元格的值改变时另一单元格的值随之改变

    你可以为每个字段设置OnSetText事件,这样在输入完后回车会移动时就会触发,或者在adoquery的beforepost中或afterpost中都可以grid也提供了一些事件,也可以在某些条件下做 ...

  5. PopupMenu动态创建菜单

    1.TPopupMenu一条横线在Caption输入一个'-'就可以了.2.在Caption输入名字之后加入一个&就可以不显示快捷键,比如: 退出&  这样退出按钮的快捷键就不会显示出 ...

  6. 20165225 2017-2018-2《Java程序设计》课程总结

    20165225 2017-2018-2<Java程序设计>课程总结 - 每周作业链接汇总: 预备作业一:我期待的师生关系 预备作业二:学习基础和C语言基础调查 预备作业三:linux安装 ...

  7. 四、Spring Boot Web开发

    四.Web开发 1.简介 使用SpringBoot: 1).创建SpringBoot应用,选中我们需要的模块: 2).SpringBoot已经默认将这些场景配置好了,只需要在配置文件中指定少量配置就可 ...

  8. SQL SERVER 基于数据库镜像的主从同步(数据库镜像实践汇总)

    SQL SERVER 基于数据库镜像的主从同步 Author:chaoqun.guo    createtime:2019-03-26 目录 SQL SERVER 基于数据库镜像的主从同步... 1 ...

  9. PHP 注册错误和异常处理机制

    注册错误和异常处理机制有三个PHP函数需要学习 1. register_shutdown_function('Bootstrap\Library\Frame::fatalError'); 2. set ...

  10. 循环匹配出图片地址(即src属性)

    <script type="text/javascript"> //思路分两步:作者(yanue). //1,匹配出图片img标签(即匹配出所有图片),过滤其他不需要的 ...