Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display simple html file in Qt

Tags:

qt

qwebview

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.

like image 777
paws Avatar asked Dec 14 '16 08:12

paws


People also ask

Does Qt use HTML?

Qt's text widgets are able to display rich text, specified using a subset of HTML 4 markup.

Can Qt be used for website?

Qt for WebAssembly enables building Qt applications so that they can be integrated into web pages. It doesn't require any client-side installations and reduces the use of server-side resources.

Is Qt good for web development?

Is it a desktop app with web features? If so, yes Qt in general is very good for that. If you mean a kind of server that outputs HTML, then you should use something else because you would have to reinvent many wheels to make it work.


2 Answers

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

like image 156
Elcan Avatar answered Oct 19 '22 14:10

Elcan


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_())
like image 34
Matphy Avatar answered Oct 19 '22 16:10

Matphy