Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. What is the class of the children of a QMenu?
Forum Updated to NodeBB v4.3 + New Features

What is the class of the children of a QMenu?

Scheduled Pinned Locked Moved General and Desktop
13 Posts 3 Posters 6.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JulienMaille
    wrote on last edited by
    #1

    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)
    objects.at(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());
    qDebug(obj->objectName().toLatin1().data());
    qDebug(obj->metaObject()->className());
    return true;
    }
    else
    {
    // standard event processing
    return QObject::eventFilter(obj, event);
    }
    }@

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      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.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • J Offline
        J Offline
        JulienMaille
        wrote on last edited by
        #3

        Thanks for your answer Volker!
        Maybe I was not clear.!http://pix.wefrag.com/i/7/7/a/1/8/448248ff5d702f9bbb4bf57907143104.png(menu)!
        If I shift+click "About" the whole Help menu is disabled.

        1 Reply Last reply
        0
        • J Offline
          J Offline
          JulienMaille
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • J Offline
              J Offline
              JulienMaille
              wrote on last edited by
              #6

              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?

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  JulienMaille
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #9

                    You'll need to show more than just "It doesn't work" in order for us to be able to help you.

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      JulienMaille
                      wrote on last edited by
                      #10

                      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 *

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        andre
                        wrote on last edited by
                        #11

                        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.

                        1 Reply Last reply
                        0
                        • J Offline
                          J Offline
                          JulienMaille
                          wrote on last edited by
                          #12

                          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?

                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            andre
                            wrote on last edited by
                            #13

                            Unless you want to reimplement QMenu itself, and do the rendering yourself: yes, it is.

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved