Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt: Drop-down button?
QtWS25 Last Chance

Qt: Drop-down button?

Scheduled Pinned Locked Moved General and Desktop
12 Posts 5 Posters 47.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nonot1
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • F Offline
      F Offline
      Franzk
      wrote on last edited by
      #2

      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.

      "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      1
      • N Offline
        N Offline
        nonot1
        wrote on last edited by
        #3

        [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 :)

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          A QToolButton with a menu could be a solution.

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • N Offline
            N Offline
            nonot1
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • N Offline
              N Offline
              nonot1
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • F Offline
                F Offline
                Franzk
                wrote on last edited by
                #7

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

                "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  nonot1
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #9

                    Perhaps you could subclass QMenu?

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      sigrid
                      wrote on last edited by
                      #10

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

                      1 Reply Last reply
                      0
                      • N Offline
                        N Offline
                        nonot1
                        wrote on last edited by
                        #11

                        [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

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          andre
                          wrote on last edited by
                          #12

                          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.

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved