A small piece of code to adjust the QT font size

Source: Internet
Author: User

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 ..

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.