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_())
Related Topics
Tensorflow Different Ways to Export and Run Graph in C++
How to Compile Opencv for iOS7 (Arm64)
Size Legend for Plotly Bubble Map/Chart
Xcode 3.2 Ruby and Python Templates
Call a Function with Argument List in Python
How to Do Multiple Substitutions Using Regex
Does Python Optimize Modules When They Are Imported Multiple Times
Django HTML Template Can't Find Static CSS and Js Files
Pip Freeze Creates Some Weird Path Instead of the Package Version
In Python, How to Capture the Stdout from a C++ Shared Library to a Variable
I Can't Import Python Modules in Xcode 11 Using Pythonkit
Data Scraping from Published Power Bi Visual
Does Ruby Support Conditional Regular Expressions
How to Import CSV Data into Django Models