我正在嘗試應用基于類的字體更改,并找到了以下資訊:Changing font size of all QLabel objects PyQt5
QFont font_label = QFont("MS Shell Dlg 2", 12, QFont::Normal);
QApplication::setFont(font_label, "QLabel");
上面的代碼成功地應用了除字體大小之外的所有內容,并且呼叫font_label.setPointSize(x);不會改變任何內容。
專案詳情:
- 將 Qt 5.12.11 與 CMake 和 MSVC 一起使用
- 使用 QT Designer 創建 .ui 檔案
- 除了實作 QDarkStlyeSheet 我沒有做任何與樣式表相關的事情
- 我的代碼中沒有出現其他字體更改
我還發現了奇怪的可重現示例:
我認為這與我的 QLabels 是 RichText 有關,但是將它們全部更改為 PlainText 并沒有解決任何問題。
唯一調整大小的 QLabel 是默認的 pointSize
我的布局似乎沒有強加任何會限制其大小變化的東西,上述情況似乎支持這一點。
嘗試在任何方向更改字體大小時會出現此問題。
用戶界面檔案:
<?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>
uj5u.com熱心網友回復:
您已經為QLabelsin設定了固定的字體大小,ui因此當您更改應用程式的全域字體大小時,它不會影響這些。
<font>
<pointsize>10</pointsize>
</font>
更改應用程式的字體大小會為小部件設定默認大小。如果您設定一個特定小部件的字體大小,它將覆寫默認小部件(這就是您的情況)。
如果要更改字體大小,請設定小部件的字體,例如:
auto lbl = ui->label_2;
auto font = lbl->font();
font.setPointSize(2);
lbl->setFont(font);
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/414852.html
標籤:
