Qt: Drop-down button?
-
-
[quote author="Volker" date="1303424157"]A QToolButton with a menu could be a solution.[/quote]
Thank you, Volker. Exactly what I needed.
Maybe it should be added to the Qt Widget gallery since, as a newbie, I totally missed this buried deep in the docs.
Follow-up Question:
How would I go about making an "icon grid" popup menu? (No text, just a bunch of further icons)
Thank you again.
-
[quote author="Franzk" date="1303415395"]Absolutely possible. You should have a tool bar available somewhere. Then you create a menu:
@QMenu *menu = new QMenu(tr("My Menu"));@
You should then add some actions to that menu. Then you want to add the menu to the tool bar:
@theTargetToolBar->addAction(menu->menuAction());@
You probably want to have some sort of icon:
@menu->menuAction()->setIcon(myIcon);@
Make the necessary connections for the actions in your menu, and don't forget to connect the menuAction()'s triggered, toggled or whatever signal to your desired default action.[/quote]So I see that this works too. (At least it renders the same on Windows 7...)
What is the actual difference between Volker's solution and Franzk's solution?
Is there some kind of automatic stuff happening in one vs. the other? Just trying to understand how Qt designed and meant to be used.
Thank you.
-
OK, I understand. Thank you both.
Follow-up question:
How would I create the popup menu such that it contains a bunch of actions rendered as click-able toolbuttons/pushbuttons with icons rather than a standard "menu items"? (More of a pop-up tool bar than pop-up menu, per-se.)
-
You can use QWidgetAction to achieve this, see:
http://doc.qt.nokia.com/4.7/qwidgetaction.html
For example:
@
QMenu *menu = new QMenu("Menu");
QWidgetAction *action = new QWidgetAction(this);
QPushButton *button2 = new QPushButton("Click me", menu);
action->setDefaultWidget(button2);
menu->addAction(action);@ -
[quote author="Andre" date="1304054540"]Perhaps you could subclass QMenu?[/quote]
Andre,
A Qmenu sub-class sounds like the way to go.... given that I'd like to use QActions that are already defined and used elsewhere.
Where do I hook in to the menu & item drawing so as to render the menu's QActions as QToolButtons, rather than traditional menu items?
Thank you
-
Well... I'd say, at paintEvent() and sizeHint(), just like with any other QWidget. However, I never tried to subclass QMenu before, so perhaps there are some unexpected snags to take care of.
What you could try is to layout the actions in the menu as QToolButtons in a QGridLayout (or perhaps a flow-type layout). You should be able to create those as child widgets; QMenu is just a QWidget itself, after all. It might be that you need to reimplement paintEvent() to do nothing, and sizeHint() to return the right size for your button grid.