setFont() 应用除大小更改之外的所有内容

setFont() Applying Everything except Size Changes

我正在尝试应用基于 Class 的字体更改,并找到了以下信息:

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

以上代码成功应用了除字体大小之外的所有内容,并且调用 font_label.setPointSize(x); 不会改变任何内容。

项目详情:

我还发现了奇怪的可重现示例:

UI 文件:

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

您已经在 ui 中为 QLabels 设置了固定字体大小,因此当您更改应用程序的全局字体大小时,不会影响它们。

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

更改应用程序的字体大小会设置小部件的默认大小。如果您设置一个特定小部件的字体大小,它将覆盖默认的(这就是您的情况)。

如果要更改字体大小,请设置小部件的字体,例如:

auto lbl = ui->label_2;
auto font = lbl->font();
font.setPointSize(2);
lbl->setFont(font);