Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt, QFont for all Widget
Forum Updated to NodeBB v4.3 + New Features

Qt, QFont for all Widget

Scheduled Pinned Locked Moved Solved General and Desktop
qfront
6 Posts 3 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    plevasse
    wrote on last edited by
    #1

    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 :)

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by ChrisW67
      #2

      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.

      P 1 Reply Last reply
      2
      • C ChrisW67

        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.

        P Offline
        P Offline
        plevasse
        wrote on last edited by
        #3

        @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)

        C 1 Reply Last reply
        0
        • hskoglundH Offline
          hskoglundH Offline
          hskoglund
          wrote on last edited by
          #4

          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"));
          }
          
          1 Reply Last reply
          0
          • P plevasse

            @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)

            C Offline
            C Offline
            ChrisW67
            wrote on last edited by
            #5

            @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>
            
            1 Reply Last reply
            1
            • P Offline
              P Offline
              plevasse
              wrote on last edited by
              #6

              I have try and your technique work
              Tank you for your answer !

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved