Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python WebView组件忽略自定义字体_Python_Css_Pyqt_Qml_Pyqt5 - Fatal编程技术网

Python WebView组件忽略自定义字体

Python WebView组件忽略自定义字体,python,css,pyqt,qml,pyqt5,Python,Css,Pyqt,Qml,Pyqt5,PyQt5有一个名为WebView(不是QWebView!)的组件,它可以用loadHtml方法加载html。如果传递给方法的字符串包含对外部字体的引用,则忽略该字体 正在qml文件中加载html: mainWebView.loadHtml(article); // mainWebView is the id of a WebView component 正在准备py文件中的html字符串: with open(CSS_PATH, 'r') as f: css_style = f.re

PyQt5有一个名为WebView(不是QWebView!)的组件,它可以用loadHtml方法加载html。如果传递给方法的字符串包含对外部字体的引用,则忽略该字体

正在qml文件中加载html:

mainWebView.loadHtml(article); // mainWebView is the id of a WebView component
正在准备py文件中的html字符串:

with open(CSS_PATH, 'r') as f:
    css_style = f.read()

article = "<html><head><style>%s</style><title>%s</title></head><body>%s</body></html>" % (css_style, article['title'], article['body'])
如果我在CSS中使用
字体系列:“Courier New”
,字体效果很好。只有当我从文件夹中获取一些字体时,它才会被忽略。我把ttf文件放在approot文件夹和css文件所在的文件夹中,以防万一。 链接到组件:

根据:

无效加载html(字符串html,url baseUrl)

将指定的html内容加载到web视图

此方法提供了url属性的较低级别替代方案, 它通过URL引用HTML页面

外部对象,如HTML中引用的样式表或图像 文档的位置应相对于baseUrl。例如,如果html 是从, 它是基本URL,然后是一个用相对URL引用的图像, diagram.png,应位于

.css、.js或任何外部元素都将与baseUrl相对,后者是
setHtml()
的第二个参数。因此,解决方案是向其传递一个虚拟文件的url,该文件应位于本地文件夹中

main.py

import os
import sys
from PyQt5 import QtCore, QtGui, QtQml

if __name__ == '__main__':
    app = QtGui.QGuiApplication(sys.argv)
    engine = QtQml.QQmlApplicationEngine()
    dir_path = os.path.dirname(os.path.abspath(__file__))
    CSS_PATH = "styles.css"
    with open(os.path.join(dir_path, CSS_PATH), 'r') as f:
        css_style = f.read()
        article = {"title": "title1", "body": "<H1> Body"}
        article = "<html><head><style>%s</style><title>%s</title></head><body>%s</body></html>" % (css_style, article['title'], article['body'])
        baseUrl = QtCore.QUrl.fromLocalFile(os.path.join(dir_path, 'index.html'))
        engine.rootContext().setContextProperty("article", article)
        engine.rootContext().setContextProperty("baseUrl", baseUrl)
    qml_filename = os.path.join(dir_path, 'main.qml')
    engine.load(QtCore.QUrl.fromLocalFile(qml_filename))
    if not engine.rootObjects():
        sys.exit(-1)
    sys.exit(app.exec_())
我假设该项目具有以下结构:

|-- AvenirNext-Regular.ttf
|-- main.py
|-- main.qml
`-- styles.css
import QtQuick 2.5
import QtQuick.Window 2.2
import QtWebView 1.1

Window {
    visible: true
    width: 640
    height: 480
    WebView{
        id: mainWebView
        anchors.fill: parent
    }
    Component.onCompleted: mainWebView.loadHtml(article, baseUrl)
}
|-- AvenirNext-Regular.ttf
|-- main.py
|-- main.qml
`-- styles.css