使用@font face时,QtWebEngine中将忽略Google字体(ttf)。 PyQt5

时间:2019-03-16 00:24:15

标签: python css pyqt pyqt5 qtwebengine

我正在尝试将google字体加载到我的pyqt5 QtWebEngine应用程序中。

该应用程序加载具有CSS样式的本地html文件。我使用字体来加载ttf文件,如下所示:

@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文件时,字体似乎被忽略了。

有人可以帮忙吗?

编辑强文本

这是我的完整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这样的话

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

它的字体不起作用

1 个答案:

答案 0 :(得分:1)

如果您使用的是setHtml(),则如docs所示,外部资源将相对于您作为第二个参数传递的网址:

  

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

     

[...]

     

外部对象(例如HTML文档中引用的样式表或图像)相对于baseUrl。

     

[...]

因此,您的解决方案是:

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