Want to have checkboxes in headers of QTreeWiddget
-
I have QTreeWidget and I want to create checkboxes in QtreeWidget headers
for exampleI tried following code and it is not working
QTreeWidget myTree = new QtreeWidget;
myTree->setHeaderLabels(QStringList() << tr("name") << tr ("v") << tr("v"));myTree->setColumnCount(3);
myTree->setHeaderLabels(QStringList() << tr("name") << tr("v") << tr("s"));
QTreeWidgetItem * header =myTree->headerItem();
header->setCheckState(1,Qt::Checked);
header->setCheckState(2,Qt::Checked);and it is not working Can someone guide me on the same
-
@Qt-Enthusiast
hi
THis should give good hints
http://blog.qt.io/blog/2014/04/11/qt-weekly-5-widgets-on-a-qheaderview/ -
https://wiki.qt.io/Qt_project_org_faq#How_can_I_insert_a_checkbox_into_the_header_of_my_view.3F
P.S.
This was basically a LMGTFY... 😒 -
@Qt-Enthusiast
Hi
That part is left as an exercise for the reader."You would also need to reimplement the mousePressEvent() http://doc.qt.io/qt-5/qheaderview.html#mousePressEvent to detect when the checkbox is clicked, in order to paint the checked and unchecked states."
-
Is it possible to have these headers per column basis, I don not see anything in such documenattion
-
@mrjj
http://blog.qt.io/blog/2014/04/11/qt-weekly-5-widgets-on-a-qheaderview/
Actually this sample have issues
It wont call paintSection every time and hence its not redrawn correctly.It seems that
this->update();
will not always trigger it to paint whole header. ( call paintSection)Anyone have good input on why ?
#include <QtGui> #include <QHeaderView> #include <QTableWidget> #include <QDebug> class MyHeader : public QHeaderView { public: MyHeader(Qt::Orientation orientation, QWidget* parent = 0) : QHeaderView(orientation, parent) { } protected: void paintSection(QPainter* painter, const QRect& rect, int logicalIndex) const { painter->save(); QHeaderView::paintSection(painter, rect, logicalIndex); painter->restore(); qDebug() << "Paint FIRST"; if (logicalIndex == 0) { QStyleOptionButton option; option.rect = QRect(10, 10, 10, 10); if (isOn) option.state = QStyle::State_On; else option.state = QStyle::State_Off; this->style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &option, painter); qDebug() << "Paint logicalIndex"; } } void mousePressEvent(QMouseEvent* event) { QHeaderView::mousePressEvent(event); if (isOn) isOn = false; else isOn = true; this->update(); qDebug() << "mousePressEvent"; } private: bool isOn; protected: }; int main(int argc, char** argv) { QApplication app(argc, argv); QTableWidget table; table.setRowCount(4); table.setColumnCount(3); MyHeader* myHeader = new MyHeader(Qt::Horizontal, &table); table.setHorizontalHeader(myHeader); table.show(); return app.exec(); }
-
Please suggest the solution
-
@Qt-Enthusiast the
the check box is not easily clickable if you can suggest the proper solution
-
@Qt-Enthusiast
Did not find any.
The sample listed on wiki do not repaint headers (always) so that did not work well. -
Can u suggest some alternative solution
-
@Qt-Enthusiast
Hi Sadly no. The sample would work if update() would make the header to be updated but
it do not as far as i can tell.