Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt QListView - context menus?

Tags:

qt

qlistview

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?

like image 427
QLatvia Avatar asked May 19 '10 21:05

QLatvia


2 Answers

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.

like image 149
Daemin Avatar answered Sep 23 '22 09:09

Daemin


I don't know what you are trying to accomplish but you can easily add a context menu to any widget by calling QWidget::AddAction(QAction*) with the actions that you want to add to your context menu and setting the context menu policy

widget->setContextMenuPolicy(Qt::ActionsContextMenu);

the widget will prepare and show the context menu, all you need to do is hook up the actions triggered() signals to the appropriate handlers

like image 24
Harald Scheirich Avatar answered Sep 19 '22 09:09

Harald Scheirich