Qt: Apply a font change on an entire form

2019-07-19 07:30发布

问题:

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.

In Windows,

QApplication::setFont(font);

works perfectly.

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.

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.

My question:

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());

or

    ui->SomeForm->setFont(QApplication::font());

But things like that give me an error...

How can I apply the change for the entire ui ?

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

回答1:

used a style sheet...

QString styleSheet = QString("font-size:%1px;").arg(QApplication::font().pointSize());
this->setStyleSheet(styleSheet);


标签: c++ forms qt fonts