using QWebView cache with QNetworkDiskCache
So hey Im completely new to QT, and hopefully this is an obvious problem.
What Im trying to do is cache my webpages and retrieve them from the cache. Caching itself seems to works ok - meaning .cache files are created everytime I load a webpage.
However Im not sure how to go about loading from the cache, any ideas?
Code:
manager = new QNetworkAccessManager();
diskCache = new QNetworkDiskCache();
diskCache->setCacheDirectory(location);
manager->setCache(diskCache);
ui->webView->page()->setNetworkAccessManager(manager);
ui->webView->page()->settings()->setMaximumPagesInCache(10);
request = QNetworkRequest();
request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::AlwaysCache);
request.
setUrl(QUrl("http://www.qtcentre.org"));
ui->webView->load(request);
Any help would be appreciated.
Re: using QWebView cache with QNetworkDiskCache
QNetworkAccessManager will take care of it itself.
Re: using QWebView cache with QNetworkDiskCache
So, an update on this. Now Im using handling the requests better, but whichever way I try and load the files inside a webview, all the relative URL's break after the files have been cached, which you can imagine destroys 90% of websites, CSS and images.
Here's a fully bodged together code example.
mainwindow.cpp
Code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtNetwork>
#include <QHttpRequestHeader>
#include <QDesktopServices>
MainWindow
::MainWindow(QWidget *parent
){
ui->setupUi(this);
QNetworkDiskCache *diskCache = new QNetworkDiskCache(this);
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
manager = ui->webView->page()->networkAccessManager();
websiteUrl
= QUrl("http://www.jann3.com/");
diskCache->setCacheDirectory(location);
manager->setCache(diskCache);
qDebug() << diskCache->cacheSize();
ui->webView->page()->setNetworkAccessManager(manager);
ui->webView->page()->settings()->setMaximumPagesInCache(10);
ui->webView->page()->settings()->setAttribute(QWebSettings::AutoLoadImages,true);
ui->webView->page()->settings()->setAttribute(QWebSettings::JavascriptEnabled,true);
request = QNetworkRequest();
request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache);
request.setUrl(websiteUrl);
connect(manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished(QNetworkReply*)));
manager->get(request);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::replyFinished(QNetworkReply* reply)
{
qDebug() << "---------------------------------------------------";
qDebug() << "Size:" << data.size();
qDebug() << "Item Url:" << reply->url().toString();
qDebug() << "Content Type:" << reply->header(QNetworkRequest::ContentTypeHeader).toString();
QVariant fromCache
= reply
->attribute
(QNetworkRequest
::SourceIsFromCacheAttribute);
qDebug() << "Item from cache?" << fromCache.toBool();
qDebug() << "File Last Modified:" << reply->header(QNetworkRequest::LastModifiedHeader).toString();
if(reply->url()==websiteUrl)
{
qDebug() << "Attempting render..";
/*
// different methods tried
ui->webView->setContent(data);
ui->webView->setContent(data,QString("text/html"),reply->url());
ui->webView->setContent(data,reply->header(QNetworkRequest::ContentTypeHeader).toString(),reply->url());
ui->webView->setHtml(str,reply->url());
*/
ui->webView->setHtml(str,reply->url());
}
}
mainwindow.h
Code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui/QMainWindow>
#include <QtNetwork>
namespace Ui
{
class MainWindow;
}
{
Q_OBJECT
public:
~MainWindow();
private slots:
void replyFinished(QNetworkReply*);
private:
//QNetworkAccessManager *manager;
//QNetworkDiskCache *diskCache;
QNetworkRequest request;
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.ui
contains a QWebView widget named webView.
Now for the strange part.
From the debugging info you can see the page and it's linked elements still have valid URL requests AND are being found in the cache, so the reason for them not rendering correctly is a complete mystery.
Re: using QWebView cache with QNetworkDiskCache
hi.
To use it, i do something like
Quote:
QNetworkDiskCache *diskCache = new QNetworkDiskCache(this);
diskCache->setCacheDirectory("path cache");
webView->page()->networkAccessManager()->setCache(diskCache );
If you want to use the same cache folder for more of one networkAccessManager, you should shared a QNetworkDiskCache between each networkAccessManager instance.
You can find different WebAttribute about cache, but i never understand how use these.