Qt Menu with QLinEdit action
I want to have a line edit field in a popup menu I've got. I'm basically
letting the user pick from one of several common sizes for something, but
I want them to be able to enter a custom size as the last entry in the
menu.
So I've got something like this (snipped from larger code, new_menu is the
menu of interest):
QWidget *widget = new QWidget(new_menu);
QHBoxLayout *layout = new QHBoxLayout;
QLineEdit* le = new QLineEdit;
le->setPlaceholderText("Custom");
le->setFixedWidth(100);
ayout->addWidget(le);
widget->setLayout(layout);
QWidgetAction* wa = new QWidgetAction(new_menu);
wa->setActionGroup(group);
wa->setDefaultWidget(widget);
new_menu->addAction(wa);
connect(le, SIGNAL(returnPressed()), this, SLOT(leslot()));
Which works great, the LineEdit shows up nice and centered in the menu,
it's got the placeholder text, I can click it and edit, everything.
however, when I hit enter on the text box, it emits the returnPressed
signal and the menu emits a "triggered" signal with one of the other
actions on the list, so at best I'm changing my configuration twice and at
worst things break.
Additionally, when I click off the edge of the LineEdit (still in the menu
though, but not in the editable area), the menu emits a triggered signal
with the QWidgetAction associated with it, which isn't what I want.
So two questions:
1) Can I get the return to work the way I want. It's fine if the menu
closes when it's hit, but it can't emit another action too.
2) Can I get it to not emit an action at all when the lineEdit is clicked?
No comments:
Post a Comment