Qt:在整个表单上应用字体更改 [英] Qt: Apply a font change on an entire form
问题描述
我有一些使用Qt Designer创建的表单。 (Qt 4.8)
I have a number of forms, created using the Qt Designer. (Qt 4.8)
我想从某处更改所有表单的字体...
I would like to change the font for all the forms ... from somewhere.
在Windows中,
QApplication::setFont(font);
完美。
成为一个错误(我不知道这个错误报告是否完全相关)...并且 QApplication :: setFont(font);
在移植到OS X的相同qt应用程序中不做任何操作。
Unfortunately there seems to be a bug (i don't know if this bug report is exactly related)... And the QApplication::setFont(font);
does not do anything in the same qt app ported to OS X.
所以...我为OS X手动操作。发出信号并创建一个插槽来更新所有受影响的ui接口字体。
So... I am doing it manually for the OS X. Emit a signal and create a slot to update the interface font, for all ui affected.
我的问题:
如何更改整个表单的字体?我可以改变它对于每个单独的项目包含文本,但有很多的...而且似乎应该有一个更好的方法。
How can I change the font for an entire form ? I can change it for each individual item containing text, but there are very many of them... and it seems that there should be a better way.
SomeForm.cpp
#include "ui_SomeForm.h"
SomeForm::SomeForm(QWidget *parent) :
QDialog(parent),
ui(new Ui::SomeForm)
{
ui->setupUi(this);
...
#if !defined (Q_OS_WIN32)
connect(&source, SIGNAL(fontChanged(QFont)),
this, SLOT(refreshFonts()));
#endif
}
#if !defined (Q_OS_WIN32)
void SomeForm::refreshFonts()
{
ui->btn1->setFont(QApplication::font());
ui->btn2->setFont(QApplication::font());
ui->lbl1->setFont(QApplication::font());
ui->lbl2->setFont(QApplication::font());
......
}
#endif
这个工作,但它可以是很多的项目...我想要的是像
That works, but it could be lots of items... what I would like is something like
ui->setFont(QApplication::font());
或
ui->SomeForm->setFont(QApplication::font());
但是类似的东西给我一个错误...
But things like that give me an error...
如何应用整个ui的更改?
How can I apply the change for the entire ui ?
他会回答一个样式表吗?但复杂的使用QApplication ::字体....我不知道如何设置它的形式,或者如果它不会减慢很多事情...以及它将如何与一个不同的样式表交互一个的表单有... ...
Would a style sheet be he answer ? But complicated to use the QApplication::font.... I don't know how to set it for the form or if it would not slow things down a lot... and how will it interact with a different style sheet that one of the forms has...
推荐答案
使用样式表...
QString styleSheet = QString("font-size:%1px;").arg(QApplication::font().pointSize());
this->setStyleSheet(styleSheet);
这篇关于Qt:在整个表单上应用字体更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!