Solved Qt, QFont for all Widget
-
Hello world,
I want to changer the font for all the widgets of my project, so I have used qApp->setFont().
Only, the font the font of widgets dosen't change
But qApp->font() return the right font
Do you have a solution ?
Really sory if my English is not right :'(
Thanks you very much for your answer :)
-
QGuiApplication::setFont() sets the default font for the application. If any widget sets another font then that will apply (to that widget, possibly children). If you use Designer and set a font other than the default one Designer offers, which is not related to the one set on the application object, then code to set a font is in the generated code as matter of routine.
-
@ChrisW67
I tried that, but the font daesn't change
I'm looking for a method witch changes the font of all my widgets (I don't want to change the font of all my widgets manualy (there are plenty of widgets) -
Hi, you could try looping through all your widgets and setting the font, say like this from your MainWindow.cpp:
for (auto pKid : findChildren<QWidget*>()) { // do it only on your own (named) widgets QString sName = pKid->objectName(); if (sName.isEmpty()) continue; if (sName.startsWith("qt_")) continue; pKid->setFont(QFont("Your font")); }
-
@plevasse This does here, with the default font widgets changing every second:
main.cpp
#include "widget.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; w.show(); return a.exec(); }
widget.h
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> QT_BEGIN_NAMESPACE namespace Ui { class Widget; } QT_END_NAMESPACE class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = nullptr); ~Widget(); private slots: void toggleFont(); private: Ui::Widget *ui; bool m_state; }; #endif // WIDGET_H
widget.cpp
#include "widget.h" #include "ui_widget.h" #include <QTimer> #include <QFont> Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) , m_state(false) { ui->setupUi(this); QTimer *timer = new QTimer(this); connect(timer, &QTimer::timeout, this, &Widget::toggleFont); timer->start(1000); } Widget::~Widget() { delete ui; } void Widget::toggleFont() { m_state = !m_state; if (m_state) { qApp->setFont(QFont("Sans Serif", 10)); } else { qApp->setFont(QFont("Serif", 12)); } }
widget.ui
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>Widget</class> <widget class="QWidget" name="Widget"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>800</width> <height>400</height> </rect> </property> <property name="windowTitle"> <string>Widget</string> </property> <layout class="QVBoxLayout" name="verticalLayout"> <item> <widget class="QLabel" name="label"> <property name="text"> <string>A label with no font set in Designer</string> </property> </widget> </item> <item> <widget class="QLabel" name="label_2"> <property name="font"> <font> <family>Serif</family> <pointsize>12</pointsize> <italic>false</italic> </font> </property> <property name="text"> <string>A label with a non-default font set in Designer</string> </property> </widget> </item> <item> <widget class="QGroupBox" name="groupBox"> <property name="title"> <string>GroupBox</string> </property> <layout class="QVBoxLayout" name="verticalLayout_2"> <item> <widget class="QRadioButton" name="radioButton"> <property name="text"> <string>No font set</string> </property> </widget> </item> <item> <widget class="QRadioButton" name="radioButton_2"> <property name="font"> <font> <family>Serif</family> <pointsize>12</pointsize> </font> </property> <property name="text"> <string>Non-default font set</string> </property> </widget> </item> <item> <widget class="QCheckBox" name="checkBox"> <property name="text"> <string>No font set</string> </property> </widget> </item> </layout> </widget> </item> </layout> </widget> <resources/> <connections/> </ui>
-
I have try and your technique work
Tank you for your answer !