Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[SOLVED] Auto select child in QTreeView



  • I'm using QTreeView (model) to show some categories for user to select. Let say I have this tree view :

    @Linux
    |__ Ubuntu
    |__ Fedora
    |__ OpenSUSE@

    How can the tree view will select all the child under Linux when I click it?



  • First you need to connect signal (clicked()) for your treeView :-

    @connect(m_treeView,SIGNAL(clicked(QModelIndex)),SLOT(onTreeViewClicked(QModelIndex)));@

    and in the implementation code you can write:

    @void YourClass::onTreeViewClicked(const QModelIndex &index)
    {
    QStandardItem *item = treeModel->itemFromIndex(index);

    for (int r=0; r<item->rowCount(); r++)
    {
        QModelIndex childIndex = treeModel->index(r,0,index);
        m_treeView->selectionModel()->select(childIndex,QItemSelectionModel::Select);
    }
    

    }@

    You also need to set the selection mode for your treeView
    @m_treeView->setSelectionMode(QAbstractItemView::MultiSelection);@

    You can change/clear the selection as per your requirement.



  • I know it's late...thank you!



  • You are welcome :)

    If the issue is solved , Kindly edit your first post and add [SOLVED] to the title.

    Regards
    Soumitra.



  • i'm using this code and it perfectly works, but also i want to give the user the ability to deselect each one he wished! any ideas??


Log in to reply