C++ (Qt)#ifndef PAGEDOWNLOADER_H#define PAGEDOWNLOADER_H #include <QtCore/QObject> class QNetworkAccessManager;class QNetworkReply; class PageDownloader : public QObject{ Q_OBJECT public: explicit PageDownloader(QObject *parent = 0); private slots: void httpFinished(QNetworkReply *reply); private: QNetworkAccessManager *qnam;}; #endif // PAGEDOWNLOADER_H
C++ (Qt)#include "pagedownloader.h" #include <QtCore/QFile> #include <QtNetwork/QNetworkRequest>#include <QtNetwork/QNetworkAccessManager>#include <QtNetwork/QNetworkReply> static const QString basePath = "http://my.website/"; PageDownloader::PageDownloader(QObject *parent) : QObject(parent){ qnam = new QNetworkAccessManager(this); connect(qnam, SIGNAL(finished(QNetworkReply *)), SLOT(httpFinished(QNetworkReply *))); qnam->get(QNetworkRequest(QUrl(basePath)));} void PageDownloader::httpFinished(QNetworkReply *reply){ QFile file("file.htm"); if (file.open(QIODevice::WriteOnly)) { file.write(reply->readAll()); file.flush(); file.close(); } reply->deleteLater(); reply = 0;}
C++ (Qt)file.flush();
C++ (Qt)reply->deleteLater();reply = 0;
C++ (Qt)private:QNetworkAccessManager qnam;QNetworkReply *reply;
C++ (Qt)PageDownloader::PageDownloader(QObject *parent) : QObject(parent){reply = qnam.get(QNetworkRequst(QUrl(basePath)));connect(reply, SIGNAL(finished()), this, SLOT(httpFinished()));} void PageDownloader::httpFinished(){ QFile file("file.htm"); if (file.open(QIODevice::WriteOnly)) { file.write(reply->readAll()); //file.flush(); // нах? file.close(); } reply->deleteLater(); //reply = 0; // тут я хз, надо или нет?}
C++ (Qt) QFile file("file.htm"); if (file.open(QIODevice::WriteOnly)) { reply->readAll(); file.flush(); file.close(); }
C++ (Qt){ QFile file("file.htm"); if (file.open(QIODevice::WriteOnly)) file.write(reply->readAll());} // Здесь будет вызван деструктор QFile, который закроет файл (close) при этом все буферы вытолкнутся на диск (flush)
C++ (Qt) reply->readAll(); QFile file("file.htm"); if (file.open(QIODevice::WriteOnly)) { file.flush(); }