Search code examples
qtqtreeview

How to select child's items checkBoxs in QTreeView when select their parent's checkbox


I want to select/unselect all child's items QCheckBoxs when I select/unselect their parent's item QCheckBox.

i inherit from QTreeView and detect when the QCheckBox is selected then i call function to do selecting/unselecting process.

here my code:

#ifndef MYQTREEVIEW_H
#define MYQTREEVIEW_H

#include <QTreeView>
#include <QMouseEvent>
#include <QDebug>
#include <QStandardItem>

class MyQTreeView: public QTreeView {
public:
    MyQTreeView(QWidget* parent=0): QTreeView(parent){}
    virtual ~MyQTreeView() {}

protected:
    void resettingCheckBox (QModelIndex& parentIndex) {
        if ( ! parentIndex.isValid() )
            return;

        QString text = parentIndex.data( Qt::DisplayRole ).value<QString>();
        qDebug() << "parent is: " << text;

        if ( model()->hasChildren(parentIndex) ) {
            for( int i = 0; i < model()->rowCount(parentIndex) ; i++ ) {
                QModelIndex childIndex = model()->index( i, 0, parentIndex );

                if ( model()->hasChildren(childIndex) )
                    resettingCheckBox(childIndex);
                else {
                    QString text = childIndex.data( Qt::DisplayRole ).value<QString>();
                    qDebug() << "child is: " << text;

                    QStandardItem *parentItem = static_cast<QStandardItem*> (parentIndex.internalPointer());
                    QStandardItem *childItem = static_cast<QStandardItem*> (childIndex.internalPointer());

                    if ( parentItem->checkState() == Qt::Checked ) {
                        qDebug() << "child item " << childItem->checkState();
                        childItem->setCheckState( Qt::Unchecked);

                    }
                    else {
                        qDebug() << "child item " << childItem->checkState();
                        childItem->setCheckState(  Qt::Checked);
                    }
                }
            }
        }
    }

    void mousePressEvent (QMouseEvent *event) {
        QModelIndex index = indexAt(event->pos());

        if(index.isValid()) {
            QStyleOptionButton opt;
            opt.QStyleOption::operator=(viewOptions());
            opt.rect = visualRect(index);
            QRect rect = style()->subElementRect(QStyle::SE_ViewItemCheckIndicator, &opt);

            if (rect.contains(event->pos())) {
                resettingCheckBox(index);
            }

            QTreeView::mousePressEvent(event);
        }
    }
};

#endif // MYQTREEVIEW_H

the code is not working probably when i select/unselect parent checkBox (subchilds is not selected/unselected).

Thanks in advance.


Solution

  • I believe the best way to manipulate treeview items is through the model. It looks like you're using QStandardItemModel; so you can override your model's setData method and reset child items values for the item index passed as a parameter to this method. Below is a small example:

    class TestModel : public QStandardItemModel
    {
    public:
        TestModel() {}
    
        bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole)
        {
            QStandardItem *item = itemFromIndex(index);
            for( int i = 0; i < item->rowCount() ; i++ )
            {
                QStandardItem *childItem = item->child(i);
                setData(childItem->index(), value, role);
            }
            return QStandardItemModel::setData(index, value, role);
        }
    };
    

    here's how this model gets initialized:

    QStandardItemModel* tableModel = new TestModel();
    QStandardItem* parentItem = tableModel->invisibleRootItem();
    for (int i = 0; i < 4; ++i)
    {
         QStandardItem *item = new QStandardItem(QString("item %0").arg(i));
         item->setCheckable(true);
         parentItem->appendRow(item);
         parentItem = item;
    }
    treeView->setModel(tableModel);
    

    hope this helps, regards