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. Left align QToolButtons in a vertical QToolBar
QtWS25 Last Chance

Left align QToolButtons in a vertical QToolBar

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 8.4k 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.
  • J Offline
    J Offline
    JulienMaille
    wrote on 21 Sept 2015, 19:27 last edited by JulienMaille
    #1

    I've googled a lot this question and did not find a real answer. By default those buttons are centered and there's no simple way (stylesheet, property) to change that.
    I'm ready to subclass QStyle if that's the way to go but I'm still clueless on where in the code is defined the size and position of a QToolButton

    Basically here is what I want to achieve:
    hey

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 21 Sept 2015, 20:40 last edited by Chris Kawa
      #2

      If you're not gonna change the items much you can "fix" them like this:

      QLayout* lay = toolBar->layout();
      for(int i = 0; i < lay->count(); ++i)
          lay->itemAt(i)->setAlignment(Qt::AlignLeft);
      

      otherwise you'll need to carefully fix any new item added.

      Btw. And why on earth are you developing on a 15 years old OS :P

      1 Reply Last reply
      0
      • J Offline
        J Offline
        JulienMaille
        wrote on 21 Sept 2015, 20:46 last edited by
        #3

        Thanks for your answer.
        That's an interesting option, however it is still quite ugly because the buttons all have a different width (instead of using 100% of the toobar width)

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on 21 Sept 2015, 20:48 last edited by Chris Kawa
          #4

          You can set a fixed size buttons for example with stylesheet:

          QToolBar QToolButton { width: 100px;  } //or switch that to min-width if you prefer
          

          EDIT: Scratch that. You would loose left alignment again this way.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            alex_malyu
            wrote on 22 Sept 2015, 00:37 last edited by alex_malyu
            #5

            Setting minimum size on toolbutton should work.
            I do not see why Chris says it would not,

            I believe icon is (or at least was last time checked) painted aligned to the left margin and text is painted right next to it.
            So specifying minimum size should do the job.

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on 22 Sept 2015, 06:35 last edited by
              #6

              @alex_malyu Icon and text is centered inside tool button, not aligned left (at least on Windows, don't know about other platforms).
              If you set a width then the buttons are placed properly, but the problem shifts from the toolbar to the tool button. Since there doesn't seem to be a way to set alignment of tool button content (or is there?) and you can't provide a custom widget for the toolbar without modifying the code "outside", the only other, non-intrusive to the user code solution I can think of is to set the width as I posted and implement your own style that would paint QStyle::CE_ToolButtonLabel aligned left (as suggested in that other thread).

              1 Reply Last reply
              0
              • A Offline
                A Offline
                alex_malyu
                wrote on 22 Sept 2015, 22:13 last edited by
                #7

                It looks like I've done it in Qt4 for QPushButton
                It did have 'text-align' property you could specify in the Stylesheet.

                I could not find it looking in Qt 5 documentation.
                It seems instead of adding it as common for AbstractButton to all buttons property it was removed.
                Kind of disappointing.

                1 Reply Last reply
                0

                2/7

                21 Sept 2015, 20:40

                topic:navigator.unread, 5
                • Login

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