简体   繁体   English

如何通过样式表获取Qt中设置的小部件的字体?

[英]How to get font of widget in Qt set by stylesheet?

I have Qt application with custom stylesheet applied to it (and for all widgets in general) with custom font included in this stylesheet. 我有一个Qt应用程序,该样式表中包括了自定义字体,并且该Qt应用程序已应用了自定义样式表(以及所有普通小部件)。 But when try to get font of some widget font() method return different font. 但是,当尝试获取某些小部件font()方法的字体时,将返回不同的字体。 I want to get the font of a QWidget which is set by a stylesheet. 我想获取由样式表设置的QWidget的字体。 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. font()方法始终返回全局系统字体或setFont()设置的字体,但不返回setStyleSheet()设置的,用于在小部件中绘画的字体。 I need the font to do some calculations based on the font size. 我需要字体根据字体大小进行一些计算。 I use Qt 4.6. 我使用Qt 4.6。 How can I get real font of widget (that is showing when application run) set by stylesheet? 如何获得样式表设置的小部件的真实字体(在应用程序运行时显示)?

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. 经过一些调查,我发现如果将定义的样式表应用于某些小部件,则可以使用myWidget->font()方法获得正确的字体信息(由样式表定义myWidget->font() Also when I set stylesheet to whole MainWindow I can get proper font information with font() method for all the widgets that MainWindow contains. 同样,当我将样式表设置为整个MainWindow我可以通过font()方法获取MainWindow包含的所有小部件的正确字体信息。 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() . 但是,当我将样式表设置为QApplication的实例时,所有小部件的font()方法都将返回默认字体或先前由setFont()设置的字体。 Why so? 为什么这样?

You can retrieve a font of a specific widget reading it's properties, as the following: 您可以读取特定窗口小部件的字体,读取其属性,如下所示:

//Get pushbutton font.
QFont font = ui->pushButton->property("font").value<QFont>();
qDebug() << font.family() << font.pointSize();

//Get MainWindow font.
QFont font2 = property("font").value<QFont>();
qDebug() << font2.family() << font2.pointSize();

To load values from Qt Stylesheet you should call this methods: 要从Qt样式表加载值,您应该调用以下方法:

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. 此后,将根据指定的样式表值来更新小部件的所有值。

The best I can tell from QStyleSheetStyle::updateStyleSheetFont , the widget always contains the resolved font from the stylesheet. 我可以从QStyleSheetStyle :: updateStyleSheetFont看出来 ,最好的是,小部件始终包含样式表中已解析的字体。 I'd expect QWidget::font() to return the resolved font that you've set using the stylesheet - ie the font that is the merged application font, any parent widget fonts, and the stylesheet font. 我希望QWidget::font()返回使用样式表设置的已解析字体-即作为合并的应用程序字体,任何父窗口小部件字体和样式表字体的字体。

The widget must be polished first, of course, unless you're querying after the events have been delivered (ie from within the event loop). 当然,必须首先对窗口小部件进行打磨,除非您在事件交付后(即从事件循环内)进行查询。

// https://github.com/KubaO/stackoverflown/tree/master/questions/style-font-query-test-45422885
#include <QtWidgets>

int main(int argc, char ** argv) {
   QApplication app{argc, argv};
   QLabel label("Test");
   auto font1 = label.font();
   label.setStyleSheet("font-size: 49pt;");
   label.show();
   label.ensurePolished();
   auto font2 = label.font();
   Q_ASSERT(font1.pointSize() != 49);
   Q_ASSERT(font2.pointSize() == 49);
   Q_ASSERT(font1.family() == font2.family());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM