What I need is a QMenu that displays a button, and when I press the button,
it triggers an action and goes away. I have managed all except the last
part: the QWidgetAction doesn't disappear (more specifically: the program blocks
execution on QMenu::exec(), no matter if QWidgetAction::trigger() is executed or not.)
What is the correct (!) way to let QWidgetAction behave like a simple
QAction in QMenu (i.e. to remove the widget after triggering the action and let QMenu::exec() return the QWidgetAction.)?
Take the the example below as a starting point:
public:
setDefaultWidget(b);
connect(b, SIGNAL(clicked()),
this, SLOT(trigger())); //Docs suggest executing this
connect(b, SIGNAL(clicked()),
this, SIGNAL(triggered()));
}
};
class A : public QWidgetAction {
public:
A(QObject *parent = 0) : QWidgetAction(parent){
QPushButton *b = new QPushButton("BUTTON");
setDefaultWidget(b);
connect(b, SIGNAL(clicked()),
this, SLOT(trigger())); //Docs suggest executing this
connect(b, SIGNAL(clicked()),
this, SIGNAL(triggered()));
}
};
To copy to clipboard, switch view to plain text mode
PS Something similar was asked here, but is left unresolved. For the guy QMenu::exec() returns the QWidgetAction on second QWidgetAction::trigger() execution, but for the above code it does not, no matter how many signals are sent.
PPS Currently I have a workaround using
QKeyEvent *e = new QKeyEvent(QEvent::KeyPress, Qt::Key_Escape, Qt::NoModifier);
QCoreApplication::postEvent(this->parent(), e);
To copy to clipboard, switch view to plain text mode
but that shouldn't be the way to go.
Bookmarks