Important: Please read the Qt Code of Conduct -

How can I select a child node in a QTreeView?

  • Hi, I have a QTreeView with nested nodes, I need to select a child node according to an index in a table.
    Here's what I do:

            QModelIndex nIndex = view.model()->index(tableIndex, 0);
            view.selectionModel()->select(nIndex, QItemSelectionModel::Select | QItemSelectionModel::Rows);

    for example: when tableIndex == 3 I expect to select the 3rd child node, but this selects the 3rd parent row

  • Lifetime Qt Champion


    The third child node of what ? Overall ? Of the top level item ? Of the third top level item ?

  • @nzur said in How can I select a child node in a QTreeView?:

    index(tableIndex, 0)

    You are missing the 3rd argument of index. How can it know what parent to take it from? for example, for the third child of the first parent:
    QModelIndex nIndex = view.model()->index(tableIndex, 0, view.model()->index(0,0));

  • Thx, That's it!!!

Log in to reply