/ ************** main.cpp **************** /#include <QtCore/QCoreApplication>#include <QDebug>#include "threadhttpdownload.h"#define USAGE QT_TRANSLATE_NOOP("threadDownload::main", "usage: %s <link> <file name>")int main(int argc, char *argv[]){ if (argc < 2) { qDebug(USAGE, argv[0]); return 0; } QCoreApplication a(argc, argv); for (int i = 0; i < 5; i++) { ThreadHttpDownload *th = new ThreadHttpDownload(0,argv[1]); th->start(); qDebug() << "tread " << i << " started"; } return a.exec();}/ ************** threadhttpdownload.h **************** /#ifndef THREADHTTPDOWNLOAD_H#define THREADHTTPDOWNLOAD_H#include <QThread>#include <QNetworkAccessManager>#include <QNetworkReply>#include <QFile>#include <QDebug>class ThreadHttpDownload : public QThread{ Q_OBJECT public: ThreadHttpDownload(QObject *parent,QString link); void run(); private: QString link; private slots: void replyFinished(QNetworkReply*);};#endif // THREADHTTPDOWNLOAD_H/ ************** threadhttpdownload.h **************** /#include "threadhttpdownload.h"ThreadHttpDownload::ThreadHttpDownload(QObject *parent,QString _link) :QThread(parent){ link = _link;}void ThreadHttpDownload::run(){ // qDebug() << currentThreadId(); QNetworkAccessManager manager; connect(&manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)), Qt::DirectConnection); manager.get(QNetworkRequest(QUrl(link))); exec();}void ThreadHttpDownload::replyFinished(QNetworkReply *reply){ // qDebug() << currentThreadId(); if (reply->error()) { qDebug() << "error download" << reply->errorString(); }else { reply->readAll(); qDebug() << "download complete"; }}
C++ (Qt)141: emit pageContentReady(pageContent, pageAddress);