QTableView中嵌入复选框CheckBox
来源:互联网 发布:软件咨询服务 编辑:程序博客网 时间:2024/05/22 06:44
设置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. 9. QAbstractTableModel(parent) {rowCheckStateMap.clear();}; 10. public: 11. int rowCount(const QModelIndex &parent = QModelIndex()) const; 12. int columnCount(const QModelIndex &parent = QModelIndex()) const; 13. QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; 14. QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; 15. Qt::ItemFlags flags(const QModelIndex &index) const; 16. bool setData(const QModelIndex &index, const QVariant &value, int role); 17. 18. 19. public: 20. void AddStudentInfo(const StudentInfo &studentInfo); 21. 22. 23. signals: 24. void StudentInfoIsChecked(const StudentInfo &studentInfo); 25. 26. 27. private: 28. typedef QVector<StudentInfo> StudentInfos; 29. StudentInfos studentInfos; 30. int totalColumn; 31. int colNumberWithCheckBox; 32. QMap<int, Qt::CheckState> rowCheckStateMap; 33. };
1. 3.在StudentInfoModel.cpp文件中的主要代码如下: 2. QVariant StudentInfoModel::data( const QModelIndex &index, int role ) const 3. { 4. if (role == Qt::DisplayRole) 5. { 6. if (index.column() == 0) 7. return QString::number(index.row()+1); 8. if (index.column() == 1) 9. return studentInfos[index.row()].stuNumber; 10. if (index.column() == 2) 11. return studentInfos[index.row()].stuName; 12. if (index.column() == 3) 13. return studentInfos[index.row()].stuID; 14. if (index.column() == 4) 15. return studentInfos[index.row()].stuPhoneNumber; 16. if (index.column() == 5) 17. return studentInfos[index.row()].department; 18. if (index.column() == 6) 19. return studentInfos[index.row()].stuDescription; 20. } 21. if (role == Qt::CheckStateRole) 22. { 23. if (index.column() == colNumberWithCheckBox) 24. { 25. if (rowCheckStateMap.contains(index.row())) 26. return rowCheckStateMap[index.row()] == Qt::Checked ? Qt::Checked : Qt::Unchecked; return Qt::Unchecked; 27. } 28. } 29. return QVariant(); 30. } 31. 32. 33. Qt::ItemFlags StudentInfoModel::flags( const QModelIndex &index ) const 34. { 35. if 36. (!index.isValid()) 37. return 0; 38. 39. 40. if (index.column() == colNumberWithCheckBox) 41. return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable; 42. 43. 44. return Qt::ItemIsEnabled | Qt::ItemIsSelectable; 45. } 46. 47. 48. bool StudentInfoModel::setData( const QModelIndex &index, const QVariant &value, int role ) 49. { 50. if(!index.isValid()) 51. return false; 52. if (role == Qt::CheckStateRole && index.column() == colNumberWithCheckBox) 53. { 54. if (value == Qt::Checked) // 55. { 56. rowCheckStateMap[index.row()] = Qt::Checked; 57. if(studentInfos.size() > index.row()) 58. emit StudentInfoIsChecked(studentInfos[index.row()]); 59. } 60. else 61. { 62. rowCheckStateMap[index.row()] = Qt::Unchecked; 63. } 64. } 65. return true; 66. }
【Qt】QTableView中嵌入复选框CheckBox 的四种方法总结
阅读全文
0 0
- QTableView中嵌入复选框CheckBox
- QTableView中嵌入复选框CheckBox
- 【Qt】QTableView中嵌入复选框CheckBox 的四种方法总结
- QTableView中嵌入复选框CheckBox方法:实现QAbstractItemDelegate的paint()函数。
- QTableView中VerticalHeader嵌入checkbox
- QTableView中嵌入复选框CheckBox 的四种方法总结:设置QAbstractTableModel的flags()函数法。
- QTableView中嵌入CheckBox的四种方法
- QTableView添加复选框
- MFC中复选框 checkbox
- 使用qstandarditemmodel获取qtableview中复选框的数据
- Qt 中 QComboBox 嵌入复选框
- gridcontrol中添加checkbox复选框
- gridcontrol中添加checkbox复选框
- Android中CheckBox复选框操作
- gridcontrol中添加checkbox复选框
- gridcontrol中添加checkbox复选框
- gridcontrol中添加checkbox复选框
- GridControl中添加checkbox复选框
- libsvm学习记录(一)理论推导
- JSON数据转成html表格
- 在 JS 中使用 fetch 更加高效地进行网络请求
- 物理使用细节问题
- jQuery插件模拟支付宝密码输入框效果
- QTableView中嵌入复选框CheckBox
- 控制设计的步骤和设计的视角
- error creating fake context.xml报错解决办法
- 卷积神经网络直观理解
- 手机直播全套源码开发, 直播程序源码经验干货分享!
- 创建安全登录凭据和启动防火墙
- 覆盖式发布,非覆盖式发布
- get请求传参的最大长度
- 1.笔记本win7 centOS双系统实录