Important: Please read the Qt Code of Conduct -

What is the class of the children of a QMenu?

  • I'am trying to install an event filter on all existing QObject.
    Then I toggle the enabled property when shift+clicking a graphical element. I am also printing out the clicked element's class.

    It works for most of the QWidgets but when I click on a QAction of a QMenu, the whole menu is disabled, and my output is "QMenu" when I would expect a QToolButton, or a QAction.

    Here is my code:
    @bool MainWindow::init()
    QList<QObject*> objects = this->findChildren<QObject*>();
    for (int i = 0; i < objects.size(); ++i)>installEventFilter(this);

    bool MainWindow::eventFilter(QObject *obj, QEvent event)
    if (event->type() == QEvent::MouseButtonPress && ( ((QMouseEvent
    )event)->modifiers() & Qt::ShiftModifier ) )
    obj->setProperty("enabled", !obj->property("enabled").toBool());
    return true;
    // standard event processing
    return QObject::eventFilter(obj, event);

  • If you click on the entry that represents a sub menu, that entry is associated with a QAction and/or a QMenu. If you disable that one all its submenu entries are disabled too resp. you cannot open that submenu. Which looks quite sensible to me, that's what I would expect here.

  • Thanks for your answer Volker!
    Maybe I was not clear.!!
    If I shift+click "About" the whole Help menu is disabled.

  • 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": 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?
    :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.

Log in to reply