(二)Qt中QTableView中加入Check列实现
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列实现相关推荐
- 【Qt】QTableView中嵌入复选框CheckBox 的四种方法总结
搜索了一下,QTableView中嵌入复选框CheckBox方法有四种: 第一种不能之前显示,必须双击/选中后才能显示,不适用. 第二种比较简单,通常用这种方法. 第三种只适合静态显示静态数据用 第四 ...
- (四)Paint函数实现QTableView中增加QCheckBox的方法
第四种方法是:实现QAbstractItemDelegate的paint()函数. 这种方法和<C++_GUI_Qt4_编程(第二版)>中第十章的自定义委托例子,画星星的作法是一样的, ...
- qt中Qtableview的用法
QTableView常用于实现数据的表格显示.下面我们如何按步骤实现学生信息表格: 一 添加表头 //准备数据模型 QStandardItemModel *student_model = ne ...
- Qt自定义委托在QTableView中绘制控件、图片、文字
自定义委托,继承于,QStyledItemDelegate类,重载Paint()函数, 1.实现在QTableView中绘制 格式字符串 2.实现在QTableView中绘制进度条 3.实现在QTab ...
- QT:在QTableView中使用各种自定义委托
原文地址::https://blog.csdn.net/lhchen922/article/details/38367719 相关文章 1.QT:在QTableView中使用各种自定义委托----ht ...
- QT中QTableView 点击表头进行排序
今日,由于工作需要,要实现在QTableView中点击表头进行排序的功能,但QTableView中并未提供此功能,经过苦苦的网络搜索也为发现可用的代码.最后经过跟踪QTableWidget的排序功能实 ...
- 【QT Model/View】QTableView中使用委托实现表格中插入箭头
一.应用场景 在QTableView表格中,右键插入一行数据,需要在表格上标记待插入的位置,插入完成后标记消除 二.源码实现 箭头代理继承QItemDelegate,重写paint事件,画出箭头形状 ...
- numpy使用[]语法索引二维numpy数组中指定指定列之后所有数据列的数值内容(accessing columns in numpy array after specifc column)
numpy使用[]语法索引二维numpy数组中指定指定列之后所有数据列的数值内容(accessing columns in numpy array after specifc column) 目录
- numpy使用[]语法索引二维numpy数组中指定数据列的数值内容(accessing the specific column in numpy array)
numpy使用[]语法索引二维numpy数组中指定数据列的数值内容(accessing the specific column in numpy array) 目录 numpy使用[]语
最新文章
- 架构师之路 — 数据库设计 — 关系型数据库的迁移与版本控制
- Shell环境变量以及set,env,export的区别
- 读《构建之法》第11,12章有感
- 约瑟夫问题(java实现)
- matlab中统计工具箱函数名大全
- 2023. 连接后等于目标字符串的字符串对
- 电脑k歌软件_金麦客专业k歌app下载|金麦客专业k歌软件 手机安卓版v1.1.5.0 下载...
- springboot使用thymeleaf完成数据的页面展示
- (第一章)数据库的类型
- GNU make manual 翻译(四十六)
- php 公众号 群发,php实现微信公众号无限群发
- Spectrum采集卡在飞行时间质谱中的应用案例
- 计算机英语专业摘要,推荐:计算机毕业论文英文摘要的写作方法
- vs2017 git 操作重置、还原、挑拣对比
- AtCoder Beginner Contest 158 D.String Formation
- ArcEngine中的ICommand和ITool
- 如何登录MySQL数据库
- 《做有质感的民族》方文山
- 编程初学者快速上手实战套路
- linux命令忽略大小写zip,Linux基础命令---unzip
热门文章
- Linear Algebra - Determinant(几何意义)
- Modbus通讯两种传输方式
- 浅谈Stein算法求最大公约数(GCD)的原理及简单应用
- 为什么站点使用https加密之后还能看到相关数据
- 每天CookBook之Python-048
- ylb:SQL 视图(View)基础
- c++ explicit构造函数[转]
- 在一个工程(包)下面让activity和SensorEventListener监听分离,在不同类中完成
- [转]80后偷偷“老了”的八大表现
- app应用需要怎么测试