6

I'm looking for the easiest way to display a simple html file (just a long html-formatted text) inside the Qt dialog. Links, if any, should be opened in the external default system browser.

paws
  • 197
  • 1
  • 2
  • 13

2 Answers2

15

No need for a QWebView, use a QTextBrowser:

#include <QTextBrowser>
QTextBrowser *tb = new QTextBrowser(this);
tb->setOpenExternalLinks(true);
tb->setHtml(htmlString);

also remember QT += widgets

http://doc.qt.io/qt-5/qtextedit.html#html-prop

http://doc.qt.io/qt-5/qtextbrowser.html#openExternalLinks-prop

PHA
  • 1,588
  • 5
  • 18
  • 37
Elcan
  • 814
  • 13
  • 27
5

Working example in Python using 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_())
Matphy
  • 1,086
  • 13
  • 21