Adjust QPushButton size when I change the text
-
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 -
-
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.
-
-
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. -
Hi,
Can you show how you setup your button ?
-
What are you using to handle your pile of widgets ?
-
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
-
QStackedLayout is usually used when you want to have a pile of widget and only show one at a time.