Qt World Summit: Register Today!

[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 :

    |__ 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 :-


    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);


    You also need to set the selection mode for your treeView

    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.


  • 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