I do exactly this - QLineEdit in a QActionWidget as a menu item with the action triggered when Enter is pressed.
I do nothing complicated at all - just create a menu class derived from QMenu, which has the QLineEdit and a QActionWidget. Set the lineEdit as the default widget of the QWidgetAction. Connect QLineEdit's "returnPressed" to some slot. In my case, the slot does nothing special (just emits some custom signals).
class MyMenu
: public QMenu{
...
private:
}
class MyMenu : public QMenu
{
...
private:
QLineEdit* lineEdit;
QWidgetAction* widgetAction;
}
To copy to clipboard, switch view to plain text mode
In the MyMenu constructor:
widgetAction->setDefaultWidget(lineEdit);
addAction(widgetAction); // Add the widget action to MyMenu
connect(lineEdit, SIGNAL(returnPressed()),
this, SLOT(DoSomethingWithReturnPress()));
lineEdit = new QLineEdit(this);
widgetAction = new QWidgetAction(this);
widgetAction->setDefaultWidget(lineEdit);
addAction(widgetAction); // Add the widget action to MyMenu
connect(lineEdit, SIGNAL(returnPressed()),
this, SLOT(DoSomethingWithReturnPress()));
To copy to clipboard, switch view to plain text mode
I'm doing something a little more complicated at the moment and I'm struggling to get it working. I have a QMenu that has a QWidgetAction that has a custom QWidget at its default widget. The custom widget comprises a QPushButton and a QSpinBox. I would like the QWidgetAction to be triggered by pressing the button, thereby making the popup context menu disappear. Here's a post that get's me some of the way there.
EDIT: On advice from norobro, I just hide the QMenu manually when the QPushButton::clicked signal is caught.
Bookmarks