Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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);
        }
    }
    

  • Lifetime Qt Champion

    Hi @lano1106,

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

    Regards



  • 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.


  • Lifetime Qt Champion

    @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



  • 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?


  • Lifetime Qt Champion

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



  • 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...


  • Lifetime Qt Champion

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



  • 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...



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



  • 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?


  • Lifetime Qt Champion

    @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.



  • 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.


Log in to reply