Reputation: 2859
I am using QTWebview
in a window application. When I load a page by setHtml
function, my web view sometimes does not load content images.
This problem especially occur after several pages are loaded.
I am sure that this is an issue of QTWebview
because my page is loaded completely in browsers.
I have embed Fire bug and have found something. The QTWebview
actually does not load new css file. For example, I have 2 css files. Firstly, I copy 1st file into stylesheet folder and load the web. And then, I copy 2nd file into stylesheet and force webview reload. No thing happen. All css items in fire bug are the same with the first, the apperance has no change. I think QTWebview
auto cache data for reload but can find any solution for that. Does anyone have the same problem like me???
Upvotes: 2
Views: 1965
Reputation: 2859
I finally found the solution for this issue. Because I alway load the same URL and change style sheet only,QWebview
auto use it's cache data. I fixed it by adding
QWebSettings::clearMemoryCaches();
before reload.
Upvotes: 3
Reputation: 3689
These stuffs are for using QWebView
in local content and probably for Web
is same with some changes, if you want to load CSS file, you must put it in HTML file and load HTML file in QWebView
, you can embed your HTML file in a resource file (.qrc)
and Load it from resource by adding prefix qrc
, here is an example:
in addresses.h file:
const QString MAIN_HTML = "qrc:/path-to-your-HTML-file-in-resource-file.html";
in MainWindow.cpp:
QWebView *webView = new QWebView();
webView->settings()->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls, true);
webView->page()->settings()->setAttribute(QWebSettings::JavascriptEnabled, true);
webView->page()->settings()->setAttribute(QWebSettings::OfflineWebApplicationCacheEnabled, true);
webView->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
webView->settings()->setUserStyleSheetUrl(QUrl("qrc:/path-your-css-file-in-resource-file.css"));
and finally load HTML file:
webView->load(QUrl(MAIN_HTML)); // remember to include header file -> #include "addresses.h"
if you want to load your files from your local hard disk, use QUrl
by just removing qrc
from your address:
QUrl::fromLocalFile(":/path-to-your-css-file.css");
in your HTML file (if it located in resource file):
<link type="text/css" rel="stylesheet" href="qrc:/path-to-your-css-file-in-resource-file.css"/>
in your HTML file (if it located in local hard disk):
<link type="text/css" rel="stylesheet" href="/path-to-your-css-file.css"/>
so it is best to embed all your files in a resource file and it will be compile and embed in output executable file.
Upvotes: 2