title: How to stop QHttp/QtWebKit from caching pages
body: <p>Recently I found that I could modify a txt file that's stored online and when I then fetch it using <strong>QHttp</strong> I'm still getting the previous version.  My code also used <strong>QtWebKit</strong> so maybe this relates to the problem somehow?  Or maybe I'm just missing a setting that prevents caching?</p>
  
  <p>(I'm using Qt 4.7.3 under Windows 7 and compiling with the Visual Studio 2010 Add-in)</p>
  
  <p>I'm doing the following to prevent <strong>QtWebKit</strong> from caching anything -</p>
  
  <pre><code>QWebSettings::globalSettings( )-&gt;setAttribute( QWebSettings::PrivateBrowsingEnabled, true );
  QWebSettings::globalSettings( )-&gt;setAttribute( QWebSettings::LocalContentCanAccessRemoteUrls, true );
  QWebSettings::setMaximumPagesInCache( 0 );
  QWebSettings::setObjectCacheCapacities( 0, 0, 0 );
  QWebSettings::clearMemoryCaches( );
  </code></pre>
  
  <p>The code I'm using to load the txt file is as follows -</p>
  
  <pre><code>CacheHTML::CacheHTML( QObject* parent, QString urlId, QByteArray* bytes, QString stringUrl ) : QObject( parent ) {
      this-&gt;urlId = urlId;
      QUrl url = QUrl( stringUrl );
      http = new QHttp( this );
      connect( http, SIGNAL( requestFinished( int, bool )), this, SLOT( Loaded( int, bool )));
      pBytes = bytes;
      buffer = new QBuffer( pBytes, this );
      buffer-&gt;open( QIODevice::WriteOnly );
      http-&gt;setHost( url.host( ));
      fetchId = http-&gt;get( url.toString( ), buffer );
  }
  </code></pre>
  
  <p>The slot I'm using to correspond with this is -</p>
  
  <pre><code>void CacheHTML::Loaded( int id, bool error ) {
      if( id != fetchId )
          return;
      if( error ) {
          emit CacheComplete( urlId, false );
          return;
      }
      emit CacheComplete( urlId, true );
  }
  </code></pre>
  
  <p>But once it's fetched the txt file from the internet it seems to be caching it somewhere/somehow.  No matter what changes I make in the txt file, when I run my code it still buffers the original file.</p>
  
  <p>I've tried OrcunC's suggestion (below) but this results in receiving an empty string as opposed to the contents of the txt file -</p>
  
  <pre><code>CacheHTML::CacheHTML( QObject* parent, QString urlId, QByteArray* bytes, QString stringUrl ) : QObject( parent ) {
      this-&gt;urlId = urlId;
      QUrl url = QUrl( stringUrl );
      http = new QHttp( this );
      connect( http, SIGNAL( requestFinished( int, bool )), this, SLOT( Loaded( int, bool )));
      pBytes = bytes;
      buffer = new QBuffer( pBytes, this );
      buffer-&gt;open( QIODevice::WriteOnly );
  
      QHttpRequestHeader header( "GET", url.path( ));
      header.setValue( "Cache-Control", "no-cache" );
      header.setValue( "Host", url.host( ));
  
      http-&gt;setHost( url.host( ));
      fetchId = http-&gt;request( header );
  }
  </code></pre>
  
  <p>I've also tried implementing it in a different way as show below, but got the same results as if the txt file is being cached.  Even though checking the <strong>QNetworkReply</strong> in the <strong>finished</strong> slot with <strong>bool fromCache = reply->attribute( QNetworkRequest::SourceIsFromCacheAttribute ).toBool( )</strong> returned <strong>false</strong>.</p>
  
  <pre><code>    QNetworkAccessManager* manager = new QNetworkAccessManager( this );
      connect( manager, SIGNAL( finished( QNetworkReply* )), this, SLOT( Loaded( QNetworkReply* )));
      QNetworkRequest req = QNetworkRequest( QUrl( stringUrl ));
      req.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::AlwaysNetwork );
      req.setAttribute( QNetworkRequest::CacheSaveControlAttribute, false );
      manager-&gt;get( req );
  </code></pre>
  
  <p>I'm at my wit's end now so would be very grateful for anyone pointing me in the right direction.</p>