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. Adjust QPushButton size when I change the text
QtWS25 Last Chance

Adjust QPushButton size when I change the text

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 5 Posters 5.6k 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.
  • PerdrixP Offline
    PerdrixP Offline
    Perdrix
    wrote on last edited by
    #1

    I have a QPushButton whose text I change using setText(). Unfortunately if the text is short the button is over-sized, and if the text is too long, the text is clipped at both ends.

    As the push button in question is flat, if I could make the text left-aligned in the button, I could make it large enough for the largest text, but I can find no way to left align the button text.

    So:

    • Did I miss the way to left align text in a QPushButton? Or,

    • How can I adjust the size of the button to the minimum required to contain the text? I don't think this is related to Horizontal Size Policy, but could be completely wrong.

    Please provide as much detail as possible - I'm still very much a Qt beginner!

    Thanks
    David

    1 Reply Last reply
    0
    • CybeXC Offline
      CybeXC Offline
      CybeX
      wrote on last edited by
      #2

      Maybe this could help you:

      https://www.qtcentre.org/threads/2750-QPushbutton-to-fit-it-s-text

      and they recommend using adjustSize() (as an inherited function from QWidget).

      Hope it helps.

      1 Reply Last reply
      2
      • PerdrixP Offline
        PerdrixP Offline
        Perdrix
        wrote on last edited by
        #3

        That helps if I know the size it needs to be, but given that I don't know a priori the size needed for the several different text contents and their translations, how do I found out how big it needs to be based on the textual content.

        David

        1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          See QWidget::sizeHint()

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • B Offline
            B Offline
            Bonnie
            wrote on last edited by
            #5

            You can set the align by style sheet:

            text-align:left
            

            Just an additional note, it is related to the horizontal size policy when the button is added to a layout.
            In that case if you set the horizontal size policy to Fixed / Maximum, it's width will be the minimum, but still with some margin.

            1 Reply Last reply
            0
            • PerdrixP Offline
              PerdrixP Offline
              Perdrix
              wrote on last edited by Perdrix
              #6

              I used a style-sheet text-align:left which has worked for this case. I'm not marking it as the correct answer as I still wish to know how to size the button to the contained text.

              David

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

                Hi,

                Can you show how you setup your button ?

                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
                • PerdrixP Offline
                  PerdrixP Offline
                  Perdrix
                  wrote on last edited by Perdrix
                  #8

                  67bd44ac-28dd-4f84-ae77-cb9c6a2550c5-image.png

                  0c4997a9-5ede-40c7-92ae-924e1ec33c36-image.png

                  Sorry it's a but messy but there's a bunch of controls there all on top of one-another at the same location but only one is ever shown

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

                    What are you using to handle your pile of widgets ?

                    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
                    • PerdrixP Offline
                      PerdrixP Offline
                      Perdrix
                      wrote on last edited by
                      #10

                      In what sense? – the decision to show/hide them is based upon the conditions that apply when the dlg is shown. The ctor sets them all invisible and they are made visible as conditions determine (in this case depending on the value on one member variable).

                      If you're referring to layout - the GroupBox has "Broken layout" which is how I can pile 'em high in one place.

                      David

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

                        QStackedLayout is usually used when you want to have a pile of widget and only show one at a time.

                        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
                        1
                        • PerdrixP Offline
                          PerdrixP Offline
                          Perdrix
                          wrote on last edited by
                          #12

                          Thanks for that pointer to StackedLayout. It encapsulates what I'm actually doing. ...

                          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