Results 1 to 6 of 6

Thread: How to make QMenu.exec() return a QWidgetAction

  1. #1
    Join Date
    Mar 2008
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question How to make QMenu.exec() return a QWidgetAction

    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

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to make QMenu.exec() return a QWidgetAction

    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..

  3. #3
    Join Date
    Mar 2008
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to make QMenu.exec() return a QWidgetAction

    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:

    Qt Code:
    1. self.connect(edit, SIGNAL("returnPressed()"), self, SLOT("trigger()"))
    To copy to clipboard, switch view to plain text mode 

    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...
    Last edited by gregsan; 23rd April 2009 at 09:25.

  4. #4
    Join Date
    Jun 2008
    Posts
    88
    Thanks
    4
    Thanked 4 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11
    Wiki edits
    1

    Default Re: How to make QMenu.exec() return a QWidgetAction

    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

    menu_bug..png

  5. #5
    Join Date
    Jun 2008
    Posts
    88
    Thanks
    4
    Thanked 4 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11
    Wiki edits
    1

    Default Re: How to make QMenu.exec() return a QWidgetAction

    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
    Qt Code:
    1. emit clicked(); // connect this up to the widget action's trigger() method
    2. event->ignore();
    To copy to clipboard, switch view to plain text mode 

    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.

  6. #6
    Join Date
    Feb 2010
    Location
    Sydney, Australia
    Posts
    111
    Thanks
    18
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to make QMenu.exec() return a QWidgetAction

    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).

    Qt Code:
    1. class MyMenu : public QMenu
    2. {
    3. ...
    4. private:
    5. QLineEdit* lineEdit;
    6. QWidgetAction* widgetAction;
    7. }
    To copy to clipboard, switch view to plain text mode 

    In the MyMenu constructor:
    Qt Code:
    1. lineEdit = new QLineEdit(this);
    2. widgetAction = new QWidgetAction(this);
    3.  
    4. widgetAction->setDefaultWidget(lineEdit);
    5. addAction(widgetAction); // Add the widget action to MyMenu
    6.  
    7. connect(lineEdit, SIGNAL(returnPressed()),
    8. 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.
    Last edited by stefanadelbert; 15th April 2010 at 05:14.

Similar Threads

  1. error with QList with a class
    By john_god in forum Newbie
    Replies: 7
    Last Post: 12th January 2009, 21:48
  2. QTableView performances
    By miraks in forum Qt Programming
    Replies: 18
    Last Post: 1st December 2008, 10:25
  3. QSql*Model + QTreeView : make a tree
    By punkypogo in forum Qt Programming
    Replies: 18
    Last Post: 24th October 2008, 18:14
  4. QTableView Repaint/Refresh
    By millsks in forum Newbie
    Replies: 9
    Last Post: 10th January 2008, 17:18
  5. Compiling with Qmake/Make
    By VireX in forum Newbie
    Replies: 25
    Last Post: 22nd February 2007, 05:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.