PDA

View Full Version : Correct way for QMenu::exec() to return QWidgetAction?



Davor
16th March 2010, 12:24
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:


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()));
}
};

PS Something similar was asked here (http://www.qtcentre.org/threads/20450-How-to-make-QMenu-exec()-return-a-QWidgetAction?highlight=QWidgetAction), 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);
but that shouldn't be the way to go.