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. QTreeView context menu item
Qt 6.11 is out! See what's new in the release blog

QTreeView context menu item

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 2.4k Views 2 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.
  • JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #1

    At present I have a QTreeView, bound to a (derived) QAbstractItemModel. I have a right-click context menu with a QAction, whose triggered connects to a slot. In the slot I need to access which model item was right-clicked, but I don't think I can see that from the action/slot?

    How is this easiest achieved?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      The only way I see is to as the QTreeView for the current index.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

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

        Hi,

        Using the customContextMenuRequested signal you can get the index using QTreeView::indexAt.

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

        JonBJ 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi,

          Using the customContextMenuRequested signal you can get the index using QTreeView::indexAt.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @SGaist
          Thanks, yes, I have seen that approach. And I may indeed change over. But that happens at the context-menu-requested stage, right? I need to know at the QAction-slot-execute phase. So you want me to either:

          • Store up the item at the right-click phase, and apply that at the action-phase; or
          • Pass the item at the right-click phase as a parameter to the action-slot phase

          Is that right? Or am I going doolally?

          @Christian-Ehrlicher

          The only way I see is to as [edit: "ask"] the QTreeView for the current index.

          Thank you. I will investigate tomorrow. I am so tired today :(

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

            IIRC, when you right click on an item, it becomes selected so you can use the QTreeView::selectedIndexes in your action's slot.

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

            JonBJ 1 Reply Last reply
            1
            • SGaistS SGaist

              IIRC, when you right click on an item, it becomes selected so you can use the QTreeView::selectedIndexes in your action's slot.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @SGaist
              Tx so much, I will look tomorrow

              The point is, as I think both you & @Christian-Ehrlicher are confirming, the QAction/slot has no concept of which "item" you were on? You either have to look up the item by converting a click coordinate position to an item (QTreeView::itemAt()), or you have to look at whatever item is currently selected ( QTreeView::selectedIndexes). Is that right, that's what I need to know?

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

                AFAIK, yes.

                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
                2
                • JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  Thanks guys, I will hopefully report back and close issue tomorrow.

                  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