2

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???

2 Answers 2

3

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.

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

Comments

2

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.

5 Comments

Thanks for reply. But there is no change in my web view. I tried to set setUserStyleSheetUrl each time new page is loaded but no help. My app allows user modify the apperance so I can not embed the css files in a resource.
I assume that I have 2 saved files. Open a file is ok, user can continue edit the web. But if he/she continue open the second, Qwebview will load the first one. It 's really strange. It 's always happening no matter what which file is opened first.
@sahara108: on every file opening you should set its address to load(QUrl(address)) and QwebView loads that file, if it doesn't load the file, the problem was from anywhere than load method, post your codes here for further verification.
Firstly, I copy a stylesheet into working folder using _dispatcherManager->RouteMessage(eFilesystem, eZipFolder, sZipFile); . And then use the below code to load the page QUrl url = QUrl( "file:///" + QString( jIndexFile["path"].asString().c_str() ) + "?t=" + std::rand() * 1000 ); QString string = url.toString(); _myWebView->load(url); The "?t=" + std::rand() * 1000 to make my url be different each time I load the web.
I 've found the solution for my problem. Use QWebSettings::clearMemoryCaches(); before loading new page.

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.