Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Get the index of an item from QTreeView

Get the index of an item from QTreeView

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 1.3k Views 3 Watching
  • 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.
  • V Offline
    V Offline
    vijaychsk
    wrote on last edited by
    #1

    Hi,
    I have a QTreeView that is working fine. Separate from this view, I have another QRegLineEdit widget where users can search using a full hierarchical path (like top.U1 or top.U3.U3_1 or top.U4.U4_1_1) and I display properties to that path. It is also working fine because both are independent of each other which means QRegLineEdit doesn't depend on any data from QTreeModel.

    However, I now want to connect QRegLineEdit and QTreeView. Whenever a user enters a path, I perform some basic checks on whether the input is right or wrong and then I would like to highlight or scroll to that hierarchy/parent in the QTreeView.

    How do I do that reverse lookup? If I have the index of the item in the QTree then I can do the scrollTo method to go to that item. I just don't know how to get the index from a string.

    It is to be noted that I cannot simply search for a child say U4_U4_1_1 because it can exist under different parents (duplicates possible) but the full hierarchical path is unique.

    top
        -- U1
        -- U2
        -- U3
             -- U3_1
             -- U3_2
        -- U4
             -- U4_1
                  -- U4_1_1
                  -- U4_1_2
        -- U5
    
    JonBJ 1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by
      #2

      One way is to keep a lookup table/map where the path is the key, and the pointer to the tree leaf/node is the data.

      If you meet the AI on the road, kill it.

      1 Reply Last reply
      0
      • jeremy_kJ Offline
        jeremy_kJ Offline
        jeremy_k
        wrote on last edited by
        #3

        QAbstractItemModel::match() is worth a look.

        Asking a question about code? http://eel.is/iso-c++/testcase/

        1 Reply Last reply
        0
        • V vijaychsk

          Hi,
          I have a QTreeView that is working fine. Separate from this view, I have another QRegLineEdit widget where users can search using a full hierarchical path (like top.U1 or top.U3.U3_1 or top.U4.U4_1_1) and I display properties to that path. It is also working fine because both are independent of each other which means QRegLineEdit doesn't depend on any data from QTreeModel.

          However, I now want to connect QRegLineEdit and QTreeView. Whenever a user enters a path, I perform some basic checks on whether the input is right or wrong and then I would like to highlight or scroll to that hierarchy/parent in the QTreeView.

          How do I do that reverse lookup? If I have the index of the item in the QTree then I can do the scrollTo method to go to that item. I just don't know how to get the index from a string.

          It is to be noted that I cannot simply search for a child say U4_U4_1_1 because it can exist under different parents (duplicates possible) but the full hierarchical path is unique.

          top
              -- U1
              -- U2
              -- U3
                   -- U3_1
                   -- U3_2
              -- U4
                   -- U4_1
                        -- U4_1_1
                        -- U4_1_2
              -- U5
          
          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by
          #4

          @vijaychsk
          You could keep a "lookup table/map" as @Kent-Dorfman says. But potentially that could be "costly" to maintain, especially if the tree is large/deep.

          There are two ways you can do it just from the information already held in the tree model.

          • Do like QFileSystemModel does it. It defines a role QFileSystemModel::FilePathRole Qt::UserRole + 1. That role returns the full file path from each node/leaf. You can then search for this full path (e.g. via @jeremy_k's QAbstractItemModel::match()). This is simple but "expensive": the tree does a lot of looking back up at parentage (unless you do some extra work) to generate each item's full path to compare.

          • Just look down the tree for each element in turn from the desired path. For example, if looking for /a/b/c, first find a at top-level, then b among its children, and finally c among b's children. You could do that easily yourself from model's nodes' children, or maybe from QAbstractItemModel::match() without the Qt::MatchRecursive flag being set in Qt::MatchFlags flags.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved