qt model 中的角色 role Qt::CheckStateRole角色

Qt模型中的每个数据元素包含modelIndex和多种角色(role)。常用角色如Qt::DisplayRole和Qt::EditRole分别用于显示和编辑。Qt::CheckStateRole特殊在于,当设置某列具有此角色后,列会显示为复选框。在TradeTableModel的setData()函数中,通过检查Qt::CheckStateRole和column(),可以控制行的复选框状态,实现用户交互更新。

model中的每一个数据元素都有一个modelIndex和一套角色role。

model中的角色,role有多个,但是常用的就几个。 这些角色可以保存任意值,角色中可用保持QVariant类型的值。


最常用的是Qt::DisplayRole和Qt::EditRole角色。

这两个角色是用于显示和编辑的角色。


还有几个常用的角色是用来控制 基本显示属性的。

比如:

Qt:: FontRole,控制显示的字体。

Qt::TextAlignmentRole,控制元素 显示文字的对齐方式的

Qt::TextColorRole,控制显示文字颜色的。

Qt::BackgroundColorRole,控制背景颜色的。



Qt::CheckStateRole也是qt中的角色, 当flag()函数中,设置某一列具有Qt::CheckStateRole这种角色后,该列就会显示为一个CheckBox,


        //允许用户编辑item,必须重写setData()函数
        bool TradeTableModel::setData ( const QModelIndex & index, const QVariant & value, introle/* = Qt::EditRole */)
     &nbs

QVariant attendanceRecordModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); if (index.column() == 7) { if (role == Qt::EditRole || role == Qt::DecorationRole || role == Qt::DisplayRole) { QByteArray imageData = QSqlTableModel::data(index, Qt::EditRole).toByteArray(); if (role == Qt::EditRole || role == Qt::DisplayRole) { return imageData; } if (!imageData.isEmpty()) { QPixmap pixmap; if (pixmap.loadFromData(imageData)) { return pixmap.scaled(130, 110, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation); } } } return QVariant(); } //编辑和显示模式确保模型可以拿到数据 if (role == Qt::DisplayRole || role == Qt::EditRole) { return QSqlTableModel::data(index, role); } return QVariant(); } QVariant attendanceRecordModel::headerData(int section, Qt::Orientation orientation, int role) const { if (role == Qt::CheckStateRole && orientation == Qt::Horizontal && section == 8) { if (selectedIds.size() == rowCount()) return Qt::Checked; else if (selectedIds.isEmpty()) return Qt::Unchecked; else return Qt::PartiallyChecked; } if (role == Qt::DisplayRole && orientation == Qt::Horizontal) { switch (section) { case 0: return "序号"; case 1: return "工号"; case 2: return "姓名"; case 3: return "设备编号"; case 4: return "部门"; case 5: return "考勤状态"; case 6: return "打卡时间"; case 7: return "照片"; default: return QVariant(); } } return QVariant(); }在这个代码的基础上修改,让第九列显示复选框,同时表头也要有复选框,显示全选+复选框
07-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值