2

I'm trying to set external CSS for QWebView with:

ui->webView->settings()->setUserStyleSheetUrl( QUrl::fromLocalFile(":/default.css") );

Which contains only:

body { color: red; }

But it's not working on any page ( nothing is in red )

Also i double checked by:

ui->webView->page()->mainFrame()->toHtml();

But no CSS was applied.

2
  • The path ":/default.css" specifies a file stored as a compiled resource. Is that your intention? Commented Mar 15, 2012 at 2:58
  • @ArnoldSpence yes , I use a Qt's resource file Commented Mar 15, 2012 at 7:55

2 Answers 2

4

Apparently, QUrl::fromLocalFile does not work with resource files. The problem and a workaround is discussed in this forum thread:

Not only the qrc scheme is (usually) not associated with any application, but remember that resources are compiled inside your executable. How is a 3rd party program supposed to access them?

A workaround could be copying the file from the resource to a temporary file/directory and then use QUrl::fromLocalFile + QDesktopServices::openUrl.

Sign up to request clarification or add additional context in comments.

Comments

0

A much simpler solution is to use

ui->webView->settings()->setUserStyleSheetUrl(QUrl("qrc:/filename.css"));
Using QUrl::fromLocalFile() is not at all necessary if you're opening the resource from within the application itself. Writing it to an external file isn't needed unless the URL is being passed to an external application.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.