Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Baffling problems with setUserStyleSheetUrl
Hi. Sorry if this has been posted before but I've searched the web for hours with no luck.
I'm having a completely baffling problem with setUserStyleSheetUrl. I'm trying to use a style sheet on the local machine, and generating the path using QFileInfo file("name.css") and QUrl::fromLocalFile(file.absoluteFilePath()). For some bizarre reason, if I use that the stylesheet is not picked up, but after random experiments on a Windows XP machine, I've found that if I reduce the number of leading slashes in the URI from 3 to 2 (file://C:/... instead of the file:///C:/... returned by fromLocalFile()), the stylesheet is picked up.
This is clearly not right as QWebView::load() correctly finds local HTML files with the (correct) three slashes for a windows local URI.
However, even once this "fix" is applied, I'm finding that tables are not rendering correctly. They render fine in webkit browsers such as Chrome, and they render fine in a QWebView when the stylesheet is embedded in the HTML in a style section, but when the css is loaded with setUserStyleSheetUrl, the borders and cellpadding instructions in the css seem to be ignored.
I can't believe I am the only person to have had this problem but can find nothing even vaguely related on here or on the web generally so I guess I must be missing something. Could someone shed some light on a) why the file is not found using the unmodified URI returned by QUrl::fromLocalFile() and b) why the QWebView is rendering different results using setUserStyleSheetUrl() than when the css is just embedded in the HTML?
Thanks in advance.
andre last edited by
Unfortunately, it does not seem to work that way. There is, however, a workaround. Just like you can with images, you can encode the style sheet file as base64, and then pass it directly. The ULR to generate looks like this:
data:text/css;base64,<the actual base64 encoded file contents>
I know, it is a hassle, but it does work...
Thank you for the fast response, Andre. Thankfully we already have a default system for passing the CSS as base64 encoded string from a database and using an external file is just for testing CSS so less of a hassle than it may seem.
Is this a known issue with setUserStyleSheetUrl? Your response surprises me given the maturity of the library and the activity level of bug fixing going on.
Well, loading the file and encoding it into base 64 then passing it as a data: object to setUserStyleSheetUrl works around the problem with actually locating the file, but the table rendering is still broken.
I've tested this with third party CSS as well as my own and confirmed that the table is rendered correctly if I embed the CSS in the HTML with a style declaration.
Can anyone explain why the rendering would be different using setUserStyleSheetUrl than when embedding the CSS with a style directive in the HTML?