PDA

View Full Version : QWebView & external css file



folibis
27th November 2013, 02:17
I use QWebView as template editor in my app.
Structure of my project:

MyProject/
app.exe
Templates/
style.css
template.html
This is simplified HTML code of template:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<p>Some content</p>
</body>
</html>
in same directory I have style.css:

html,body {
font-family:Tahoma;
}
But QWebView ignores my styles. I guess it just can't load it.
Code for content loading:

QFile file(QApplication::applicationDirPath() + "/Templates/template.html");
QUrl url(QApplication::applicationDirPath() + "/Templates");
ui->textEdit->setHtml(file.readAll(),url);
I tried

ui->textEdit->settings()->setUserStyleSheetUrl(url);
and

QUrl url("file://" + QApplication::applicationDirPath() + "/Templates");
but nothing helps.
So how can I load local css file to QWebView?

ChrisW67
27th November 2013, 04:42
Your code snippet does not open the file before trying to read from it, which may explain a blank display.

Why don't you simply:


webView->load(QUrl::fromLocalFile(QApplication::application DirPath() + "/Templates/template.html"));

and let the browser find the styles based on the header link element?


BTW: Why have you given a QWebView the truly misleading name of textEdit?

folibis
27th November 2013, 05:10
Thanks ChrisW67, you gave me hope to dig it one more time )
I changed setHtml() to load() and now it works fine. with all css files and page()->settings()->setUserStyleSheetUrl() works too.

At first it loads blank page just because my file was page.tpl and not page.html ))))))



BTW: Why have you given a QWebView the truly misleading name of textEdit?

Heh ... At first it was QTextEdit but it was not good for html editing. So I changed it to QWebView but all names remained )

folibis
27th November 2013, 22:42
It is so strange ... now setHtml() works too. I mean load external css files etc.
It was problem with file extension. I just changed it from *.tpl to *.html and it works fine ))