How can I access Model index via contextMenuEvent?
-
Hi,
I have a custom Model that inherits from QAbstractListModel and I use QListView to access the data in it.I want to right click on a single item from the list and know which item from the Model that is.
Why in the below code, the returned "item" index is wrong and later not recognised bymyListView->model()->data(item).toString();
Please see the below code snippet:
void MyClass::contextMenuEvent(QContextMenuEvent* event) { if (event->reason() == QContextMenuEvent::Mouse) { QModelIndex item = myListView->indexAt(event->pos()); //blah, blah, blah } }
-
indexAt() expects local coordinates of the view, not of some other widget where you catch the contextMenuEvent. You have to translate it.
btw: https://doc.qt.io/qt-5/qwidget.html#customContextMenuRequested is the normal way to go here.
-
Fully working minimal example:
#include <QApplication> #include <QStandardItemModel> #include <QListView> #include <QDebug> int main(int argc, char *argv[]) { QApplication app(argc,argv); QStandardItemModel model(5,1); for(int i=0;i<model.rowCount();++i) model.setData(model.index(i,0),i+1); QListView view; view.setModel(&model); view.setContextMenuPolicy(Qt::CustomContextMenu); QObject::connect(&view,&QListView::customContextMenuRequested,[&view](const QPoint &pos){ qDebug() << view.indexAt(pos); }); view.show(); return app.exec(); }
-
Thanks guys, that's looking well-promising now! The "Item" looks like a valid number now. Much appreciate your help!
However, what I'm trying to do next, fails. Would you mind having a look, please?
void MyClass::showContextMenu(const QPoint& pos) { /* Handle global position */ QPoint globalPos = deviceView->mapToGlobal(pos); QModelIndex item = deviceView->indexAt(pos); /* Create menu and insert some actions */ QMenu rightClickMenu; rightClickMenu.addAction("Some Action"); QAction* selectedMenuItem = rightClickMenu.exec(globalPos); // does not work with "pos" either if (!selectedMenuItem) { return; // always end up here! } ...
-
What I'm trying to do is:
- open a QListView in my MainWindow (in fact, I open it in a Dock, that is in MainWindow, which then sets its widget as my QListView)
- display data from the Model that my QListView is using
- right-click on a particular row and select an item which will create another dedicated dock
How can I make sure that the right-click only works for the QListView? Because at the moment, it seems that I can click anywhere and it will show the right-click menu. Doing the below doesn't seem to work:
MyListModel* myModel = new MyListModel(); myView = new QListView; myView->setModel(myModel); /* Custom right-click menu */ myView ->setContextMenuPolicy(Qt::CustomContextMenu); connect(myView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showContextMenu(QPoint)));
As you can see in the connect, signal-to-slot is from myView to "this" because showContextMenu is defined in "this", i.e. my class that instantiates QListView... is that all correct?