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?
In Qt, all paths to external files need to be ABSOLUTE paths, not relative ones.
To fix the problem, I add to make the following change:
And everything worked correctly. Hopefully this will help someone in the future and save them a few hours debugging.