Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved How can I select a child node in a QTreeView?

    General and Desktop
    3
    4
    2089
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • N
      nzur last edited by VRonin

      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()->clearSelection();
              view.selectionModel()->select(nIndex, QItemSelectionModel::Select | QItemSelectionModel::Rows);
              view.scrollTo(nIndex);
      

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

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

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

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 1
        • VRonin
          VRonin last edited by

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

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply Reply Quote 2
          • N
            nzur last edited by

            Thx, That's it!!!

            1 Reply Last reply Reply Quote 0
            • First post
              Last post