如何在Qt中显示简单的html文件
How to display simple html file in Qt
我正在寻找在 Qt 对话框中显示简单 html 文件(只是 html 格式的长文本)的最简单方法。
如果有链接,请在外部默认系统浏览器中打开。
不需要 QWebView,使用 QTextBrowser:
#include <QTextBrowser>
QTextBrowser *tb = new QTextBrowser(this);
tb->setOpenExternalLinks(true);
tb->setHtml(htmlString);
还记得QT += widgets
http://doc.qt.io/qt-5/qtextedit.html#html-prop
http://doc.qt.io/qt-5/qtextbrowser.html#openExternalLinks-prop
Python 中使用 PySide2 的工作示例:
from PySide2.QtWidgets import QTextBrowser, QApplication
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
text_browser = QTextBrowser()
str_html = """
<!DOCTYPE html>
<html>
<body>
<h1 style="color:blue;">Hello World!</h1>
<p style="color:red;">Lorem ipsum dolor sit amet.</p>
</body>
</html>
"""
text_browser.setText(str_html)
text_browser.show()
text_browser.raise_()
sys.exit(app.exec_())
我正在寻找在 Qt 对话框中显示简单 html 文件(只是 html 格式的长文本)的最简单方法。 如果有链接,请在外部默认系统浏览器中打开。
不需要 QWebView,使用 QTextBrowser:
#include <QTextBrowser>
QTextBrowser *tb = new QTextBrowser(this);
tb->setOpenExternalLinks(true);
tb->setHtml(htmlString);
还记得QT += widgets
http://doc.qt.io/qt-5/qtextedit.html#html-prop
http://doc.qt.io/qt-5/qtextbrowser.html#openExternalLinks-prop
Python 中使用 PySide2 的工作示例:
from PySide2.QtWidgets import QTextBrowser, QApplication
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
text_browser = QTextBrowser()
str_html = """
<!DOCTYPE html>
<html>
<body>
<h1 style="color:blue;">Hello World!</h1>
<p style="color:red;">Lorem ipsum dolor sit amet.</p>
</body>
</html>
"""
text_browser.setText(str_html)
text_browser.show()
text_browser.raise_()
sys.exit(app.exec_())