【问题标题】:QWebView not loading external CSSQWebView 不加载外部 CSS
【发布时间】:2011-01-19 06:47:43
【问题描述】:

我正在使用 QWebView 来显示一些内容,并且我想使用自定义 CSS 来修饰输出。我发现我可以使用QWebSettings.setUserStyleSheetUrl() 方法将我自己的CSS 加载到视图中。 .css 文件与我的主程序位于同一目录中。

self.webview = QWebView(MainWindow)
self.webview.settings().setUserStyleSheetUrl(QUrl.fromLocalFile("myCustom.css"))

但是,当我使用 setHtml() 将内容添加到页面时,不会加载自定义样式。我已经测试了 CSS 是否正确地应用于标准浏览器中的 HTML。

知道我做错了什么吗?

【问题讨论】:

    标签: python qt pyqt qwebview pyside


    【解决方案1】:

    在 Qt 中,所有外部文件的路径都必须是 ABSOLUTE 路径,而不是相对路径。

    为了解决这个问题,我添加了以下更改:

    path = os.getcwd()
    self.webview.settings().setUserStyleSheetUrl(QUrl.fromLocalFile(path + "/myCustom.css"))
    

    一切正常。希望这将有助于将来的某人,并为他们节省几个小时的调试时间。

    【讨论】:

    • 对于非 ASCII 路径名,请考虑使用 os.getcwdu()。我还发现os.getcwdu()在Mac OS-X上可以返回奇怪编码的值,需要通过unicodedata.normalize('NFC', your_unicode_pathname)进行归一化。
    【解决方案2】:

    在 Qt 中,外部文件的所有路径 需要是绝对路径,而不是 相对的。

    那不是真的。下面的代码对我有用。

    #include <QtCore>
    #include <QtGui>
    #include <QtWebKit>
    
    int main(int argc, char ** argv)
    {
        QApplication app(argc, argv);
    
        QMainWindow mainWindow;
    
        QWebView* webView = new QWebView(&mainWindow);
        webView->settings()->setUserStyleSheetUrl(QUrl::fromLocalFile("google.css"));
    
        QFile source("google.html");
        source.open(QIODevice::ReadOnly);
        webView->page()->mainFrame()->setHtml(QString::fromUtf8(source.readAll().constData()));
    
        mainWindow.setCentralWidget(webView);
        mainWindow.show();
    
        return app.exec();
    }
    

    .css 文件在同一目录下 作为我的主程序。

    相对路径是相对于当前工作目录解释的,不需要与可执行文件的目录相同。

    【讨论】:

      【解决方案3】:

      我刚刚遇到了这个问题,所以我将在此处发布我的测试 sn-p; sn-p 在与 python 脚本相同的目录中生成自己的 .html 和 .css 文件;并且从同一目录调用脚本进行测试。

      至少在python/PyQt4 中,似乎-确实-它只有绝对路径可以与setHtml一起使用。

      测试代码可以:

      setHtml 方法似乎只显示带有规范 c3 的样式文本,其中使用了 file:// + 绝对路径。 (编辑:只是想注意this post 中的建议,“尝试arora(QtWebKit 之上的一个非常简单的包装);如果它有效,它是你的代码。如果它没有,它该网站。”对于仔细检查行为非常有帮助

      这是测试脚本的设置:

      $ lsb_release --description --codename 
      Description:    Ubuntu 11.04
      Codename:   natty
      
      $ apt-show-versions -r python-qt4
      python-qt4/natty uptodate 4.8.3-2
      python-qt4-dbus/natty uptodate 4.8.3-2
      
      $ apt-show-versions -r libqtwebkit4
      libqtwebkit4/natty uptodate 2.1~really2.0.2-0ubuntu1
      
      $ python --version
      Python 2.7.1+
      

      脚本是:

      qtwebkit-test.py

      #!/usr/bin/env python
      
      # portions from:
      # http://pysnippet.blogspot.com/2010/01/more-fun-with-qwebkit.html
      
      import sys
      import os
      from PyQt4 import QtCore
      from PyQt4 import QtGui
      from PyQt4 import QtWebKit
      
      global htmltext
      
      def GenerateFiles():
        global htmltext
      
        print "GenerateFiles running"
      
        csstext = """
        body {
          background-color: #058;
          margin: 0px;
          color: red;
        }
        """
      
        css_file = open("test.css", "w")
        css_file.write(csstext)
        css_file.close()
      
      
        htmltextTop = """
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
        <html>
        <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
        """
      
        htmltextBottom = """
        <title>qtwebkit-test</title>
        </head>
        <body>
        <h1>HEADING</h1>
        <p>Just to test ....</p>
        <p>.... and test some more</p>
        </body>
        </html>
        """
      
        cssopen = '<link rel="stylesheet" type="text/css" href="'
        cssclose = '">'
      
        # c1
        cssfile = "test.css"
        # c2
        #~ cssfile = os.path.abspath(os.path.dirname(__file__)) + "/" + "test.css"
        # c3
        #~ cssfile = "file://" + os.path.abspath(os.path.dirname(__file__)) + "/" + "test.css"
        # c4
        #~ cssfile = "qrc://" + os.path.abspath(os.path.dirname(__file__)) + "/" + "test.css"
        # c5 (empty)
        #~ cssfile = ""
      
        cssline = cssopen + cssfile + cssclose
      
        #~ htmltext = htmltextTop + htmltextBottom      # without css
        htmltext = htmltextTop + cssline + htmltextBottom
      
        html_file = open("test.html", "w")
        html_file.write(htmltext)
        html_file.close()
      
      
      def main():
        global htmltext
      
        GenerateFiles()
        qApp = QtGui.QApplication(sys.argv)
      
        webView = QtWebKit.QWebView()
      
        # l1
        #~ webView.load(QtCore.QUrl.fromLocalFile("test.html")) # fails
      
        # l2
        #~ webView.load(QtCore.QUrl.fromLocalFile("./test.html")) # fails
      
        # l3
        #~ webView.load(QtCore.QUrl.fromLocalFile(os.path.abspath(os.path.dirname(__file__)) + "/" + "test.html")) # this works with #c1-#c3
      
        # setHtml
        #print htmltext
        webView.setHtml(htmltext) # works with #c3 (rest are unstyled)
      
        webView.show()
        webView.resize(500, 400)
        webView.setWindowTitle(__file__)
        sys.exit(qApp.exec_())
      
      
      if __name__ == "__main__":
          main()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-06-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-27
        • 1970-01-01
        • 2019-09-18
        相关资源
        最近更新 更多