Connect trigger() from a QMenu to another function
-
Hi. I'm trying to connect trigger() from a QMenu to another function. This is the code.
MenuBar::MenuBar(QWidget *parent) : QWidget(parent){ resize(800, 20); QMenuBar *menuBar = new QMenuBar(this); qDebug() << menuBar->size(); setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); QMenu *menuSettings = new QMenu("Settings", menuBar); menuBar->addMenu(menuSettings); QMenu *menuExit = new QMenu("Exit", menuSettings); menuSettings->addMenu(menuExit); connect(menuExit, QMenu::triggered(), this, [=](){this->onMenuExitTriggered();}); //THE IMPORTANT BIT } void MenuBar::onMenuExitTriggered(){ qDebug() << "Exit"; }
On the connect line, the IDE is telling me I am calling a non-static member function with an object argument. This makes sense, since according to the QMenu documentation a QAction needs to be provided as the argument. However I am unsure how to represent this argument in syntax.
Some of the code I have tried:
connect(menuExit, QMenu::triggered(QAction::triggered()), this, [=](){this->onMenuExitTriggered();});
(This is calling a non-static member function with an object argument, despite QAction's triggered not requiring an object argument, which is confusing me)
QAction *actionExit = new QAction(menuExit); connect(actionExit, &QAction::triggered, this, [=](){this->onMenuExitTriggered();});
(There are no syntax issues but I am unsure how to make the QAction a child of the QMenu menuExit)
Please let me know if more info is required.
-
Hi. I'm trying to connect trigger() from a QMenu to another function. This is the code.
MenuBar::MenuBar(QWidget *parent) : QWidget(parent){ resize(800, 20); QMenuBar *menuBar = new QMenuBar(this); qDebug() << menuBar->size(); setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); QMenu *menuSettings = new QMenu("Settings", menuBar); menuBar->addMenu(menuSettings); QMenu *menuExit = new QMenu("Exit", menuSettings); menuSettings->addMenu(menuExit); connect(menuExit, QMenu::triggered(), this, [=](){this->onMenuExitTriggered();}); //THE IMPORTANT BIT } void MenuBar::onMenuExitTriggered(){ qDebug() << "Exit"; }
On the connect line, the IDE is telling me I am calling a non-static member function with an object argument. This makes sense, since according to the QMenu documentation a QAction needs to be provided as the argument. However I am unsure how to represent this argument in syntax.
Some of the code I have tried:
connect(menuExit, QMenu::triggered(QAction::triggered()), this, [=](){this->onMenuExitTriggered();});
(This is calling a non-static member function with an object argument, despite QAction's triggered not requiring an object argument, which is confusing me)
QAction *actionExit = new QAction(menuExit); connect(actionExit, &QAction::triggered, this, [=](){this->onMenuExitTriggered();});
(There are no syntax issues but I am unsure how to make the QAction a child of the QMenu menuExit)
Please let me know if more info is required.
@Dummie1138 said in Connect trigger() from a QMenu to another function:
connect(actionExit, &QAction::triggered, this, [=](){this->onMenuExitTriggered();});
This is the only syntactically correct one (the
QMenu::triggered(...)
cannot be right, you will never want the parentheses, this must be the address of a member method like&QAction::triggered
, not a call to that method which is what the(...)
would do).IIRC, QAction *QMenu::menuAction() const returns a
QMenu
's action to use for the connection given aQMenu
you have created (like fromQMenu *menuExit = new QMenu("Exit", menuSettings)
). UPDATE Or QAction *QMenu::addMenu(QMenu *menu) would return theQAction
created frommenuSettings->addMenu(menuExit)
.BTW, the difference between
QAction::triggered
andQMenu::triggered
is that the former can be specified per item/action in a menu with multiple ones whereas the latter fires for any action/item on the menu, and you then have to find out which one if you need to know. -
I have modified the code accordingly based on @JonB.
connect(menuExit->menuAction(), &QAction::hovered, this, [=](){this->onMenuExitTriggered();});
This works. Thank you very much to Jon for this.