在下面的Python代码中,我在“”标记内有一点手工编码的HTML。它在Chrome中运行良好,但当我在PyQT文本浏览器中显示它时,背景颜色出乎意料
“更多粉红色”行没有粉红色背景
当我读到https://doc.qt.io/qtforpython/PySide2/QtWidgets/QTextBrowser.html时,似乎我正在使用的所有功能都被列为已覆盖。但有些功能仍然存在
什么HTML可以在文本浏览器小部件中实现两行以上的粉红色背景
以下是此示例的代码:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(364, 284)
self.horizontalLayout = QtWidgets.QHBoxLayout(Form)
self.horizontalLayout.setObjectName("horizontalLayout")
self.textBrowser = QtWidgets.QTextBrowser(Form)
self.textBrowser.setObjectName("textBrowser")
self.horizontalLayout.addWidget(self.textBrowser)
self.textBrowser.setHtml(
"""
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html><head><meta name="qrichtext" content="1" />
</head>
<body style=" font-family:'.SF NS Text'; font-size:13pt; font-weight:400; font-style:normal;">
<p>preliminary text</p>
<div style = " -qt-block-indent:1; font-style:italic; background-color:#fff5f5; font-style:italic;">
background pink
<p>more pink</p>
</div>
<p> Non pink <\p>
</body></html>
"""
)
self.retranslateUi(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
我把Qt和PyQt包含为标签,因为我认为C++中的问题将是相同的。
QTextBrowser
是基于只支持limited subset of HTML4的QTextDocument
而不像谷歌Chrome支持HTML5,所以你可以看到区别。如果您想拥有类似于Google Chrome的行为,请使用QWebEngineView
(pip install pyqtwebengine
):(左侧是QTextBrowser,右侧是QWebEngineView)
相关问题 更多 >
编程相关推荐