Qt QListView - context menus?
I'm trying to add a context (right click) menu to a Qt QListView. 开发者_Python百科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?
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.
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
I don't know why the signal has been removed but it's still a QWidget
so you can always override
void QWidget::contextMenuEvent ( QContextMenuEvent* );
or
void QWidget::customContextMenuRequested( const QPoint& pos );
depending on your context menu policy setting for the widget.
精彩评论