daisy
daisy

Reputation: 23499

Setting external CSS for QWebView with resource file

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.

Upvotes: 2

Views: 2108

Answers (2)

stonecrusher
stonecrusher

Reputation: 1276

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.

Upvotes: 0

teukkam
teukkam

Reputation: 4317

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.

Upvotes: 4

Related Questions