I'm using QWebEngineView from QtWebEngineWidgets to display an image and I want to make it as a background image. But it does not show anything. How can I do that?
I also try QWebView from QtWebKitWidgets but it still does not work.
Here's my code:
view = QtWebEngineWidgets.QWebEngineView()
html = """
<html>
<head>
<style>
body{background-image:url("D:\\house.jpg");}
</style>
</head>
<body></body>
</html>"""
view.setHtml(html)
I expect the view to display the image house.jpg as the background, but it just displays a white window
void QWebEnginePage::setHtml(const QString &html, const QUrl &baseUrl = QUrl())
Sets the content of this page to html. baseUrl is optional and used to resolve relative URLs in the document, such as referenced images or stylesheets.
Tested on Windows.
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtCore import QTimer, QDateTime, QUrl
html = """
<html>
<head>
<meta charset="utf-8">
<title>background-image</title>
<style>
body {
background-image: url('file:///D:/_Qt/Python-Examples/_PyQt5/Image/logo.png');
background-color: #c7b39b;
}
p {
color: #f00;
font: bold italic 20pt 'Comic Sans MS';
}
</style>
</head>
<body>
<p>Hello World</p>
</body>
</html>
"""
def append():
some_html = "<p>{}</p>".format(QDateTime.currentDateTime().toString())
page.runJavaScript("document.body.innerHTML += '{}'".format(some_html)
)
if __name__ == '__main__':
app = QApplication(sys.argv)
view = QWebEngineView()
timer = QTimer()
timer.timeout.connect(append)
page = view.page()
page.loadFinished.connect(lambda: timer.start(1000))
page.setHtml(html, baseUrl=QUrl('file://')) # <---
view.show()
sys.exit(app.exec_())