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 on QTreeView item?
Forum Updated to NodeBB v4.3 + New Features

Context menu on QTreeView item?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 665 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.
  • R Offline
    R Offline
    rjmx
    wrote on 27 Dec 2023, 02:19 last edited by
    #1

    I have a QTreeView of a heirarchy of items, and it works fine. I've set up a context menu on that tree view, and that gets me a QPoint when I right-click on an item and select an action. How do I get the item I've right-clicked on from that QPoint?

    Here's what I'm aiming for:

    Item 1
    Item 2
        |____ Item 2a <---- Right-click here
        |____ Item 2b
    Item 3
    

    I want to be able to right-click on Item 2a here, select "delete" from the context menu, and use the resulting QPoint to delete Item 2a and its associated data from the treeview. How do I go about that?

    C 1 Reply Last reply 27 Dec 2023, 08:10
    0
    • R rjmx
      27 Dec 2023, 02:19

      I have a QTreeView of a heirarchy of items, and it works fine. I've set up a context menu on that tree view, and that gets me a QPoint when I right-click on an item and select an action. How do I get the item I've right-clicked on from that QPoint?

      Here's what I'm aiming for:

      Item 1
      Item 2
          |____ Item 2a <---- Right-click here
          |____ Item 2b
      Item 3
      

      I want to be able to right-click on Item 2a here, select "delete" from the context menu, and use the resulting QPoint to delete Item 2a and its associated data from the treeview. How do I go about that?

      C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 27 Dec 2023, 08:10 last edited by Christian Ehrlicher
      #2

      You can use QAIV::indexAt().

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

      M 1 Reply Last reply 27 Dec 2023, 14:00
      2
      • C Christian Ehrlicher
        27 Dec 2023, 08:10

        You can use QAIV::indexAt().

        M Offline
        M Offline
        mpergand
        wrote on 27 Dec 2023, 14:00 last edited by mpergand
        #3

        @Christian-Ehrlicher said in Context menu on QTreeView item?:
        QAIV ???
        itemAt() doesn't exist for QTreeView.
        It's QTreeView::indexAt()

        But in context menu, you receive a signal in a slot with no explicit mouse location.

        @rjmx
        You can retreive the selected cell, here some code of mine:

        void BookmarksManager::contextMenuAction(QAction *action)
        {
        	QModelIndex selectedIndex;
        
        	bool hasSelection=!sBookmarksDialog->treeView()->selectionModel()->selectedRows().isEmpty();
        
        	if(hasSelection)
        	{
        		selectedIndex=sBookmarksDialog->treeView()->selectionModel()->selectedRows().first();
        		
        		if(action->objectName()==Menu_Delete)
        		{
        		// delete item here
        		}
        		else if(...)
        		...
        	}
        
        C 1 Reply Last reply 27 Dec 2023, 14:14
        0
        • M mpergand
          27 Dec 2023, 14:00

          @Christian-Ehrlicher said in Context menu on QTreeView item?:
          QAIV ???
          itemAt() doesn't exist for QTreeView.
          It's QTreeView::indexAt()

          But in context menu, you receive a signal in a slot with no explicit mouse location.

          @rjmx
          You can retreive the selected cell, here some code of mine:

          void BookmarksManager::contextMenuAction(QAction *action)
          {
          	QModelIndex selectedIndex;
          
          	bool hasSelection=!sBookmarksDialog->treeView()->selectionModel()->selectedRows().isEmpty();
          
          	if(hasSelection)
          	{
          		selectedIndex=sBookmarksDialog->treeView()->selectionModel()->selectedRows().first();
          		
          		if(action->objectName()==Menu_Delete)
          		{
          		// delete item here
          		}
          		else if(...)
          		...
          	}
          
          C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 27 Dec 2023, 14:14 last edited by Christian Ehrlicher
          #4

          @mpergand The link was to QAIV::indexAt(). Just the link text was wrong as I first was on the item widgets instead views.

          QAIV = QAbstractItemView

          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
          0
          • R Offline
            R Offline
            rjmx
            wrote on 31 Dec 2023, 02:52 last edited by
            #5

            Thanks, folks. That worked fine.

            1 Reply Last reply
            0
            • R rjmx has marked this topic as solved on 31 Dec 2023, 02:53

            1/5

            27 Dec 2023, 02:19

            • Login

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