PyQt6中UI文件定义的QLabel字体加粗失效,如何调整UI文件?
PyQt6中QLabel在UI文件中实现加粗字体的解决方案
针对你升级PyQt6(Qt6.7.1)后,QLabel原有UI字体加粗设置失效的问题,可通过以下几种UI文件调整方式解决:
方法1:直接在UI中添加样式表属性
在QLabel的属性中增加styleSheet配置,明确指定字体加粗,这是最可靠的方式:
<widget class="QLabel" name="label"> <property name="font"> <font> <pointsize>12</pointsize> </font> </property> <property name="styleSheet"> <string notr="true">font-weight: bold;</string> </property> <property name="text"> <string>Plot Control</string> </property> </widget>
方法2:移除冗余的weight属性
Qt6中weight属性可能会干扰bold属性的生效,尝试删除<weight>75</weight>,仅保留<bold>true</bold>:
<widget class="QLabel" name="label"> <property name="font"> <font> <pointsize>12</pointsize> <bold>true</bold> </font> </property> <property name="text"> <string>Plot Control</string> </property> </widget>
方法3:指定支持加粗的字体家族
如果默认系统字体缺少加粗变种,可明确指定一个支持加粗的字体(如Arial、微软雅黑):
<widget class="QLabel" name="label"> <property name="font"> <font> <family>Arial</family> <pointsize>12</pointsize> <bold>true</bold> </font> </property> <property name="text"> <string>Plot Control</string> </property> </widget>
原因说明
Qt6对字体渲染逻辑进行了调整,UI文件中weight与bold属性的解析优先级、生效规则与Qt5存在差异,部分场景下权重值的设置会导致加粗标记未被正确识别。
内容的提问来源于stack exchange,提问作者braunlee
相关产品推荐
相关产品推荐

