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. Why is Qt makes it hard to activate a QAction with menu() from QMenu?
Forum Updated to NodeBB v4.3 + New Features

Why is Qt makes it hard to activate a QAction with menu() from QMenu?

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 5 Posters 1.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.
  • L lano1106

    Is it that outlandish to want to have something like:
    checkable QAction with a menu
    ?

    Qt draws the checkboxes but there is no way to toggle them. The culprit is located in QMenu::mouseReleaseEvent() that insist on the action not having a menu to activate it.

    The ugly workaround to make it work is to create my own derived QMenu class and do the following:

    void ClickableMenu::mouseReleaseEvent(QMouseEvent *e)
    {
        QAction *action = actionAt(e->pos());
        QMenu *oldMenu = NULL;
        if (action) {
            if (action->menu()) {
                oldMenu = action->menu();
                action->setMenu(NULL);
            }
        }
        QMenu::mouseReleaseEvent(e);
        if (oldMenu) {
            action->setMenu(oldMenu);
        }
    }
    
    aha_1980A Offline
    aha_1980A Offline
    aha_1980
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi @lano1106,

    So are you saying QAction::setChecked() does not work for you?

    Regards

    Qt has to stay free or it will die.

    1 Reply Last reply
    1
    • L Offline
      L Offline
      lano1106
      wrote on last edited by lano1106
      #3

      no

      I am saying that there doesn't seem to be any way for a user to interact with those checkboxes. AFAIK, the way QMenu is coded, it is impossible from the keyboard and the mouse...

      My ClickableMenu class hack let the user change the check state with the mouse.

      aha_1980A 1 Reply Last reply
      0
      • L lano1106

        no

        I am saying that there doesn't seem to be any way for a user to interact with those checkboxes. AFAIK, the way QMenu is coded, it is impossible from the keyboard and the mouse...

        My ClickableMenu class hack let the user change the check state with the mouse.

        aha_1980A Offline
        aha_1980A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on last edited by
        #4

        @lano1106

        Well, with the menus "Order Num" and "Order MA" it should work.

        For the Top Level menus (menu items that open a submenu) I honestly have never seen a program that uses checkmarks. I'm not even sure if the interface guidelines allow such constructs.

        Regards

        Qt has to stay free or it will die.

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lano1106
          wrote on last edited by
          #5

          Hi @aha_1980 ,

          Yes, it does perfectly work for the leaf items.
          I'm talking specifically about the items below 'Charts'.
          Those that are checkable and have a submenu.

          You are right. I haven't seen a lot of programs doing that sort of thing but why not?
          Why should it not be possible?
          With my hack, I have been experimenting with the system and it feels right.
          What would be the alternative?
          Having a second top-level menu called 'View' repeating all the same items except without a submenu?

          1 Reply Last reply
          0
          • Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #6

            A QMenu can not be checkable, the style just doesn't ignore the checkmark here.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            2
            • L Offline
              L Offline
              lano1106
              wrote on last edited by
              #7

              Thx Christian!

              In that case, I'll leave my hack as-is and hope that it keeps working for some time.

              Do you know where this rule comes from?
              Is it a design choice from Qt devs or maybe some platforms are simply not capable of doing it?
              I'm asking because it is technically possible to do it as my workaround demonstrates...

              1 Reply Last reply
              0
              • Christian EhrlicherC Online
                Christian EhrlicherC Online
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #8

                If a QMenu would be checkable, then it would have a function to create/toggle this.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  lano1106
                  wrote on last edited by
                  #9

                  ok... Interesting...

                  Then in which situation QMenu::menuAction() is intended to be used in?
                  I'm curious about that because this with that that I created those checkable submenus...

                  JonBJ 1 Reply Last reply
                  0
                  • L lano1106

                    ok... Interesting...

                    Then in which situation QMenu::menuAction() is intended to be used in?
                    I'm curious about that because this with that that I created those checkable submenus...

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #10

                    @lano1106
                    I think your situation is "unusual". Can you think of/show an example from some application which has checkable submenus like yours?

                    1 Reply Last reply
                    1
                    • L Offline
                      L Offline
                      lano1106
                      wrote on last edited by lano1106
                      #11

                      I'm just trying to understand the reason behind the limitation.
                      How can we have GUI innovations if it is restricted by only what has been done before?

                      1 Reply Last reply
                      0
                      • Christian EhrlicherC Online
                        Christian EhrlicherC Online
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        @lano1106 said in Why is Qt makes it hard to activate a QAction with menu() from QMenu?:

                        I'm just trying to understand the reason behind the limitation

                        Until now nobody complained about it and / or volunteered the needed code changes.

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        1 Reply Last reply
                        4
                        • Q Offline
                          Q Offline
                          QuinlanJm
                          wrote on last edited by
                          #13

                          excellent I was literally just looking on how to use these checkboxes to promote a subsection as favorites to be placed at the top of the root menu, I was kind of curious why the actions support it, the menus render it, but there is no ability to click it without causing the trigger of the action.

                          Thank you for already going through the pain of solving it.

                          also this is most definitely a bug in Qt, if the actions support it the menus should allow the interaction with the action correctly.

                          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