Issue
I am trying to apply Class-Based Font changes, and have found this information: Changing font size of all QLabel objects PyQt5
QFont font_label = QFont("MS Shell Dlg 2", 12, QFont::Normal);
QApplication::setFont(font_label, "QLabel");
The above code successfully applies everything except the Font Size, and calling font_label.setPointSize(x);
does not change anything.
Project Details:
- Using Qt 5.12.11 with CMake and MSVC
- Creating .ui files with QT Designer
- Besides implementing QDarkStlyeSheet I do nothing relevant with Style Sheets
- No other Font alterations appear in my code
I have also discovered odd reproducible examples:
I thought it had to do with my QLabels being RichText, but changing them all to PlainText did not solve anything.
The only QLabel's that resize are default pointSize ones
My layouts do not seems to be imposing anything that would limit their size changes, and the above situations seems to support this.
This problem occurs when trying to change the font size in any direction.
UI File:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MreApp</class>
<widget class="QMainWindow" 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 class="QWidget" name="Back">
<widget class="QLabel" 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 class="QLabel" 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 class="QLabel" 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 class="QLabel" 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 class="QLabel" 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 class="QLabel" 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>
Solution
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);
Answered By - Mat
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.