如何获取由样式表设置的Qt中的小部件的字体? [英] How to get font of widget in Qt set by stylesheet?

查看:349
本文介绍了如何获取由样式表设置的Qt中的小部件的字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有应用自定义样式表的Qt应用程序(以及所有常用的小部件),这个样式表中包含自定义字体。但是当试图获取一些小部件 font()方法的字体时会返回不同的字体。我想获取由样式表设置的 QWidget 的字体。 font()方法总是返回全局系统字体或由 setFont()设置的字体,但不是字体它由 setStyleSheet()设置,用于在小部件中绘画。我需要字体根据字体大小做一些计算。我使用Qt 4.6。我怎样才能得到由样式表设置的小部件的真正的字体(即显示应用程序运行时)?

I have Qt application with custom stylesheet applied to it (and for all widgets in general) with custom font included in this stylesheet. But when try to get font of some widget font() method return different font. I want to get the font of a QWidget which is set by a stylesheet. The font() method always returns the global system font or the font set by setFont(), but not the font which is set by setStyleSheet() and is used for painting in the widget. I need the font to do some calculations based on the font size. I use Qt 4.6. How can I get real font of widget (that is showing when application run) set by stylesheet?

经过一些调查,我看到如果我将定义的样式表应用于某个小部件,可以使用 myWidget-> font()方法获取适当的字体信息(由样式表定义)。当我将样式表设置为整个 MainWindow 时,我可以通过 font()方法获得适当的字体信息, MainWindow 包含。但是,当我将样式表设置为 QApplication 的实例时,所有窗口小部件的 font()方法将返回默认字体或以前由 setFont()设置的字体。为什么要这样?

After some investigations I saw that if I apply defined stylesheet to some widget I can get proper font information (defined by stylesheet) with myWidget->font() method. Also when I set stylesheet to whole MainWindow I can get proper font information with font() method for all the widgets that MainWindow contains. But, when I set stylesheet to instance of QApplication the font() method for all the widgets return default font or the font previously set by setFont(). Why so?

推荐答案

为了从Qt样式表加载值,你应该调用这个方法:

To load values from Qt Stylesheet you should call this methods:

widget->style()->unpolish(widget);
widget->style()->polish(widget);
widget->update();

之后,您的小部件的所有值将根据您指定的样式表值进行更新。

After this all values of your widget will be updated according your stylesheet values specified.

这篇关于如何获取由样式表设置的Qt中的小部件的字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆