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

点点圈 提交于 2020-07-01 07:27:51

问题


I am trying to load a google font to my pyqt5 QtWebEngine app.

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;
}

the font seem to be ignored when loading the html file.

Can someone please assist.

Editstrong text

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>

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

it font does not work


回答1:


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


来源:https://stackoverflow.com/questions/55192269/google-fonts-ttf-being-ignored-in-qtwebengine-when-using-font-face

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!