Qt adjusts the resolution to make the text bigger or smaller and then change the solution

foreword

In fact, under normal circumstances, this problem does not seem to be considered. But I encountered this problem, so I have these two optimal solutions.

Solution 1: Modify the style sheet

Fix the text size in the style sheet, so no matter how you adjust the resolution, the font will not be affected.

code snippet:

font: 24pt "Arial";/*24pt//是文字大小,"Arial"是文字字体样式*/

Sample image:

insert image description here

Solution 2: Add the code segment main() to the main function

The effect is similar to the above, but it is in the main function, which is not friendly to some fonts that need to be larger. For those texts that require different sizes, it is better to modify the style sheet. (PS: Setting the font size in the style sheet will override the code setting)

source code:

	QFont font(QStringLiteral("Tahoma"));   //设置字体
	font.setPixelSize(25); //设置字体大小
	qApp->setFont(font);//设置到应用程序中

Sample image:

insert image description here

The text expands and shrinks algorithm with the window change percentage

(PS: put it in the global event, it is necessary to judge whether the size changes && the mouse is released)

		int Old_Width = re->oldSize().width();
		int New_Width = re->size().width();
		int Old_Height = re->oldSize().height();
		int New_Height = re->size().height();
		double Upstep_percentage = (New_Width - Old_Width)*0.1;
		double shrink_percentage = (Old_Width - New_Width)*0.1;
		if (Old_Width < New_Width && Old_Height < New_Height)
		{
    
    
			QFont ft;//QFont设置字体尺寸
			int i_Upstep_percentage = 10 + (Upstep_percentage*0.1);
			ft.setPointSize(i_Upstep_percentage );//设置字体大小
			ui.label->setFont(ft);//设置label中文字大小
		}
		else if (Old_Width > New_Width && Old_Height > New_Height)
		{
    
    

			QFont ft;//QFont设置字体尺寸
			int i_shrink_percentage = 10 + (shrink_percentage*0.1);
			ft.setPointSize(i_shrink_percentage );//设置字体大小
			ui.label->setFont(ft);//设置label中文字大小
		}

Guess you like

Origin blog.csdn.net/weixin_45357007/article/details/125563501