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. [solved]How to center toolbuttons in horizontal toolbar?

[solved]How to center toolbuttons in horizontal toolbar?

Scheduled Pinned Locked Moved General and Desktop
16 Posts 2 Posters 7.3k Views 1 Watching
  • 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.
  • A Offline
    A Offline
    aze473
    wrote on last edited by
    #1

    I have a horizontal toolbar and im trying to get the buttons to center but i cant get it right.
    I am also very new to QT and programming.

    This is what i have now its aligned to the left.

    @void BitcoinGUI::createToolBars()
    {
    QToolBar *toolbar = addToolBar(tr("Tabs toolbar"));
    toolbar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    toolbar->addAction(overviewAction);
    toolbar->addAction(sendCoinsAction);
    toolbar->addAction(receiveCoinsAction);
    toolbar->addAction(historyAction);
    toolbar->addAction(addressBookAction);
    @

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      What do you mean by center ? On the tool bar itself ? In the widget ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • A Offline
        A Offline
        aze473
        wrote on last edited by
        #3

        [quote author="SGaist" date="1400531931"]Hi and welcome to devnet,

        What do you mean by center ? On the tool bar itself ? In the widget ?[/quote]

        Hi! Thank you!, Yes on the toolbar itself i have toolbuttons, but they are aligned to the left now, i would like to have them in the center of the toolbar. Its a horizontal toolbar/menu.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          IIRC there's no such thing out of the box. However you can set two "spacer" QWidgets, one on the left and one on the right side of your actions.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • A Offline
            A Offline
            aze473
            wrote on last edited by
            #5

            [quote author="SGaist" date="1400537649"]IIRC there's no such thing out of the box. However you can set two "spacer" QWidgets, one on the left and one on the right side of your actions.[/quote]

            I tried that and set them to maximize, but somehow the one on the left maxed and pushed everything to the right. And the right one did nothing?
            I am sorry for asking such noob questions.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              aze473
              wrote on last edited by
              #6

              i used
              @ QWidget *spacerWidget = new QWidget(this);
              toolbar->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Preferred);
              spacerWidget->setVisible(true);@

              and before and after the actions:
              @toolbar->addWidget(spacerWidget);@

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                You should set the policy on the widgets, not the toolbar

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  aze473
                  wrote on last edited by
                  #8

                  oh wow, so i just change it into spacerWidget->SetSizePolicy ?
                  Im gonna try this right away.

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

                    [quote author="SGaist" date="1400570992"]You should set the policy on the widgets, not the toolbar[/quote]
                    i tried this but it didnt work..am i still doing something wrong?

                    @void BitcoinGUI::createToolBars()
                    {
                    QToolBar *toolbar = addToolBar(tr("Tabs toolbar"));
                    QWidget *spacerWidget = new QWidget(this);
                    spacerWidget>setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Preferred);
                    spacerWidget->setVisible(true);
                    toolbar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
                    toolbar->addWidget(spacerWidget);
                    toolbar->addAction(overviewAction);
                    toolbar->addAction(sendCoinsAction);
                    toolbar->addAction(receiveCoinsAction);
                    toolbar->addAction(historyAction);
                    toolbar->addAction(addressBookAction);
                    toolbar->addWidget(spacerWidget);
                    @

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      You need two spacer widgets, otherwise you are just moving it from left to right

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        aze473
                        wrote on last edited by
                        #11

                        [quote author="SGaist" date="1400707308"]You need two spacer widgets, otherwise you are just moving it from left to right[/quote]

                        So i cannot use toolbar->addWidget(spacerWidget);
                        twice?
                        I have to make like a spacerwidget2?

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          No you can't, addWidget doesn't mean "copy that widget in the toolbar" (you can't copy widgets by the way nor any QObject derived class)

                          So yes you have to create another spacer widget

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            aze473
                            wrote on last edited by
                            #13

                            Thank you so much for all of your time and help! I really appreciate it.

                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              You're welcome !

                              Did you got it working ?

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              0
                              • A Offline
                                A Offline
                                aze473
                                wrote on last edited by
                                #15

                                [quote author="SGaist" date="1400775959"]You're welcome !

                                Did you got it working ?[/quote]
                                Yes it works perfectly

                                1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  Nice ! Then please update the thread title prepending [solved] so other forum users may know a solution has been found :)

                                  Interested in AI ? www.idiap.ch
                                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  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