Google QtWebEngine 在使用@font face 时忽略字体 (ttf)

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

我正在尝试将 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_())

它的字体不工作

如果您使用 setHtml(),那么如 docs 所示,外部资源将与您作为第二个参数传递的 url 相关:

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.

[...]

所以在你的情况下,解决方案是:

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