[SOLVED] Context menu for QListView entries
-
wrote on 31 Jul 2015, 11:01 last edited by mkolenda
I know how to set
Qt::CustomContextMenu
for QListView. I want to make two different menus for QListView and for items inside of it.I mean, when user click item in QListView he may want to see something like properties or edit this entry.
If he click on QListView beyond items he may want to add new item to this list.
How to make different menu for item? ( and know what item it was )
-
wrote on 31 Jul 2015, 11:51 last edited by
Hi!
Maybe what you want isn't an item specific context menu but a custom item delegate (see: http://doc.qt.io/qt-5/model-view-programming.html#delegate-classes). -
Hi!
Maybe what you want isn't an item specific context menu but a custom item delegate (see: http://doc.qt.io/qt-5/model-view-programming.html#delegate-classes).wrote on 31 Jul 2015, 15:49 last edited by mkolendaWorking code:
void MainWindow::listWidgetContextMenu(const QPoint &pos) { QPoint globalpos = ui->listWidget->mapToGlobal(pos); QMenu menuBeyondItem; QAction* action_addElement = menuBeyondItem.addAction("Add"); QMenu menuForItem; QAction* action_editElement = menuForItem.addAction("Edit"); QListWidgetItem* pointedItem = ui->listWidget->itemAt(pos); QAction* selectedAction; if(!pointedItem) { selectedAction = menuBeyondItem.exec(globalpos); if(selectedAction) { if(selectedAction == action_addElement) { qDebug() << "Add"; } } } else { selectedAction = menuForItem.exec(globalpos); if(selectedAction) { if(selectedAction == action_editElement) { qDebug() << "Edit"; } } } }
What I didn't know was existence of
QListWidget::itemAt(QPoint)
function.For QListView use
QModelIndex QListView::indexAt(const QPoint & p) const
function. -
wrote on 29 Nov 2019, 07:20 last edited byThis post is deleted!
-
Hi and welcome to devnet,
Please don't necropost such old thread.
In any case, the point is provided by the customContextMenuRequested signal.