tao4yu
tao4yu

Reputation: 326

How to load local static html file with QtWebkit

Using PySide QtWebkit, I want to show a home html page in QWebView. I tried, but I cannot render it. Here is My code:

home.html:

<!DOCTYPE html>
<html>
    <head><title>HomeStart</title></head>
    <body>
        <h3 align="center">Home Page</h3>
        <div>
            <img src="images/welcome.png"/>
        </div>
    </body>
</html>

python code:

self.view = QtWebKit.QWebView()
self.view.load("home.html")  # can not render in webkit.
# QtCore.QUrl.fromLocalFile(QtCore.QFile().fileName()))  # can not render in webkit either.  

PS: the python code file and the html file are in the same directory. But in webkit it renders it blank.

Upvotes: 1

Views: 2170

Answers (2)

ekhumoro
ekhumoro

Reputation: 120578

It makes no difference that the python code file and the html are in the same directory. What matters, is that the html file is in the current directory.

If you use an absolute path:

self.view.load("/path/to/html/files/home.html")

or change to the relevant directory first:

os.chdir("/path/to/html/files")
self.view.load("home.html")

then it should work okay.

Upvotes: 1

Fenikso
Fenikso

Reputation: 9451

It works fine for me with very simple code. Maybe you have omitted something.

import sys
from PySide.QtGui import *
from PySide.QtWebKit import QWebView

class Window(QWidget):
    def __init__(self, *args, **kwargs):
        QWidget.__init__(self, *args, **kwargs)

        self.view = QWebView(self)
        self.view.load("home.html")

        self.layout = QHBoxLayout()
        self.layout.addWidget(self.view)

        self.setLayout(self.layout)
        self.show()

app = QApplication(sys.argv)
win = Window()
sys.exit(app.exec_())

Upvotes: 0

Related Questions