Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent a QMenu from closing when one of its QAction is triggered

Tags:

c++

qt

qt4

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).

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

Any ideas? Thanks.

like image 885
gregseth Avatar asked Jan 12 '10 16:01

gregseth


1 Answers

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);

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.

like image 95
baysmith Avatar answered Sep 18 '22 08:09

baysmith