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?

Context menu on QTreeView item?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.0k 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 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?

    Christian EhrlicherC 1 Reply Last reply
    0
    • R rjmx

      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?

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 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
      2
      • Christian EhrlicherC Christian Ehrlicher

        You can use QAIV::indexAt().

        M Offline
        M Offline
        mpergand
        wrote on 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(...)
        		...
        	}
        
        Christian EhrlicherC 1 Reply Last reply
        0
        • M mpergand

          @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(...)
          		...
          	}
          
          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 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 last edited by
            #5

            Thanks, folks. That worked fine.

            1 Reply Last reply
            0
            • R rjmx has marked this topic as solved on

            • Login

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