I have a QPushButton
in a QWidgetAction
in a QMenu
. When the button is clicked, I want the action to trigger and the menu to close, returning which action was triggered. According to the docs, the widget itself must trigger the action directly.
Here's my code:
QMenu *menu = new QMenu();
QWidgetAction *widgetAction = new QWidgetAction(menu);
QPushButton *button = new QPushButton("Finish");
widgetAction->setDefaultWidget(button);
menu->addAction(widgetAction);
connect(button, SIGNAL(clicked()), widgetAction, SLOT(trigger()));
connect(widgetAction, SIGNAL(triggered()), menu, SLOT(close())); //Menu won't close without this
QAction* selectedAction = menu->exec(mapToGlobal(ui->pushButton->pos()));
if(selectedAction != NULL)
{
qDebug() << "no output from here";
}
However selectedAction
always returns NULL
. Regular QAction
's added to the menu automatically close the menu and return pointers to themselves. Why doesn't QWidgetAction
?
Thanks for your time!