Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Adjust QPushButton size when I change the text

    General and Desktop
    5
    12
    3417
    Loading More Posts
    • 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.
    • Perdrix
      Perdrix last edited by

      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 Reply Quote 0
      • CybeX
        CybeX last edited by

        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 Reply Quote 2
        • Perdrix
          Perdrix last edited by

          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 Reply Quote 0
          • Christian Ehrlicher
            Christian Ehrlicher Lifetime Qt Champion last edited by

            See QWidget::sizeHint()

            Qt has to stay free or it will die.

            1 Reply Last reply Reply Quote 0
            • B
              Bonnie last edited by

              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 Reply Quote 0
              • Perdrix
                Perdrix last edited by Perdrix

                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 Reply Quote 0
                • SGaist
                  SGaist Lifetime Qt Champion last edited by

                  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 Reply Quote 0
                  • Perdrix
                    Perdrix last edited by Perdrix

                    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 Reply Quote 0
                    • SGaist
                      SGaist Lifetime Qt Champion last edited by

                      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 Reply Quote 0
                      • Perdrix
                        Perdrix last edited by

                        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 Reply Quote 0
                        • SGaist
                          SGaist Lifetime Qt Champion last edited by

                          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 Reply Quote 1
                          • Perdrix
                            Perdrix last edited by

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

                            1 Reply Last reply Reply Quote 0
                            • First post
                              Last post