Please nominate your Qt Champions for 2021!

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

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

    attaching menu to QToolButton

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

  • Lifetime Qt Champion

    @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


Log in to reply