Context menu on QTreeView item?
-
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?
-
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?
You can use QAIV::indexAt().
-
You can use QAIV::indexAt().
@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-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(...) ... }
@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
-