Add a QCheckBox as a header to a QTableWidget
-
@hbatalha It's an image so changing pen, brush or palette doesn't affect it. It might appear disabled if you give it the wrong state flags when painting, so check that the state member is set correctly in the style option.
The checkbox also appears crooked in your screenshot, which suggests the rect member is set incorrectly, so again make sure you set all the members of your style options object correctly before painting with it.wrote on 18 Jun 2021, 20:31 last edited by hbatalha@Chris-Kawa said in Add a QCheckBox as a header to a QTableWidget:
so again make sure you set all the members of your style options object correctly before painting with it.
The code is like the one you provided in your answer above:
class CheckBox : public QHeaderView { Q_OBJECT public: using QHeaderView::QHeaderView; signals: void clicked(bool); protected: void paintSection(QPainter* painter, const QRect &rect, int logicalIndex) const override { painter->save(); QHeaderView::paintSection(painter, rect, logicalIndex); painter->restore(); if (model() && logicalIndex == 0) { QStyleOptionButton option; option.initFrom(this); QRect checkbox_rect = style()->subElementRect(QStyle::SubElement::SE_CheckBoxIndicator, &option, this); checkbox_rect.moveLeft(rect.left()); bool checked = model()->headerData(logicalIndex, orientation(), Qt::CheckStateRole).toBool(); option.rect = checkbox_rect; option.state = checked ? QStyle::State_On : QStyle::State_Off; style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &option, painter); } } public: void mouseReleaseEvent(QMouseEvent* event) override { QHeaderView::mouseReleaseEvent(event); if(model()) { int section = logicalIndexAt(event->pos()); if (section >= 0) { bool checked = model()->headerData(section, orientation(), Qt::CheckStateRole).toBool(); model()->setHeaderData(section, orientation(), !checked, Qt::CheckStateRole); viewport()->update(); emit clicked( ! checked); } } } };
Edit: The problem is only on windows for the looks of it as I just tested it on linux mint 20.1 and it looks totally fine
-
wrote on 22 Jul 2022, 07:34 last edited by
No one mentioned how to add Checkbox in QTableWidget header with PartiallyChecked support, anyone knowns how to make it? There only option for Checkbox is just QStyle::State_On and QStyle::State_Off, which is far from functional. The most important option QStyle::State_Partial is not found.
-
No one mentioned how to add Checkbox in QTableWidget header with PartiallyChecked support, anyone knowns how to make it? There only option for Checkbox is just QStyle::State_On and QStyle::State_Off, which is far from functional. The most important option QStyle::State_Partial is not found.
wrote on 22 Jul 2022, 07:44 last edited by JonB@nonskill
Did you tryQStyle::State_NoChange
("Used to indicate a tri-state checkbox.") from flags QStyle::State?