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

Associating one menu with two QToolButton



  • I have some menu with some QActions like

    QMenu test = new QMenu;
    QAction action1 = new QAction("test1",this);
    QAction action2 = new QAction("test2",this);

    Now adding QAction to menu
    test->addAction(action1);
    test->addAction(action2);

    Now to QPushButton
    QToolButton *button1 = new QToolButton(this);
    QToolButton *button2 = new QToolButton(this);

    attaching menu to QToolButton
    button1->setMenu(test);
    button2->setMenu(test);

    Now I need to connect the signal to QAction test1,test2.

    And do something according to the type of QPushButton. But how can I get to know the QPushButton type when I clicked on QAction since common menu is associated with two QToolButton.

    Is this possible?



  • Hey,

    you can check the Signals form QAction here: QAction - Signals.
    Why you use QAction and not QPushButton?
    QPushButton have a signal "clicked" ...


  • Qt Champions 2019

    @ayush-gupta said in Associating one menu with two QToolButton:

    But how can I get to know the QPushButton type when I clicked on QAction

    I don't know what you mean with "type", but you can connect a lambda to the clicked() signal from the buttons and pass additional parameters to the lambda to differentiate the buttons.



  • You can call sender() inside a slot to determine calling object and just compare, which button caused the slot to execute.


  • Moderators

    @umod-47 you can, but you shouldn't, let me quote the documentation

    0_1568705894317_13d69ae2-2a35-4555-adc3-be1bb907ed24-image.png


Log in to reply