Qt QListView - context menus?

qlistview, qt

Solution

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.

Problem

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?

Original source