tags:

views:

245

answers:

3

Hi,

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.

A: 

Hey,

Here are couple ideas I've had... Not sure at all they will work tho ;)

1) Try to catch the Event by using the QMenu's method aboutToHide(); Maybe you can "Cancel" the hide process ?

2) Maybe you could consider using an EventFilter ?

Try to have a look at : http://qt.nokia.com/doc/4.6/qobject.html#installEventFilter

3) Otherwise you could reimplement QMenu to add your own behavior, but it seems a lot of work to me...

Hope this helps a bit !

Andy M
A: 

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.

baysmith
Thanks a lot.With the plastique style there's indeed a margin to add. So I put the checkbox in a widget with a layout, and set it margins (maybe there's a simpler way...)One last thing: the checkbox doesn't expands to the full width of the menu, so if the click occurs after the end of the box's label the menu is closed and the box is not checked. Setting the size policy has no effect.
gregseth
A: 

I have the same probleme, i tried to do my Qmenu with QWidgetAction and Checkbox, but when I set the defaultWidget of my QWidgetAction the text in the menu isn't show, but the checkbow is show ....

Chapo