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. Context menu for tree view: Accessing model index in action method

Context menu for tree view: Accessing model index in action method

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 2.1k 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.
  • R Offline
    R Offline
    Remoneo
    wrote on last edited by
    #1

    Hi,

    I created a custom context menu for a tree view (customContextMenuRequested). This works fine. Inside the customContextMenuRequested method I have access to the model index. The actions in the context menu have their own slots. What is the appropriate way to pass the model index to this slots?

    Remoneo

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      One way to do it is when you create the menu you can set the model index as data on your QAction and in your slot you can retrieve that index.

      Hope it helps

      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
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        I'd advise against doing that. You should not store a QModelIndex. In the time that your context menu is visible and waiting for user interaction, the underlying model may already have changed and index be invalidated. Also, this requires your slot to use the sender() function to get a pointer to the QAction that triggered the action. I'd try to avoid that. If you use C++11, you could use a lambda instead though for your connection. That would avoid having to store anything in the action itself.

        There are two options: if you do want to store the QModelIndex, use a QPersistentModelIndex. Otherwise, simply query QItemSelectionModel again at the moment of execution to get the current item or the selected item(s) to operate on. These will be updated properly.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Andre is completely right and I just saw that what I wrote wasn't what I had in mind.

          I was thinking about the index coordinates as in (row, column) but that word didn't reach the keyboard.

          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
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            [quote author="SGaist" date="1422951027"]Andre is completely right and I just saw that what I wrote wasn't what I had in mind.

            I was thinking about the index coordinates as in (row, column) but that word didn't reach the keyboard.[/quote]

            Even those might not be what you want. What if you click on (3,0) and you get your context menu, and then the model decides to insert a new row at the top of the model? That will move the row you just clicked on down to (4,0). Now you click the delete option in that menu, and... where did my (what used to be) row 2 go? Or the other way around: what if the model decided to remove row 0 in the meantime, and now there is no row 3 any more (the item you clicked became row 2 when the model removed row 0).

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              [quote author="Andre" date="1422951693"][quote author="SGaist" date="1422951027"]Andre is completely right and I just saw that what I wrote wasn't what I had in mind.

              I was thinking about the index coordinates as in (row, column) but that word didn't reach the keyboard.[/quote]

              Even those might not be what you want. What if you click on (3,0) and you get your context menu, and then the model decides to insert a new row at the top of the model? That will move the row you just clicked on down to (4,0). Now you click the delete option in that menu, and... where did my (what used to be) row 2 go? Or the other way around: what if the model decided to remove row 0 in the meantime, and now there is no row 3 any more (the item you clicked became row 2 when the model removed row 0). [/quote]

              Indeed, it was just a correction of my original proposition and nothing more. Using the selection model is safer. One thing I see with the model being updated between the context menu call and the slot invocation is that the selection might get lost depending on how the update was done.

              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
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                That will only happen if the model is reset, or the item you had selected was removed from the model.

                Resetting models is not something you should do if you can avoid it at all, and if the model removed the item you had selected, then you cannot operate on it anymore anyway right?

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Yep, 100% right !

                  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
                  0

                  • Login

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