PDA

View Full Version : How to make QMenu.exec() return a QWidgetAction



gregsan
22nd April 2009, 12:41
Hi,

I'm using QWidgetAction to add a QLineEdit widget into the QMenu and then show the menu using menu.exec() function. When the user types something in the line edit and presses enter I would like to see that exec() would return me the appropriate QWidgetAction instance. I tried calling QWidgetAction.triggered() on that keyboard event but it didn't work.

How do I do that? I don't seem to find out what do I have to call in order for the exec() to return this action...

Any help is greatly appreciated...

Gregor

aamer4yu
22nd April 2009, 12:58
From the documentation of QWidgetAction

Note that it is up to the widget to activate the action, for example by reimplementing mouse event handlers and calling QAction::trigger().

Hence you might have to call trigger when Enter key is pressed on the line edit..

gregsan
23rd April 2009, 08:14
Hi,

thank you for your reply. As I said, I did try calling trigger() but it doesn't work as expected.

I use PyQt so what I tried was:


self.connect(edit, SIGNAL("returnPressed()"), self, SLOT("trigger()"))

which is supposed to connect the returnPressed() event of the line edit instance with the trigger() function of the QWidgetAction instance.

Now, the first time when I press Enter in the line edit, nothing happens. Only when I press it for the second time the QMenu.exec() actually returns and gives me the QWidgetAction. I checked if the returnPressed is sent also the first time, and (of course) it is.

Any ideas why such behavior?

Thanks...

chezifresh
8th April 2010, 18:31
I would absolutely love to hear if you were able to solve this problem.

1. I have the same issue where I can't get the QMenu to exit after I'm done using my custom widget.

2. I also have a problem where the menu doesn't 'un-highlight' other menu actions properly (see image below of a custom widget action in a menu, the widget is a qpushbutton just to illustrate the problem).

If anyone knows how to fix #1 that would be wicked awesome :D

4494

chezifresh
8th April 2010, 19:52
Even looking through the Qt source code yields no interesting info on this. Though it turns out you can get the menu to disappear when click on your custom widget. In the mouseReleaseEvent you basically just need to ignore the event


emit clicked(); // connect this up to the widget action's trigger() method
event->ignore();


The hover problem still eludes me. I've tried ignoring the enterEvent to see if I could get a similar result but that doesnt work either, neither calling the hover() method nor emitting the hovered() signal seems to unhighlight other menu items.

stefanadelbert
15th April 2010, 00:04
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:
QLineEdit* lineEdit;
QWidgetAction* widgetAction;
}

In the MyMenu constructor:


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


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 (http://lists.trolltech.com/qt-interest/2007-06/thread00463-0.html)'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.