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

问题描述 投票:1回答:1

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

该应用程序使用css stying加载本地html文件。我用font face加载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文件时似乎忽略了字体。

有人可以帮助。

Editstrong文字

这是我的完整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_())

它的字体不起作用

python css pyqt pyqt5 qtwebengine
1个回答
1
投票

如果您正在使用setHtml()然后如docs所示,外部资源将相对于您作为第二个参数传递的URL:

空隙QWebEngineView :: setHtml(常量的QString&HTML,INT&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_())
© www.soinside.com 2019 - 2024. All rights reserved.