QT6 源(148)模型视图架构里的表头类型 QHeaderView 篇一:先学习其属性,

(1)本表格表头类型 QHeaderView 的类的继承关系如下 ,可见,本质上表头类型属于视图,Qt 把表格的表头也作为了视图来处理:

在这里插入图片描述

(2) 表格表头的属性引出

在这里插入图片描述

(3)接着开始本类的属性学习

在这里插入图片描述

(4)

在这里插入图片描述

(5)

在这里插入图片描述

++

在这里插入图片描述

(6)

在这里插入图片描述

++

在这里插入图片描述

(7)

在这里插入图片描述

++可视化演示

在这里插入图片描述

(8)

在这里插入图片描述

++ 举例实验如下

在这里插入图片描述

(9)

在这里插入图片描述

(10)接着学习其成员函数部分。

谢谢

Qt框架的模型-视图架构中,实现表头显示复选框的功能主要涉及对`QHeaderView`和数据模型的定制化处理。通常,表头的内容由模型的`headerData()`方法提供,而复选框的交互行为则需要通过设置特定的委托(delegate)或自定义视图组件来实现。 ### 实现步骤 1. **在模型中提供表头数据和复选框状态** 如果使用的是自定义模型(如继承自`QAbstractTableModel`),可以通过重写`headerData()`方法,为表头提供文本或角色数据。为了显示复选框,需要定义个自定义角色(如`Qt::CheckStateRole`),并在`headerData()`中根据角色返回相应的状态(`Qt::Checked`或`Qt::Unchecked`)。 2. **设置表头的编辑委托** `QHeaderView`本身不支持直接设置委托,但可以通过自定义`QTableView`的水平表头(horizontal header)并重写其`editorEvent()`方法,以响应鼠标点击事件。也可以通过继承`QStyledItemDelegate`并将其设置为表头的委托来实现交互逻辑。 3. **更新复选框状态** 当用户点击表头的复选框时,应更新模型中的状态,并通过`dataChanged()`信号通知视图刷新。 ### 示例代码 以下是个简化版本的模型实现,用于在表头显示复选框: ```cpp class CustomTableModel : public QAbstractTableModel { Q_OBJECT public: explicit CustomTableModel(QObject *parent = nullptr) : QAbstractTableModel(parent), m_headerChecked(Qt::Unchecked) {} int rowCount(const QModelIndex &parent = QModelIndex()) const override { return 5; // 示例行数 } int columnCount(const QModelIndex &parent = QModelIndex()) const override { return 3; // 示例列数 } QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override { if (role == Qt::DisplayRole) { return QString("Row %1, Col %2").arg(index.row()).arg(index.column()); } return QVariant(); } QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override { if (orientation == Qt::Horizontal && role == Qt::DisplayRole && section == 0) { return "Header"; // 表头标题 } if (orientation == Qt::Horizontal && role == Qt::CheckStateRole && section == 0) { return static_cast<int>(m_headerChecked); // 返回复选框状态 } return QAbstractTableModel::headerData(section, orientation, role); } bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole) override { if (orientation == Qt::Horizontal && role == Qt::CheckStateRole && section == 0) { m_headerChecked = static_cast<Qt::CheckState>(value.toInt()); emit headerDataChanged(orientation, section, section); return true; } return false; } Qt::ItemFlags flags(const QModelIndex &index) const override { return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable; } private: Qt::CheckState m_headerChecked; }; ``` 在视图部分,可以创建个继承自`QTableView`的类,并重写其`horizontalHeader()`方法以支持复选框的点击事件。此外,也可以通过安装事件过滤器或设置自定义委托来实现更复杂的交互逻辑。 ### 自定义委托示例 ```cpp class CheckBoxHeaderDelegate : public QStyledItemDelegate { Q_OBJECT public: using QStyledItemDelegate::QStyledItemDelegate; void paint(QPainter *painter, const QStyleOptionButton &option, const QModelIndex &index) const { QStyleOptionButton opt = option; opt.state |= QStyle::State_Enabled; opt.text = ""; opt.state = (index.data(Qt::CheckStateRole).toInt() == Qt::Checked) ? QStyle::State_On : QStyle::State_Off; QApplication::style()->drawControl(QStyle::CE_CheckBox, &opt, painter); } bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionButton &option, const QModelIndex &index) { if (event->type() == QEvent::MouseButtonRelease) { Qt::CheckState state = (model->headerData(index.column(), Qt::Horizontal, Qt::CheckStateRole).toInt() == Qt::Checked) ? Qt::Unchecked : Qt::Checked; model->setHeaderData(index.column(), Qt::Horizontal, state, Qt::CheckStateRole); return true; } return false; } }; ``` ### 设置委托 ```cpp QTableView *tableView = new QTableView; CustomTableModel *model = new CustomTableModel; tableView->setModel(model); // 设置表头复选框的委托 tableView->horizontalHeader()->setItemDelegate(new CheckBoxHeaderDelegate(tableView)); ``` 通过上述方法,可以在Qt模型-视图架构中实现表头显示复选框的功能,并支持用户交互操作。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangzhangkeji

谢谢支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值