#include <QtCore/QCoreApplication>#include <QNetworkAccessManager>#include <QNetworkReply>#include <QDebug>#include <QFile>#include <QThread>#define USAGE QT_TRANSLATE_NOOP("threadDownload::main", "usage: %s <link> <file name>")class ThreadHttpDownload : public QThread{ Q_OBJECT public: ThreadHttpDownload(QObject *parent,QString link); QNetworkReply *reply; QFile file; QString link; void run(); private: private slots: void readyRead(); void replyFinished(QNetworkReply*);};ThreadHttpDownload::ThreadHttpDownload(QObject *parent,QString _link) :QThread(parent){ link = _link;}void ThreadHttpDownload::run(){ file.setFileName("download.file"); file.open(QIODevice::WriteOnly); QNetworkRequest request; // Формируем запрос request.setUrl(QUrl(link)); QNetworkAccessManager manager; manager.moveToThread(this); reply = manager.get(request); //Отправляем запрос reply->moveToThread(this); connect(&manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*))); connect(reply,SIGNAL(readyRead()),this,SLOT(readyRead())); exec();}void ThreadHttpDownload::readyRead(){ qDebug() << reply->size(); file.write(reply->readAll());}void ThreadHttpDownload::replyFinished(QNetworkReply *reply){ if (reply->error()) { qDebug() << "error download" << reply->errorString(); QCoreApplication::quit(); }else { qDebug() << "download complete"; file.close(); QCoreApplication::quit(); }}int main(int argc, char *argv[]){ if (argc < 2) { qDebug(USAGE, argv[0]); return 0; } QCoreApplication a(argc, argv); ThreadHttpDownload *th = new ThreadHttpDownload(0,argv[1]); th->start(); return a.exec();}#include <main.moc>
C:\threadDownload4\debug>threadDownload5.exe http://127.0.0.1/AdobePhotoshopCS4.rar327683276803276832768ASSERT: "d->ref == 1" in file tools\qlistdata.cpp, line 123This application has requested the Runtime to terminate it in an unusual way.Please contact the application's support team for more information.
[New thread 4676.0x1218][New thread 4676.0x13d8][New thread 4676.0x12b8][New thread 4676.0x150]25724380584073007300073007300190200000000000043800000000000000000000000000189801460058407300584029207460438029207300043801460064887300730058401460730058404380000292029204380043800584073007300584005840146000004380292029204380292029200730058407300730058400584014604380043802920438029202920438029204380warning: Heap corruption detected at 01628E80warning: Heap corruption detected at 01624518warning: Heap corruption detected at 01624518warning: HEAP[networkthread.exe]:warning: HEAP: Free Heap block 1624510 modified at 1624520 after it was freedProgram received signal SIGTRAP, Trace/breakpoint trap.0x7c90120f in ntdll!DbgUiConnectToDbg () from C:\WINDOWS\system32\ntdll.dll