質問

を使用していQWebViewを表示一部のコンテンツおよび使いたいカスタムCSSスの出力に出力します。これらの本を利用できま QWebSettings.setUserStyleSheetUrl() 法への負荷の自分のCSSします。の .css ファイルと同じディレクトリとしての私のメインプログラムを置きます。

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

しかし、カスタムstylingsな負荷時に追加のコンテンツのページを利用 setHtml().いを行っているのCSSが適切に適用されるHTMLの標準ブラウザです。

あらゆるアイデアないということは間違いだったのか?

役に立ちましたか?

解決

Qt、すべての経路は外部ファイルが必要 絶対 パスは相対す。

の問題を追加する事項を次のとおり変更:

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

く機能しない問題を修正この手の人は、将来、保存し、数時間でデバッグしやすくなります。

他のヒント

  

での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ファイルが同じディレクトリにあります   私のメインプログラムとしてます。

相対パスを実行可能ファイルのディレクトリと同じである必要はなく、現在の作業ディレクトリに対して解釈されます。

かっこいいポスト試験のスニペット、スニペットを生成します。htmlます。cssファイルと同じディレクトリにpythonスクリプトのスクリプトと呼ばれたから同じディレクトリにテストが可能です。

少なくとも python/PyQt4, であで のみ 絶対パスと setHtml.

テストコードは:

compare_qtwebkit-test.py

setHtml 方法が見てとれるのではないstyled textだけで仕様 c3, では、 file:// +絶対パスを使用します。(編集:<url> <url> <url> <url> <url> <url> <url> <url> <url> <url>なお、ご提案に このポスト, は、"チャレンジする"という arora りを少し入った場所にある簡易包装のQtWebKit);だが、そのおのコードです。っていることを確認してください、その本サイトの"カルチェックを行)

これは、setupスクリプトを試験をしました。

$ 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()
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top