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

QMenu & QAction get sender button/widget?



  • Hey

    I'm in need of having a sender history while clicking on a QAction inside QMenu. Essentially I need to know the parent toolbar or widget that the action was clicked from.

    In case of a simple QActions I just passed a QToolButton to action so I can follow it up the chain, but in case of action inside menu... not sure, what to do here?

    TIA

    U u uuu another one... if I have menu + 4 actions inside... how can I configure so that when action gets pressed, the menu changes to that action and its a toggle menu? I can set button to be toggle & all that so thats fine... but again... I need to know action parent in menu to be able to hmmmmmmmmmmmm can action somehow query its parent ? o.O

    Nope can't get it to work, action is quite "sharable" item thus I don't see a way of getting a parent of action that was just clicked on if it was in a menu :- (


  • Lifetime Qt Champion

    Hi
    Why do you need to know the parent?
    QAction can be shared so the same function can be activated from
    multiple sources. But normally action does the same regardless from where is was triggered.


  • Moderators

    Instead of starting with the action you can connect to the menu's triggered signal. In the slot sender() will give you the menu and you can go up the parents with parentWidget() to find the toolbar if you need.



  • @mrjj said in QMenu & QAction get sender button/widget?:

    Hi
    Why do you need to know the parent?
    QAction can be shared so the same function can be activated from
    multiple sources. But normally action does the same regardless from where is was triggered.

    I need to adjust action command parameters depending on the parent widget.

    I tried action->parentWidget() but it did not return anything I could use .

    Esentially I can have action that does A+B, and if the action is in global widget it would do just that, but if its in local widget then I will need localA + localB... more or less... thus the need to have a way to knowing parent widget while sending command to work.

    @Chris-Kawa said in QMenu & QAction get sender button/widget?:

    Instead of starting with the action you can connect to the menu's triggered signal. In the slot sender() will give you the menu and you can go up the parents with parentWidget() to find the toolbar if you need.

    Hmmmmm that looks very tempting!!! Will give it a go thanks!

    I was about to write my own action system to handle my needs but perhaps with Chris Kawa reply I can utilize native qt ones ! Thanks!

    BRB testing.


    Ok so this gets me half way there. I can now do quite a bit nice stuff with menu & actions, like changing menu default action to do something else based on action clicked and all that - yay !

    But... how do I find out that menu parent - QToolbar or QToolButton? Is there a path down from there?


    And it looks like the toolButton has thesame triggered signal... hmmmm


    yep this is beautifull, it propagates all the way to toolButton where I can do what I want with the action and its data.

    Amazing, whoever designed that system is my hero.


Log in to reply