使用@font face时在QtWebEngine中忽略Google字体(ttf) [英] Google Fonts (ttf) being ignored in QtWebEngine when using @font face
问题描述
我正在尝试将google字体加载到我的pyqt5 QtWebEngine应用程序中.
I am trying to load a google font to my pyqt5 QtWebEngine app.
该应用程序加载具有CSS样式的本地html文件.我使用了字体来加载ttf文件,如下所示:
The app loads a local html file with css stying. I used font face to load the ttf file as below:
@font-face {
font-family: "Work Sans";
src: url("C:\Users\Godwin\TIAT\fonts\WorkSans-Regular.ttf") format('truetype');
}
body {
font-family: "Work Sans";
background-color: #eef0f2;
}
加载html文件时,字体似乎被忽略了.
the font seem to be ignored when loading the html file.
有人可以帮忙吗?
编辑强文本
这是我的完整html
Here is my full html
@font-face {
font-family: Work Sans;
src: url("Work_Sans/WorkSans-Regular.ttf")
}
div {
font-family: Work Sans;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" type="text/css" href="style.css"/>
<title>Document</title>
</head>
<style>
</style>
<body>
<div>
Hello World
</div>
</body>
</html>
这些可以在chrome,firefox和chrome上使用,但是如果我像这样使用qtwebengine
These works on chrome, firefox and chrome but if i use qtwebengine like this
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"))
fh = open('main.html','r')
html = fh.read()
webEngineViewGen.setHtml(html)
verticalLayout_7.addWidget(webEngineViewGen)
genVLayout.addLayout(verticalLayout_7)
wnd.show()
sys.exit(app.exec_())
它的字体不起作用
推荐答案
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:
无效QWebEngineView :: setHtml(const QString& html,const QUrl& baseUrl = QUrl())
[...]
外部对象(例如HTML文档中引用的样式表或图像)相对于baseUrl定位.
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_())
或者只需使用 load()
方法:>
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_())
这篇关于使用@font face时在QtWebEngine中忽略Google字体(ttf)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!