• <small id='pzDHq'></small><noframes id='pzDHq'>

    <legend id='pzDHq'><style id='pzDHq'><dir id='pzDHq'><q id='pzDHq'></q></dir></style></legend>
  • <i id='pzDHq'><tr id='pzDHq'><dt id='pzDHq'><q id='pzDHq'><span id='pzDHq'><b id='pzDHq'><form id='pzDHq'><ins id='pzDHq'></ins><ul id='pzDHq'></ul><sub id='pzDHq'></sub></form><legend id='pzDHq'></legend><bdo id='pzDHq'><pre id='pzDHq'><center id='pzDHq'></center></pre></bdo></b><th id='pzDHq'></th></span></q></dt></tr></i><div id='pzDHq'><tfoot id='pzDHq'></tfoot><dl id='pzDHq'><fieldset id='pzDHq'></fieldset></dl></div>

        <bdo id='pzDHq'></bdo><ul id='pzDHq'></ul>
      <tfoot id='pzDHq'></tfoot>
      1. 使用@font face 时在 QtWebEngine 中忽略 Google 字体 (

        时间:2023-08-07

          1. <tfoot id='zHrD0'></tfoot>
          2. <small id='zHrD0'></small><noframes id='zHrD0'>

              <tbody id='zHrD0'></tbody>
              <bdo id='zHrD0'></bdo><ul id='zHrD0'></ul>

                <legend id='zHrD0'><style id='zHrD0'><dir id='zHrD0'><q id='zHrD0'></q></dir></style></legend>
                <i id='zHrD0'><tr id='zHrD0'><dt id='zHrD0'><q id='zHrD0'><span id='zHrD0'><b id='zHrD0'><form id='zHrD0'><ins id='zHrD0'></ins><ul id='zHrD0'></ul><sub id='zHrD0'></sub></form><legend id='zHrD0'></legend><bdo id='zHrD0'><pre id='zHrD0'><center id='zHrD0'></center></pre></bdo></b><th id='zHrD0'></th></span></q></dt></tr></i><div id='zHrD0'><tfoot id='zHrD0'></tfoot><dl id='zHrD0'><fieldset id='zHrD0'></fieldset></dl></div>

                  本文介绍了使用@font face 时在 QtWebEngine 中忽略 Google 字体 (ttf)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

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

                  应用程序加载一个带有 css 样式的本地 html 文件.我使用字体加载ttf文件如下:

                  @font-face {font-family: "Work Sans";src: url("C:UsersGodwinTIATfontsWorkSans-Regular.ttf") 格式('truetype');}身体 {font-family: "Work Sans";背景颜色:#eef0f2;}

                  加载 html 文件时似乎忽略了字体.

                  有人可以帮忙吗.

                  编辑强文本

                  这是我的完整html

                  @font-face {字体系列:Work Sans;src: url("Work_Sans/WorkSans-Regular.ttf")}分区 {字体系列:Work Sans;}

                  <!DOCTYPE html><html lang="zh"><头><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>文档</title></头><风格></风格><身体>

                  你好世界</div></身体></html>

                  这些适用于 chrome、firefox 和 chrome,但如果我像这样使用 qtwebengine

                  从 PyQt5 导入 QtCore、QtGui、QtWidgets、QtWebEngineWidgets如果 __name__ == "__main__":导入系统sys.argv.append("--disable-web-security")应用程序 = QtWidgets.QApplication(sys.argv)wnd = QtWidgets.QWidget()genVLayout = QtWidgets.QVBoxLayout(wnd)verticalLayout_7 = QtWidgets.QVBoxLayout()webEngineViewGen = QtWebEngineWidgets.QWebEngineView(wnd)webEngineViewGen.setUrl(QtCore.QUrl(关于:空白"))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())

                  [...]

                  HTML 文档中引用的样式表或图像等外部对象相对于 baseUrl 定位.

                  [...]

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

                  导入操作系统从 PyQt5 导入 QtCore、QtGui、QtWidgets、QtWebEngineWidgets如果 __name__ == __main__":导入系统sys.argv.append("--disable-web-security")应用程序 = 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"))使用 open('main.html','r') 作为 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() 方法:

                  导入操作系统从 PyQt5 导入 QtCore、QtGui、QtWidgets、QtWebEngineWidgets如果 __name__ == __main__":导入系统sys.argv.append("--disable-web-security")应用程序 = 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_())

                  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:UsersGodwinTIATfontsWorkSans-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

                  解决方案

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

                  这篇关于使用@font face 时在 QtWebEngine 中忽略 Google 字体 (ttf)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:以交互模式在 qwebview 中打开 plotly 下一篇:QWebView 中不显示散景图

                  相关文章

                  最新文章

                  <tfoot id='iMCtI'></tfoot>

                    • <bdo id='iMCtI'></bdo><ul id='iMCtI'></ul>
                    <i id='iMCtI'><tr id='iMCtI'><dt id='iMCtI'><q id='iMCtI'><span id='iMCtI'><b id='iMCtI'><form id='iMCtI'><ins id='iMCtI'></ins><ul id='iMCtI'></ul><sub id='iMCtI'></sub></form><legend id='iMCtI'></legend><bdo id='iMCtI'><pre id='iMCtI'><center id='iMCtI'></center></pre></bdo></b><th id='iMCtI'></th></span></q></dt></tr></i><div id='iMCtI'><tfoot id='iMCtI'></tfoot><dl id='iMCtI'><fieldset id='iMCtI'></fieldset></dl></div>
                      <legend id='iMCtI'><style id='iMCtI'><dir id='iMCtI'><q id='iMCtI'></q></dir></style></legend>

                      <small id='iMCtI'></small><noframes id='iMCtI'>