【发布时间】:2018-12-28 18:42:10
【问题描述】:
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文件中获取和设置外部字体:
@font-face {
font-family: "AvenirNext";
src: url("AvenirNext-Regular.ttf") format("truetype");
}
html *
{
color: #e6e6e6;
font-family: "AvenirNext";
background-color: #494845;
margin-left: 14px;
}
如果我在 CSS 中使用 font-family: "Courier New",字体可以正常工作。只有当我从文件夹中获取一些字体时,它才会被忽略。我将 ttf 文件放在应用程序根文件夹和 css 文件所在的文件夹中,以防万一。
链接到组件:
https://doc.qt.io/qt-5.11/qml-qtwebview-webview.html
【问题讨论】: