What is the class of the children of a QMenu?
-
I'm still struggling with this. Let's formulate the problem differently:
I want to change the stylesheet of all the widgets associated with a specific QAction.
I can do this for actions added to a toolbar (in that case the corresponding widget is a QToolButton) but how do I access the widget that represent the action in a QMenu?QAction::associatedWidgets() does not help, it returns the QMenu not the menu item.
-
QMenu is one widget. Take a look at its code to see how it is rendered. It does not consist of QToolButtons or QActions. Note that QAction is not a widget at all. So, I guess what you are after is not possible, at least not without subclassing QMenu.
-
Thanks for your help Andre. I agree that QActions are not QWidgets. However there is something I don't understand:
- when you add a QAction to a QToolBar, you "get" a QToolButton
- when you add a QAction to a QMenu, do you "get" any widget?
If QMenu is one widget, maybe it has child widgets?
If it has no child, then I guess it is impossible to style its items? -
No, you do not get a widget for an action on a QMenu. However, you can style items using style sheets. Items support a whole array of pseudo states you can use. If you want to keep only a specified action enabled, you should also keep the container it is in enabled.
-
I tried to "style a specific item":http://doc.trolltech.com/latest/stylesheet-syntax.html#selector-types of a QMenu using selector, dynamic properties and object name.
None of these methods worked. -
You'll need to show more than just "It doesn't work" in order for us to be able to help you.
-
Ok, I set this stylesheet to my QMenu containing (among others) a QAction whose object name is "actionExit" and whose text is "Exit". I also added a dynamic property to the QAction: a boolean called "styleMe" set to true.
@*#actionExit { background-color: blue; }
*[text="Exit"] { background-color: blue; }
*[styleMe="true"] { background-color: blue; }@
I also tried with QWidget or QMenu::item instead of * -
No, that won't work. Now you are trying to style the items in your menu as if they are widgets. Again, they are not. You can only use the QMenu::item subcontrol and the associated pseudo states.
-
By pseudo states you mean this list?
@:checked
:disabled
:enabled
:focus
:hover
:indeterminate
:off
:on
:pressed
:unchecked @
Is this is the only way to set specific styling? -
Unless you want to reimplement QMenu itself, and do the rendering yourself: yes, it is.