防止在QMenu之一触发时关闭QMenu [英] Prevent a QMenu from closing when one of its QAction is triggered

查看:76
本文介绍了防止在QMenu之一触发时关闭QMenu的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用QMenu作为上下文菜单。此菜单中装有QActions。这些QAction之一是可检查的,并且我希望能够在不关闭上下文菜单的情况下对其进行检查/取消选中(并且必须再次将其重新打开以选择所需的选项)。

I'm using a QMenu as context menu. This menu is filled with QActions. One of these QActions is checkable, and I'd like to be able to check/uncheck it without closing the context menu (and having to re-open it again to choose the option that I want).

我尝试断开可检查QAction发出的信号,但是没有运气。

I've tried disconnecting the signals emitted by the checkable QAction with no luck.

有什么想法吗?谢谢。

推荐答案

使用QWidgetAction和QCheckBox进行可检查的动作,不会导致菜单关闭。 p>

Use a QWidgetAction and QCheckBox for a "checkable action" which doesn't cause the menu to close.

QCheckBox *checkBox = new QCheckBox(menu);
QWidgetAction *checkableAction = new QWidgetAction(menu);
checkableAction->setDefaultWidget(checkBox);
menu->addAction(checkableAction);

在某些样式中,这看起来与可检查操作并不完全相同。例如,对于Plastique样式,该复选框需要稍微缩进。

In some styles, this won't appear exactly the same as a checkable action. For example, for the Plastique style, the check box needs to be indented a bit.

这篇关于防止在QMenu之一触发时关闭QMenu的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆