Qt HTML子集可能存在未记录的限制

2024-04-23 23:45:32 发布

您现在位置:Python中文网/ 问答频道 /正文

在下面的Python代码中,我在“”标记内有一点手工编码的HTML。它在Chrome中运行良好,但当我在PyQT文本浏览器中显示它时,背景颜色出乎意料

output in Chrome

enter image description here

“更多粉红色”行没有粉红色背景

当我读到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++中的问题将是相同的。


Tags: selfformstylehtmlsystranslate背景font
1条回答
网友
1楼 · 发布于 2024-04-23 23:45:32

QTextBrowser是基于只支持limited subset of HTML4QTextDocument而不像谷歌Chrome支持HTML5,所以你可以看到区别。如果您想拥有类似于Google Chrome的行为,请使用QWebEngineViewpip install pyqtwebengine):

from PyQt5 import QtWidgets, QtWebEngineWidgets


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)

    html ="""
        <!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>
            """

    browser = QtWidgets.QTextBrowser()
    browser.setHtml(html)
    view = QtWebEngineWidgets.QWebEngineView()
    view.setHtml(html)

    w = QtWidgets.QWidget()
    lay = QtWidgets.QHBoxLayout(w)
    lay.addWidget(browser, stretch=1)
    lay.addWidget(view, stretch=1)
    w.resize(640, 480)
    w.show()
    sys.exit(app.exec_())

enter image description here(左侧是QTextBrowser,右侧是QWebEngineView)

相关问题 更多 >