插入短 HTML 文本时 QLabel 字体不同
QLabel font differs when inserting a short HTML text
我注意到 PyQt GUI 中 QLabel()
的字体大小不是很一致。让我们看看下面的例子。我已经写了一个完整的 python 文件用于快速测试。它会弹出一个带有两个标签的 Qt window:
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys
import os
'''---------------------------------------------------------------------'''
''' '''
''' T E S T I N G '''
''' '''
'''---------------------------------------------------------------------'''
class TestWindow(QMainWindow):
def __init__(self):
super(TestWindow, self).__init__()
# 1. Set basic geometry and color.
# --------------------------------
self.setGeometry(100, 100, 800, 600)
self.setWindowTitle('Hello World')
palette = QPalette()
palette.setColor(QPalette.Window, QColor(200,200,200))
self.setPalette(palette)
self.show()
# 2. Create the central frame.
# ----------------------------
self.centralFrame = QFrame(self)
self.centralFrame.setFrameShape(QFrame.NoFrame)
self.centralLayout = QVBoxLayout()
self.centralFrame.setLayout(self.centralLayout)
self.centralLayout.setSpacing(5)
self.centralLayout.setContentsMargins(20,20,20,20)
self.setCentralWidget(self.centralFrame)
# 3. Do the test.
# ----------------
# TEST CASE 1
# The label with html
self.infoLbl = QLabel()
self.infoLbl.setTextFormat(Qt.RichText)
self.infoLbl.setFrameShape(QFrame.StyledPanel)
self.infoTxt = \
'<html> \
<head> \
</head> \
<body> \
<font size="10"> \
<p style="margin-left:8px;">My html text, font = 10pt</p> \
</font> \
</body> \
</html> '
self.infoLbl.setText(self.infoTxt)
self.infoLbl.setMaximumHeight(50)
self.infoLbl.setMaximumWidth(500)
# TEST CASE 2
# The label with a normal string
self.normalLbl = QLabel()
self.normalLbl.setFrameShape(QFrame.StyledPanel)
self.normalLbl.setText('My normal text, font = 10pt')
font = QFont()
font.setFamily("Arial")
font.setPointSize(10)
self.normalLbl.setFont(font)
self.normalLbl.setMaximumHeight(50)
self.normalLbl.setMaximumWidth(500)
# 4. Add both labels to the central layout.
# ------------------------------------------
self.centralLayout.addWidget(self.infoLbl) # <- The label with html snippet
self.centralLayout.addWidget(self.normalLbl) # <- The normal label
self.centralLayout.addWidget(QLabel()) # <- Just a spacer
if __name__== '__main__':
app = QApplication(sys.argv)
QApplication.setStyle(QStyleFactory.create('Fusion'))
testWindow = TestWindow()
app.exec_()
app = None
如果你运行这个代码,这就是你得到的:
为什么两个标签中的字体大小不相等?
为了完整起见,这是我的系统:
- 操作系统:Windows10、64 位
- Python 版本:v3(anaconda 包)
- Qt 版本:PyQt5
我想我找到了答案。
将 html 片段替换为:
self.infoTxt = \
'<html> \
<head> \
</head> \
<body> \
<p style="margin-left:8px; font-size:10pt">My html text, font = 10pt</p> \
</body> \
</html> '
显然 html 标签 <font size="10">
不能正常工作。但是像 style="font-size:10pt"
这样的 CSS-style 属性插入它确实有效。尽管如此,html 标签应该可以正常工作。这闻起来像 Qt 错误?
编辑:
显然,这 不是 Qt 错误。请参阅@ekhumoro 的回答进行说明。
正如您已经发现的那样,您似乎需要改用样式。更多信息请点击此处:
- https://developer.mozilla.org/en-US/docs/Web/HTML/Element/font
- http://www.w3schools.com/tags/tag_font.asp
它说:
This feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it.
和
The tag is not supported in HTML5. Use CSS instead.
Qt4 和 Qt5 仍然完全支持 font
标签。它已被当前的 HTML 标准淘汰的事实无关紧要,因为 Qt 只支持 a limited subset of HTML4.
示例的真正问题是由于对 size
属性的误解。这没有指定 point 大小。相反,它指定 (a) 1-7 范围内的固定值,或 (b) 相对于 basefont
大小的 plus/minus 值。所以它所能做的就是制作字体 "smaller" 或 "larger",具体结果将完全取决于渲染引擎。
要设置精确的磅值,请使用css font-size
属性:
<p style="font-size:10pt">My html text, font = 10pt</p>
注意:有关 Qt 的富文本引擎支持的所有 css 属性的列表,请参阅 CSS Properties Table。
我注意到 PyQt GUI 中 QLabel()
的字体大小不是很一致。让我们看看下面的例子。我已经写了一个完整的 python 文件用于快速测试。它会弹出一个带有两个标签的 Qt window:
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys
import os
'''---------------------------------------------------------------------'''
''' '''
''' T E S T I N G '''
''' '''
'''---------------------------------------------------------------------'''
class TestWindow(QMainWindow):
def __init__(self):
super(TestWindow, self).__init__()
# 1. Set basic geometry and color.
# --------------------------------
self.setGeometry(100, 100, 800, 600)
self.setWindowTitle('Hello World')
palette = QPalette()
palette.setColor(QPalette.Window, QColor(200,200,200))
self.setPalette(palette)
self.show()
# 2. Create the central frame.
# ----------------------------
self.centralFrame = QFrame(self)
self.centralFrame.setFrameShape(QFrame.NoFrame)
self.centralLayout = QVBoxLayout()
self.centralFrame.setLayout(self.centralLayout)
self.centralLayout.setSpacing(5)
self.centralLayout.setContentsMargins(20,20,20,20)
self.setCentralWidget(self.centralFrame)
# 3. Do the test.
# ----------------
# TEST CASE 1
# The label with html
self.infoLbl = QLabel()
self.infoLbl.setTextFormat(Qt.RichText)
self.infoLbl.setFrameShape(QFrame.StyledPanel)
self.infoTxt = \
'<html> \
<head> \
</head> \
<body> \
<font size="10"> \
<p style="margin-left:8px;">My html text, font = 10pt</p> \
</font> \
</body> \
</html> '
self.infoLbl.setText(self.infoTxt)
self.infoLbl.setMaximumHeight(50)
self.infoLbl.setMaximumWidth(500)
# TEST CASE 2
# The label with a normal string
self.normalLbl = QLabel()
self.normalLbl.setFrameShape(QFrame.StyledPanel)
self.normalLbl.setText('My normal text, font = 10pt')
font = QFont()
font.setFamily("Arial")
font.setPointSize(10)
self.normalLbl.setFont(font)
self.normalLbl.setMaximumHeight(50)
self.normalLbl.setMaximumWidth(500)
# 4. Add both labels to the central layout.
# ------------------------------------------
self.centralLayout.addWidget(self.infoLbl) # <- The label with html snippet
self.centralLayout.addWidget(self.normalLbl) # <- The normal label
self.centralLayout.addWidget(QLabel()) # <- Just a spacer
if __name__== '__main__':
app = QApplication(sys.argv)
QApplication.setStyle(QStyleFactory.create('Fusion'))
testWindow = TestWindow()
app.exec_()
app = None
如果你运行这个代码,这就是你得到的:
为什么两个标签中的字体大小不相等?
为了完整起见,这是我的系统:
- 操作系统:Windows10、64 位
- Python 版本:v3(anaconda 包)
- Qt 版本:PyQt5
我想我找到了答案。
将 html 片段替换为:
self.infoTxt = \
'<html> \
<head> \
</head> \
<body> \
<p style="margin-left:8px; font-size:10pt">My html text, font = 10pt</p> \
</body> \
</html> '
显然 html 标签 <font size="10">
不能正常工作。但是像 style="font-size:10pt"
这样的 CSS-style 属性插入它确实有效。尽管如此,html 标签应该可以正常工作。这闻起来像 Qt 错误?
编辑: 显然,这 不是 Qt 错误。请参阅@ekhumoro 的回答进行说明。
正如您已经发现的那样,您似乎需要改用样式。更多信息请点击此处:
- https://developer.mozilla.org/en-US/docs/Web/HTML/Element/font
- http://www.w3schools.com/tags/tag_font.asp
它说:
This feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it.
和
The tag is not supported in HTML5. Use CSS instead.
Qt4 和 Qt5 仍然完全支持 font
标签。它已被当前的 HTML 标准淘汰的事实无关紧要,因为 Qt 只支持 a limited subset of HTML4.
示例的真正问题是由于对 size
属性的误解。这没有指定 point 大小。相反,它指定 (a) 1-7 范围内的固定值,或 (b) 相对于 basefont
大小的 plus/minus 值。所以它所能做的就是制作字体 "smaller" 或 "larger",具体结果将完全取决于渲染引擎。
要设置精确的磅值,请使用css font-size
属性:
<p style="font-size:10pt">My html text, font = 10pt</p>
注意:有关 Qt 的富文本引擎支持的所有 css 属性的列表,请参阅 CSS Properties Table。