Qt QListView - context menus?

QLatvia picture QLatvia · May 19, 2010 · Viewed 11.2k times · Source

I'm trying to add a context (right click) menu to a Qt QListView. I see in Qt 3.3 there is "contextMenuRequested" (which I could use) - http://doc.qt.digia.com/3.3/qlistview.html#contextMenuRequested. However, I can't see such a method in Qt4. Does anyone know how to add a context menu to a QListView?

Answer

Daemin picture Daemin · Oct 6, 2011

Depending on how you have set up the ContextMenuPolicy in the QWidget you have a few options.

If you've set it to Qt::DefaultContextMenu then just override the contextMenuEvent(QContextMenuEvent*) protected function in QWidget. I believe that this is the default.

If you've set it to Qt::ActionsContextMenu then just add all your actions to your widget and let the Qt system handle showing the context menu.

Or if you've set it to Qt::CustomContextMenu you need to connect to the customContextMenuRequested() signal and implement your own context menu handler.

This is all documented in the ContextMenuPolicy documentation available online.