Results 1 to 4 of 4

Thread: using QWebView cache with QNetworkDiskCache

  1. #1
    Join Date
    Aug 2009
    Posts
    3

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

    Qt Code:
    1. manager = new QNetworkAccessManager();
    2. diskCache = new QNetworkDiskCache();
    3.  
    4. QString location = QDesktopServices::storageLocation(QDesktopServices::CacheLocation);
    5. diskCache->setCacheDirectory(location);
    6. manager->setCache(diskCache);
    7. ui->webView->page()->setNetworkAccessManager(manager);
    8. ui->webView->page()->settings()->setMaximumPagesInCache(10);
    9.  
    10. request = QNetworkRequest();
    11. request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::AlwaysCache);
    12. request.setUrl(QUrl("http://www.qtcentre.org"));
    13.  
    14. ui->webView->load(request);
    To copy to clipboard, switch view to plain text mode 

    Any help would be appreciated.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: using QWebView cache with QNetworkDiskCache

    QNetworkAccessManager will take care of it itself.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Aug 2009
    Posts
    3

    Default 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
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QtNetwork>
    4. #include <QHttpRequestHeader>
    5. #include <QDesktopServices>
    6.  
    7. MainWindow::MainWindow(QWidget *parent)
    8. : QMainWindow(parent), ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11.  
    12. QNetworkDiskCache *diskCache = new QNetworkDiskCache(this);
    13. QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    14. manager = ui->webView->page()->networkAccessManager();
    15. websiteUrl = QUrl("http://www.jann3.com/");
    16.  
    17. QString location = QDesktopServices::storageLocation(QDesktopServices::CacheLocation);
    18. diskCache->setCacheDirectory(location);
    19. manager->setCache(diskCache);
    20. qDebug() << diskCache->cacheSize();
    21. ui->webView->page()->setNetworkAccessManager(manager);
    22. ui->webView->page()->settings()->setMaximumPagesInCache(10);
    23. ui->webView->page()->settings()->setAttribute(QWebSettings::AutoLoadImages,true);
    24. ui->webView->page()->settings()->setAttribute(QWebSettings::JavascriptEnabled,true);
    25.  
    26. request = QNetworkRequest();
    27. request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache);
    28. request.setUrl(websiteUrl);
    29.  
    30. connect(manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished(QNetworkReply*)));
    31. manager->get(request);
    32. }
    33.  
    34. MainWindow::~MainWindow()
    35. {
    36. delete ui;
    37. }
    38.  
    39. void MainWindow::replyFinished(QNetworkReply* reply)
    40. {
    41. QByteArray data=reply->readAll();
    42. QString str(data);
    43.  
    44. qDebug() << "---------------------------------------------------";
    45. qDebug() << "Size:" << data.size();
    46. qDebug() << "Item Url:" << reply->url().toString();
    47. qDebug() << "Content Type:" << reply->header(QNetworkRequest::ContentTypeHeader).toString();
    48. QVariant fromCache = reply->attribute(QNetworkRequest::SourceIsFromCacheAttribute);
    49. qDebug() << "Item from cache?" << fromCache.toBool();
    50. qDebug() << "File Last Modified:" << reply->header(QNetworkRequest::LastModifiedHeader).toString();
    51.  
    52. if(reply->url()==websiteUrl)
    53. {
    54. qDebug() << "Attempting render..";
    55. /*
    56.   // different methods tried
    57.   ui->webView->setContent(data);
    58.   ui->webView->setContent(data,QString("text/html"),reply->url());
    59.   ui->webView->setContent(data,reply->header(QNetworkRequest::ContentTypeHeader).toString(),reply->url());
    60.   ui->webView->setHtml(str,reply->url());
    61.   */
    62. ui->webView->setHtml(str,reply->url());
    63. }
    64. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include <QtNetwork>
    6.  
    7. namespace Ui
    8. {
    9. class MainWindow;
    10. }
    11.  
    12. class MainWindow : public QMainWindow
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. MainWindow(QWidget *parent = 0);
    18. ~MainWindow();
    19.  
    20. private slots:
    21. void replyFinished(QNetworkReply*);
    22.  
    23. private:
    24. QUrl websiteUrl;
    25. //QNetworkAccessManager *manager;
    26. //QNetworkDiskCache *diskCache;
    27. QNetworkRequest request;
    28. Ui::MainWindow *ui;
    29. };
    30.  
    31. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    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.

  4. #4
    Join Date
    Oct 2009
    Location
    Rennes, France
    Posts
    20
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: using QWebView cache with QNetworkDiskCache

    hi.
    To use it, i do something like

    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.
    Contributor from the French Qt community from developpez.com
    * Forum
    * FAQ Qt ( > 100 QR)
    * Advanced and Beginner Tutorials

Similar Threads

  1. Problem With QWebView
    By ivi2501 in forum Qt Programming
    Replies: 8
    Last Post: 2nd August 2009, 19:37
  2. Problems of embedded QWebView in QGraphicsView
    By naruto_9w in forum Qt Programming
    Replies: 2
    Last Post: 20th February 2009, 15:22
  3. QWebView transparent problem
    By embedyy in forum Qt Programming
    Replies: 4
    Last Post: 16th January 2009, 06:20
  4. QWebView - retrieving information?
    By lamera in forum Qt Programming
    Replies: 1
    Last Post: 25th September 2008, 17:53
  5. printing QWebView before showing in Dialog
    By Grisu in forum Qt Programming
    Replies: 1
    Last Post: 17th July 2008, 12:20

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.