views:

409

answers:

1

I am using a QWebView to display some content and I want to use custom CSS to spruce up the output. I found that I can use the QWebSettings.setUserStyleSheetUrl() method to load my own CSS into the view. The .css file is in the same directory as my main program.

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

However, the custom stylings don't load when I add the content to the page using setHtml(). I have tested that the CSS is properly applying to the HTML in a standard browser.

Any idea what I am doing wrong?

+3  A: 

In Qt, all paths to external files need to be ABSOLUTE paths, not relative oaths.

To fix the problem, I add to make the following change:

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

And everything worked correctly. Hopefully this will help someone in the future and save them a few hours debugging.

swanson