Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to get QModelIndex of children in QML TreeView
QtWS25 Last Chance

How to get QModelIndex of children in QML TreeView

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 2 Posters 2.6k Views
  • 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.
  • M Offline
    M Offline
    maxwell31
    wrote on last edited by maxwell31
    #1

    Hi,

    I have a QML TreeView which shows a QStandardItemModel. The QStandardItemModel has different files on the first level, and each file can have several calculations as children. The view displays this like this

    • file 1
      • calculation 1
      • calculation 2
    • file 2
      -file 3
      • calculation 1

    but for files and calculations, I use different delegates (using a Loader). In order to set the selection of an ItemSelectionModel assigned to the TreeView, I do the following:

          treeView.selection.clearSelection()
          treeView.selection.select(myList.index(model.index,0),2)
    

    This works perfectly for my file delegate, but in the calculation delegate, for some reason myList.index(model.index,0) seems not to work. A console.log of the Index gives QModelIndex(). I wonder whether this is because the calculations are child items of the files?

    But more precisely: How do I get the QModelIndex() of the calculation entries in my view?

    Gojir4G 1 Reply Last reply
    0
    • M maxwell31

      Hi,

      I have a QML TreeView which shows a QStandardItemModel. The QStandardItemModel has different files on the first level, and each file can have several calculations as children. The view displays this like this

      • file 1
        • calculation 1
        • calculation 2
      • file 2
        -file 3
        • calculation 1

      but for files and calculations, I use different delegates (using a Loader). In order to set the selection of an ItemSelectionModel assigned to the TreeView, I do the following:

            treeView.selection.clearSelection()
            treeView.selection.select(myList.index(model.index,0),2)
      

      This works perfectly for my file delegate, but in the calculation delegate, for some reason myList.index(model.index,0) seems not to work. A console.log of the Index gives QModelIndex(). I wonder whether this is because the calculations are child items of the files?

      But more precisely: How do I get the QModelIndex() of the calculation entries in my view?

      Gojir4G Offline
      Gojir4G Offline
      Gojir4
      wrote on last edited by
      #2

      @maxwell31 Hi,

      Probably you need to give the parent to get the index of a child from index() method:

      treeView.selection.select(myList.index(model.index,0, indexOfParent),2)
      

      It's working with file delegate because their parents is the root, and it's like if they have no parents.

      1 Reply Last reply
      2
      • M Offline
        M Offline
        maxwell31
        wrote on last edited by
        #3

        Thank you. You are right. However, how can I obtain the parent?

        Gojir4G 1 Reply Last reply
        0
        • M maxwell31

          Thank you. You are right. However, how can I obtain the parent?

          Gojir4G Offline
          Gojir4G Offline
          Gojir4
          wrote on last edited by
          #4

          @maxwell31 That's a good question. I never used TreeView...

          I would say you can use QModelIndex::parent(). But it seems it's not callable from QML.

          treeView.selection.select(myList.index(model.index, 0, model.index.parent()), 2) 
          

          Why do you need to make this selection ? is it coming from user ?

          1 Reply Last reply
          2
          • M Offline
            M Offline
            maxwell31
            wrote on last edited by
            #5

            Yes, the user should be able to click on an entry, and the state of the main window in the GUI will change. The TreeView is displayed next to the main window and I would like the selection to indicate what is displayed in the main window

            1 Reply Last reply
            0
            • M Offline
              M Offline
              maxwell31
              wrote on last edited by
              #6

              Ok, the error seems to be that I used model.index instead of styleData.index. styleData.index seems to give the QModelIndex. So do I understand it right, that model.index gives the row in the TreeView and styleData.index has the QModelIndex?

              Gojir4G 1 Reply Last reply
              0
              • M maxwell31

                Ok, the error seems to be that I used model.index instead of styleData.index. styleData.index seems to give the QModelIndex. So do I understand it right, that model.index gives the row in the TreeView and styleData.index has the QModelIndex?

                Gojir4G Offline
                Gojir4G Offline
                Gojir4
                wrote on last edited by
                #7

                @maxwell31 I don't know about model.index. But according to itemDelegate documentation: styleData.index - the QModelIndex of the current item in the model

                You can also retrieve the row and column index using styleData.row and styleData.column

                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