Results 1 to 4 of 4

Thread: QMenu with a additional QPushButton

  1. #1
    Join Date
    May 2011
    Posts
    27
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default QMenu with a additional QPushButton

    Hello,
    I also asked this question in another forum, but I haven't got a satisfying answer. I have a simple QMenu with several items. For a few of them I want to add an additional QPushButton to the right side to open an options dialog.

    Like in this picture (from Silo 2.2 which also uses Qt):


    How can I achieve this?

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: QMenu with a additional QPushButton

    I would subclass QMenu, and in the paintEvent of the customised class try to figure out the right place for the buttons and display them.

  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QMenu with a additional QPushButton

    You can use QWidgetAction class and subclass it it add you own widget to you menu actions.

    Like this, implement a custom action PushButtonAction
    Qt Code:
    1. class PushButtonAction : public QWidgetAction
    2. {
    3. public:
    4. explicit PushButtonAction(const QString& action_text, QObject* parent = 0);
    5. virtual ~PushButtonAction();
    6.  
    7. private:
    8. QString text;
    9. };
    10.  
    11. PushButtonAction::PushButtonAction(const QString& action_text, QObject* parent)
    12. : QWidgetAction(parent)
    13. , text(action_text)
    14. {
    15. QWidget* widget = new QWidget();
    16. QHBoxLayout* layout = new QHBoxLayout();
    17.  
    18. layout->addWidget(new QLabel(text));
    19. layout->addWidget(new QPushButton("Open Dialog"));
    20. widget->setLayout(layout);
    21.  
    22. setDefaultWidget(widget);
    23. }
    To copy to clipboard, switch view to plain text mode 

    and while creating menu

    Qt Code:
    1. menu->addAction(tr("Action&1"));
    2. menu->addAction(tr("Action&2"));
    3. menu->addSeparator();
    4. menu->addAction(new PushButtonAction("My Action"));
    5. menu->addAction(tr("Action&3"));
    6. menu->addSeparator();
    7. menu->addAction(tr("Action&4"));
    8. menu->addAction(tr("Action&5"));
    To copy to clipboard, switch view to plain text mode 


    There is a problem with this method, you will have to sacrifice the QAction default features, like checkable, icon, selection highlight, mouse hove, etc.. all these have to be reimplemented in your custom widget again. If you plan to use all custom actions in your menu, then it not a bad idea to implement those.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QMenu with a additional QPushButton

    I wouldn't implement something like that as a QMenu. More likely as a widget, be it a custom one based on QAction (to be compliant with QMenu approach) or simply a QTreeWidget/QTableWidget. Menus have a well defined and well known behaviour and trying to change that behaviour hits the user experience and intuition of your application.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 0
    Last Post: 22nd February 2010, 10:30
  2. Defining position for QMenu used by QPushButton
    By Ghaleon in forum Qt Programming
    Replies: 1
    Last Post: 13th February 2009, 10:23
  3. QPushButton QMenu QAction
    By hgedek in forum Newbie
    Replies: 2
    Last Post: 1st November 2007, 13:29
  4. Additional classes and widgets for Qt.
    By ufo-vl in forum Qt Programming
    Replies: 4
    Last Post: 22nd July 2007, 14:51
  5. Replies: 3
    Last Post: 26th September 2006, 13:16

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.