我是靠谱客的博主 羞涩猎豹,这篇文章主要介绍自定义 QHeaderView 绘制 QCheckBox (可配置样式的 QCheckBox),现在分享给大家,希望可以做个参考。

即通过 paintSection 将 QCheckBox 绘制到 QHeaderView 的指定列

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class SCheckBoxHeaderView : public QHeaderView { Q_OBJECT private: bool isChecked; int m_checkColIdx; public: SCheckBoxHeaderView(int checkColumnIndex, Qt::Orientation orientation, QWidget * parent = 0) : QHeaderView(orientation, parent) { m_checkColIdx = checkColumnIndex; isChecked = false; } signals: void checkStausChange(bool); protected: void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const { painter->save(); QHeaderView::paintSection(painter, rect, logicalIndex); if (logicalIndex == m_checkColIdx) { QStyleOptionButton option; // checkBox 需要设置 parent 后方便通过全局stylesheet 设置 checkBox 样式(大小似乎无法通过样式设置) QCheckBox *checkBox = new QCheckBox(dynamic_cast<QWidget *>(this->parent())); // QRect(x, y, width, height) 控制绘制的 QCheckBox 位置与大小 (注:大小可能偏大,需要自行尝试) option.rect = QRect(3, 5, 21, 21); if (isChecked) option.state = QStyle::State_On; else option.state = QStyle::State_Off; this->style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &option, painter, checkBox); } } void mousePressEvent(QMouseEvent *event) { if (visualIndexAt(event->pos().x()) == m_checkColIdx) { isChecked = !isChecked; this->updateSection(m_checkColIdx); emit checkStausChange(isChecked); } QHeaderView::mousePressEvent(event); } };

最后

以上就是羞涩猎豹最近收集整理的关于自定义 QHeaderView 绘制 QCheckBox (可配置样式的 QCheckBox)的全部内容,更多相关自定义内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(65)

评论列表共有 0 条评论

立即
投稿
返回
顶部