Google Fonts (Ttf) Being Ignored in Qtwebengine When Using @Font Face

Google Fonts (ttf) being ignored in QtWebEngine when using @font face

If you are using setHtml() then as indicated by the docs the external resources will be relative to the url that you pass as second parameters:

void QWebEngineView::setHtml(const QString &html, const QUrl &baseUrl = QUrl())

[...]

External objects, such as stylesheets or images referenced in the HTML document, are located relative to baseUrl.

[...]

So in your case the solution is:

import os
from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets

if __name__ == "__main__":
import sys
sys.argv.append("--disable-web-security")
app = QtWidgets.QApplication(sys.argv)
wnd = QtWidgets.QWidget()
genVLayout = QtWidgets.QVBoxLayout(wnd)
verticalLayout_7 = QtWidgets.QVBoxLayout()
webEngineViewGen = QtWebEngineWidgets.QWebEngineView(wnd)
webEngineViewGen.setUrl(QtCore.QUrl("about:blank"))
with open('main.html','r') as fh:
html = fh.read()
current_dir = os.path.dirname(os.path.abspath(__file__))
url = QtCore.QUrl.fromLocalFile(os.path.join(current_dir, "main.html"))
webEngineViewGen.setHtml(html, url)
verticalLayout_7.addWidget(webEngineViewGen)
genVLayout.addLayout(verticalLayout_7)
wnd.show()
sys.exit(app.exec_())

Or simply use the load() method:

import os
from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets

if __name__ == "__main__":
import sys
sys.argv.append("--disable-web-security")
app = QtWidgets.QApplication(sys.argv)
wnd = QtWidgets.QWidget()
genVLayout = QtWidgets.QVBoxLayout(wnd)
verticalLayout_7 = QtWidgets.QVBoxLayout()
webEngineViewGen = QtWebEngineWidgets.QWebEngineView(wnd)
webEngineViewGen.setUrl(QtCore.QUrl("about:blank"))
current_dir = os.path.dirname(os.path.abspath(__file__))
url = QtCore.QUrl.fromLocalFile(os.path.join(current_dir, "main.html"))
webEngineViewGen.load(url)
verticalLayout_7.addWidget(webEngineViewGen)
genVLayout.addLayout(verticalLayout_7)
wnd.show()
sys.exit(app.exec_())

How to set a background image in QWebEngineView

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_())

Sample Image



Related Topics



Leave a reply



Submit