如何禁用QDockWidget标题栏的上下文菜单 [英] How to disable the context menu of a QDockWidget title bar
问题描述
我有几个QDockWidgets都不是closabale(使用Qt 5.6).因此,右键单击其中一个的标题栏时显示的上下文菜单仅具有禁用的条目,而我想禁用整个上下文菜单.
I have a couple of QDockWidgets that are all not closabale (using Qt 5.6). Therefore, the context menu that is displayed when right-clicking a title bar of one of them only has disabled entries, and I would like to disable the whole context menu.
我试图将contextMenuPolicy设置为NoContextMenu,但没有成功.
I tried to set the contextMenuPolicy to NoContextMenu without success.
然后,我尝试使用QDockWidget的子类,重写ContextMenuEvent并忽略它.菜单仍然显示.
I then tried to use a subclass of QDockWidget, override the ContextMenuEvent and ignore it. The menu is still displayed.
然后我尝试安装一个事件过滤器以捕获ContextMenuEvent,但它没有捕获任何事件,仅是PaintEvents,ResizeEvents等.
I then tried to install an event filter to catch the ContextMenuEvent, but it did not catch any, just PaintEvents, ResizeEvents etc.
我没主意了……任何帮助将不胜感激!
I'm out of ideas … any help would be greatly appreciated!
推荐答案
根据注释,有必要将QDockWidget
上的上下文菜单策略设置为Qt::PreventContextMenu
...
As per the comments it is necessary to set the context menu policy on the QDockWidget
to Qt::PreventContextMenu
...
dock_widget->setContextMenuPolicy(Qt::PreventContextMenu);
而不是简单地Qt::NoContextMenu
.从文档 Qt::NoContextMenu
只需将上下文菜单处理推迟到父窗口小部件,而不是完全阻止它.
rather than simply Qt::NoContextMenu
. From the documentation Qt::NoContextMenu
simply defers the context menu handling to the parent widget rather than preventing it entirely.
这篇关于如何禁用QDockWidget标题栏的上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!