Important: Please read the Qt Code of Conduct -

Can we have checkbox in Header of QTreeView along with Sorting symbol

  • Hi All
    Can we have a checkbox in Header of QTreeView along with Sorting symbox . When I click on checkbox , then I should be able to perform some actions

  • Hi @qtEnthusiast ,

    Seems this works for you.

    Keep tracking the the QtreeView for the whether the checkbox is checked or not. you can find here.

  • As I understand it for each item in QtreeWidget . I am asking on common check box for header label in QtreeView it is different . LIke we have sorting mark in QtreeView like that

  • @Qt-Enthusiast
    you can reimplement QHeaderView and you can make use of QMultiComboBox like this .

  • I could not make out for example (As I was not able to access the example code). if some one can elaborate on how implement checkbox in header of QTreeView/ I have following code using QTreeView

    #include <QApplication>
    #include <QSplitter>
    #include <QTreeView>
    #include <QListView>
    #include <QTableView>
    #include <QStandardItemModel>

    int main( int argc, char **argv ) {

    QApplication app( argc, argv );
    QTreeView *tree = new QTreeView();
    QListView *list = new QListView();
    QTableView *table = new QTableView();
    QSplitter splitter;
    splitter.addWidget( tree );
    splitter.addWidget( list );
    splitter.addWidget( table );
    QStandardItemModel model( 5, 2 );

    for( int r=0; r<5; r++ )
    for( int c=0; c<2; c++) {
    QStandardItem *item = new QStandardItem( QString("Row:%0, Column:%1").arg(r).arg(c) );
    if( c == 0 )
    for( int i=0; i<3; i++ ) {
    QStandardItem *child = new QStandardItem( QString("Item %0").arg(i) );
    child->setEditable( false );
    item->appendRow( child );
    model.setItem(r, c, item);

    model.setHorizontalHeaderItem( 0, new QStandardItem( "Foo" ) );
    model.setHorizontalHeaderItem( 1, new QStandardItem( "Bar-Baz" ) );
    tree->setModel( &model );
    list->setModel( &model );
    table->setModel( &model );
    list->setSelectionModel( tree->selectionModel() );
    table->setSelectionModel( tree->selectionModel() );
    table->setSelectionBehavior( QAbstractItemView::SelectRows );
    table->setSelectionMode( QAbstractItemView::SingleSelection );;
    return app.exec();


  • I tried the example here in the link

    But I could not get how to connect slot on click/unclick of the check box . Can some one quide for the that . or its there any alternative way to implement check box in the header

  • Lifetime Qt Champion

    Hi that sample does not use a real checkbox so there is no signal.
    but you can easy make your own signal for it.

    class MyHeader : public QHeaderView
      void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const ....
     signals: ///
     void CheckedChanged( bool state); /// define signal
      void mousePressEvent(QMouseEvent *event)
    ..... ...... ...
      // emit ur new signal
      emit CheckedChanged( isOn );

Log in to reply