Qt: Drop-down button?



  • Hello,

    How can I create a "drop down button" in Qt?

    For a non-Qt example, see:
    !http://i53.tinypic.com/mhx6kj.jpg(AutoSum button in MS Excel)!

    In case that image of the AutoSum button in MS Excel did not come through, I need a widget that:

    1. Has a clickable icon button for the "primary action"
    2. Has a separate clickable "pulldown arrow" to show "secondary actions".
    3. The secondary actions are displayed in the form of a popup menu.

    Does Qt have a widget that can do this? In my case I only need icons. No text.

    If not, how can this be created in Qt? (I'm a new Qt user, so a Qt Designer based solution would be ideal.)

    Are there any good open-source 3rd party Qt widget libraries I should know about?

    Thank you



  • 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 author="Franzk" date="1303415395"]Absolutely possible. You should have a tool bar available somewhere. Then you create a menu:
    ...
    [/quote]

    Not sure I understand your answer. I've updated my original post to help clarify my question.

    Thank you :)



  • A QToolButton with a menu could be a solution.



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



  • When going for my solution, you let Qt handle the QToolButton part. Matter of taste probably.



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



  • Perhaps you could subclass QMenu?



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.