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 get right-clicked item

QTreeView get right-clicked item

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.2k 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.
  • T Offline
    T Offline
    Taytoo
    wrote on last edited by
    #1

    Using QTreeView paired with QStandardItemModel and trying to get contenxt-menu working when a user-right clicks on an item and then display custom menu for each item.

    In mainwindow.cpp code, I connected customContextMenuRequested signal with my slot - which is getting invoked properly. The issue I'm having is getting the item that the user right-clicked.

            auto pCurrModel = (QStandardItemModel*)pProxyModel->sourceModel();
    	auto index = ui.treeView->indexAt(pos);
    	auto item = pCurrModel->itemFromIndex(index);
    

    The code above is from OnCustomContextMenuRequested slot. I'm getting a valid index from indexAt() But itemFromIndex() always returns nullptr?

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

      Hi
      Do you have a proxymodel or anything like that active also ?
      So code should be something like

      QStandardItem *item = mymodel->itemFromIndex( mysortproxy ->mapToSource(index) );
      

      update:
      Ah. pProxyModel->sourceModel(); suggest you do have :)
      Amazing what eyes sometimes skips...

      JonBJ 1 Reply Last reply
      2
      • mrjjM mrjj

        Hi
        Do you have a proxymodel or anything like that active also ?
        So code should be something like

        QStandardItem *item = mymodel->itemFromIndex( mysortproxy ->mapToSource(index) );
        

        update:
        Ah. pProxyModel->sourceModel(); suggest you do have :)
        Amazing what eyes sometimes skips...

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

        @mrjj
        Just to say: what a coincidence! I am literally sitting down right now to add right-click onto items in a QTreeView using a proxy model!

        mrjjM 1 Reply Last reply
        1
        • JonBJ JonB

          @mrjj
          Just to say: what a coincidence! I am literally sitting down right now to add right-click onto items in a QTreeView using a proxy model!

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @JonB

          Heh.
          That is perfect timing and also made me read his code properly
          and i do think he has a proxy :)

          T 1 Reply Last reply
          0
          • mrjjM mrjj

            @JonB

            Heh.
            That is perfect timing and also made me read his code properly
            and i do think he has a proxy :)

            T Offline
            T Offline
            Taytoo
            wrote on last edited by
            #5

            @mrjj Yes, I am using Proxy model and thanks for the quick solution. It works!

            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