Qt QListView - 上下文菜单?

发布于 2024-09-02 12:28:11 字数 300 浏览 2 评论 0原文

我正在尝试向 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半寸时光 2024-09-09 12:28:11

取决于您在 中设置 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 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.

神回复 2024-09-09 12:28:11

我不知道您想要完成什么,但您可以通过使用要添加到上下文的操作调用 QWidget::AddAction(QAction*) 轻松地将上下文菜单添加到任何小部件菜单并设置上下文菜单策略,

widget->setContextMenuPolicy(Qt::ActionsContextMenu);

小部件将准备并显示上下文菜单,您所需要做的就是将操作 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 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

撕心裂肺的伤痛 2024-09-09 12:28:11

我不知道为什么信号已被删除,但它仍然是一个 QWidget,因此您始终可以覆盖

void QWidget::contextMenuEvent ( QContextMenuEvent* );

void QWidget::customContextMenuRequested( const QPoint& pos );

取决于小部件的上下文菜单策略设置。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文