During some applications, the size of some controls (such as qlabel) may be dynamically changed at any time. If the size of the text in the controls does not change, it is very small, or the control is exceeded.
You can use the following code to adjust the font size of the control:
Int cellwidth = 26;
Int fontsize = 1;
While (true)
{
Qfont TMP ("Arial", fontsize );
TMP. setpointsize (fontsize );
Qfontmetricsf FM (TMP );
Qreal pixelswide = FM. Width ("8 ");
Qreal pixelshigh = FM. Height ();
If (pixelswide> = cellwidth | pixelshigh> = cellwidth)
Break;
++ Fontsize;
}
Qfont font ("Arial", fontsize );
The above Code requires the font size of a square control that stores a single digit. It is assumed that the width and height of the control are limited to the value of the cellwidth variable.
Then, create a fontsize font in a successive loop, and use the qfontmetricsf class to obtain the width and height of the font applied to a certain text section ("8" is used here. Compare it with the previous cellwidth to end the loop when it exceeds the upper limit.
In this case, the fontsize is the font size that meets the requirements ....
Simple code and simple requirements do not seem worth mentioning. :) But you can throw this brick to see it ..