QTableView中嵌入复选框CheckBox
       第二种方法:设置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);
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;
}

【Qt】QTableView中嵌入复选框CheckBox 的四种方法总结

(二)Qt中QTableView中加入Check列实现相关推荐

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

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

  2. (四)Paint函数实现QTableView中增加QCheckBox的方法

      第四种方法是:实现QAbstractItemDelegate的paint()函数. 这种方法和<C++_GUI_Qt4_编程(第二版)>中第十章的自定义委托例子,画星星的作法是一样的, ...

  3. qt中Qtableview的用法

    QTableView常用于实现数据的表格显示.下面我们如何按步骤实现学生信息表格: 一 添加表头 //准备数据模型     QStandardItemModel *student_model = ne ...

  4. Qt自定义委托在QTableView中绘制控件、图片、文字

    自定义委托,继承于,QStyledItemDelegate类,重载Paint()函数, 1.实现在QTableView中绘制 格式字符串 2.实现在QTableView中绘制进度条 3.实现在QTab ...

  5. QT:在QTableView中使用各种自定义委托

    原文地址::https://blog.csdn.net/lhchen922/article/details/38367719 相关文章 1.QT:在QTableView中使用各种自定义委托----ht ...

  6. QT中QTableView 点击表头进行排序

    今日,由于工作需要,要实现在QTableView中点击表头进行排序的功能,但QTableView中并未提供此功能,经过苦苦的网络搜索也为发现可用的代码.最后经过跟踪QTableWidget的排序功能实 ...

  7. 【QT Model/View】QTableView中使用委托实现表格中插入箭头

    一.应用场景 在QTableView表格中,右键插入一行数据,需要在表格上标记待插入的位置,插入完成后标记消除 二.源码实现 箭头代理继承QItemDelegate,重写paint事件,画出箭头形状 ...

  8. numpy使用[]语法索引二维numpy数组中指定指定列之后所有数据列的数值内容(accessing columns in numpy array after specifc column)

    numpy使用[]语法索引二维numpy数组中指定指定列之后所有数据列的数值内容(accessing columns in numpy array after specifc column) 目录

  9. numpy使用[]语法索引二维numpy数组中指定数据列的数值内容(accessing the specific column in numpy array)

    numpy使用[]语法索引二维numpy数组中指定数据列的数值内容(accessing the specific column in numpy array) 目录 numpy使用[]语

最新文章

  1. 架构师之路 — 数据库设计 — 关系型数据库的迁移与版本控制
  2. Shell环境变量以及set,env,export的区别
  3. 读《构建之法》第11,12章有感
  4. 约瑟夫问题(java实现)
  5. matlab中统计工具箱函数名大全
  6. 2023. 连接后等于目标字符串的字符串对
  7. 电脑k歌软件_金麦客专业k歌app下载|金麦客专业k歌软件 手机安卓版v1.1.5.0 下载...
  8. springboot使用thymeleaf完成数据的页面展示
  9. (第一章)数据库的类型
  10. GNU make manual 翻译(四十六)
  11. php 公众号 群发,php实现微信公众号无限群发
  12. Spectrum采集卡在飞行时间质谱中的应用案例
  13. 计算机英语专业摘要,推荐:计算机毕业论文英文摘要的写作方法
  14. vs2017 git 操作重置、还原、挑拣对比
  15. AtCoder Beginner Contest 158 D.String Formation
  16. ArcEngine中的ICommand和ITool
  17. 如何登录MySQL数据库
  18. 《做有质感的民族》方文山
  19. 编程初学者快速上手实战套路
  20. linux命令忽略大小写zip,Linux基础命令---unzip

热门文章

  1. Linear Algebra - Determinant(几何意义)
  2. Modbus通讯两种传输方式
  3. 浅谈Stein算法求最大公约数(GCD)的原理及简单应用
  4. 为什么站点使用https加密之后还能看到相关数据
  5. 每天CookBook之Python-048
  6. ylb:SQL 视图(View)基础
  7. c++ explicit构造函数[转]
  8. 在一个工程(包)下面让activity和SensorEventListener监听分离,在不同类中完成
  9. [转]80后偷偷“老了”的八大表现
  10. app应用需要怎么测试