Qt QListView - 上下文菜单?
我正在尝试向 Qt QListView 添加上下文(右键单击)菜单。我在 Qt 3.3 中看到有“contextMenuRequested”(我可以使用) - http ://doc.qt.digia.com/3.3/qlistview.html#contextMenuRequested。然而,我在Qt4中看不到这样的方法。有谁知道如何向 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
取决于您在
中设置 ContextMenuPolicy 的方式>QWidget
你有几个选择。如果您将其设置为
Qt::DefaultContextMenu
,则只需重写QWidget
中的contextMenuEvent(QContextMenuEvent*)
受保护函数即可。我相信这是默认的。如果您已将其设置为
Qt::ActionsContextMenu
,那么只需将所有操作添加到您的小部件中,然后让 Qt 系统处理显示上下文菜单。或者,如果您已将其设置为
Qt::CustomContextMenu
,则需要连接到customContextMenuRequested()
信号并实现您自己的上下文菜单处理程序。这些都记录在在线提供的 ContextMenuPolicy 文档中。
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 thecontextMenuEvent(QContextMenuEvent*)
protected function inQWidget
. 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 thecustomContextMenuRequested()
signal and implement your own context menu handler.This is all documented in the ContextMenuPolicy documentation available online.
我不知道您想要完成什么,但您可以通过使用要添加到上下文的操作调用
QWidget::AddAction(QAction*)
轻松地将上下文菜单添加到任何小部件菜单并设置上下文菜单策略,小部件将准备并显示上下文菜单,您所需要做的就是将操作
triggered()
信号连接到适当的处理程序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 policythe widget will prepare and show the context menu, all you need to do is hook up the actions
triggered()
signals to the appropriate handlers我不知道为什么信号已被删除,但它仍然是一个
QWidget
,因此您始终可以覆盖或
取决于小部件的上下文菜单策略设置。
I don't know why the signal has been removed but it's still a
QWidget
so you can always overrideor
depending on your context menu policy setting for the widget.