Home > Enterprise >  setFont() Applying Everything except Size Changes
setFont() Applying Everything except Size Changes

Time:01-15

I am trying to apply Class-Based Font changes, and have found this information: enter image description here

Minimum Reproducible Example:

I have simplified the MRE even more, cutting out the .qrc resources and style sheets, but the issue persists. If the QLabel's fontSize is changed from it's initialized value the error occurs.

#include "mreApp/MreApp.h"

MreApp::MreApp() {

    this->setupUi(this);

    scaling();
}

void MreApp::scaling() {

    QFont font_label = QFont("MS Shell Dlg 2", 16, QFont::Normal);
    QApplication::setFont(font_label, "QLabel");

    return;
}

void MreApp::slotExit() {
    qApp->exit();
}
#ifndef MreApp_H
#define MreApp_H

#include <Qt>
#include <QApplication>
#include <QFont>

#include "ui_MreApp.h"

class MreApp : public QMainWindow , private Ui::MreApp {
    Q_OBJECT

    public:
    MreApp();
    ~MreApp() {};

    void scaling();

    virtual void slotExit();
};
#endif
#include <QApplication>
#include <QMainWindow>

#include "mreApp/MreApp.h"

int main(int argc, char** argv) {
    QApplication app(argc, argv);

    MreApp _MreApp;
    _MreApp.show();

    return app.exec();
}
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MreApp</class>
 <widget  name="MreApp">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>1920</width>
    <height>1050</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MreApp</string>
  </property>
  <property name="styleSheet">
   <string notr="true"/>
  </property>
  <widget  name="Back">
   <widget  name="label_1">
    <property name="geometry">
     <rect>
      <x>490</x>
      <y>660</y>
      <width>95</width>
      <height>20</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>8</pointsize>
     </font>
    </property>
    <property name="text">
     <string>I start size 8</string>
    </property>
    <property name="textFormat">
     <enum>Qt::PlainText</enum>
    </property>
    <property name="alignment">
     <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
    </property>
   </widget>
   <widget  name="label_4">
    <property name="geometry">
     <rect>
      <x>690</x>
      <y>80</y>
      <width>481</width>
      <height>131</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>10</pointsize>
     </font>
    </property>
    <property name="text">
     <string>Try Draggin in a new QLabel in QT Designer, it will listen to the code temporarily.</string>
    </property>
   </widget>
   <widget  name="label_2">
    <property name="geometry">
     <rect>
      <x>660</x>
      <y>570</y>
      <width>95</width>
      <height>20</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>10</pointsize>
     </font>
    </property>
    <property name="text">
     <string>I start size 10</string>
    </property>
    <property name="textFormat">
     <enum>Qt::PlainText</enum>
    </property>
    <property name="alignment">
     <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
    </property>
   </widget>
   <widget  name="label_3">
    <property name="geometry">
     <rect>
      <x>780</x>
      <y>570</y>
      <width>95</width>
      <height>20</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>10</pointsize>
     </font>
    </property>
    <property name="text">
     <string>I start size 10</string>
    </property>
    <property name="textFormat">
     <enum>Qt::PlainText</enum>
    </property>
    <property name="alignment">
     <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
    </property>
   </widget>
   <widget  name="label_5">
    <property name="geometry">
     <rect>
      <x>1190</x>
      <y>570</y>
      <width>371</width>
      <height>16</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>10</pointsize>
     </font>
    </property>
    <property name="text">
     <string>(The Label Below is a fresh one, dragged in from QT Designer)</string>
    </property>
   </widget>
   <widget  name="label">
    <property name="geometry">
     <rect>
      <x>1270</x>
      <y>690</y>
      <width>201</width>
      <height>61</height>
     </rect>
    </property>
    <property name="text">
     <string>TextLabel</string>
    </property>
   </widget>
  </widget>
  <action name="menu_new">
   <property name="text">
    <string>New</string>
   </property>
  </action>
  <action name="menu_load">
   <property name="text">
    <string>Load</string>
   </property>
  </action>
  <action name="menu_save">
   <property name="text">
    <string>Save</string>
   </property>
  </action>
  <action name="menu_saveAs">
   <property name="text">
    <string>Save As</string>
   </property>
  </action>
  <action name="menu_userManual">
   <property name="text">
    <string>User Manual</string>
   </property>
  </action>
 </widget>
 <resources/>
 <connections/>
</ui>

QT Designer: enter image description here

Application: enter image description here

As pictured above, the bottom right label is the only one that listens to the change in size.

Using QFontInfo I get the QLabel's original pointSize value, unaffected by setFont().

int ps = label->fontInfo().pointSize();

CodePudding user response:

You've set a fixed font size for the QLabels in the ui therefore when you change the application's global font size, it won't affect those.

<font>
  <pointsize>10</pointsize>
 </font>

Changing the application's font size sets a default size for the widgets. If you set the font size of one specific widget, it will overwrite the default one (this is what happened in your case).

If you want to change the font size, set the widget's font, like:

auto lbl = ui->label_2;
auto font = lbl->font();
font.setPointSize(2);
lbl->setFont(font);
  •  Tags:  
  • Related