PDA

View Full Version : Autosize QLabel Font in a QFrame (in a vertical layout)



davethomaspilot
29th June 2018, 23:20
I have a QMainWindow that has a vertical layout that's laid out in a grid.

The vertical layout contains a QFrame. The QFrame has a QLabel. The widget hierarchy can be seen in the attached screenshot.

Everything resizes as I'd expect when the QMainWindow size is changed. The QFrame and QLabel sizes change, but the text size in the QLabel remains fixed.

I want to adjust the QLabel font to make the text as big as possible without the text being clipped.

I thought I could use the label's fontMetrics and adjust the font size until the fontMetrics height was just less than the containing QFrames height. But, I've found I can adjust the font size so that the text isn't clipped, yet the FontMetrics height is BIGGER than the containing QFrame.

So, I don't know what to use for the target fontMetrics height.

Here's what the layout looks like (from Qt Designer):

12884

The QLabel text I'm trying to autosize is Race.

davethomaspilot
30th June 2018, 16:39
The ScaleTextlabel class posted by Vronin works well for me:



https://forum.qt.io/topic/36088/automatically-scale-text-in-qlabels/10