1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
#include <QtGui>
#include <QtNetwork>
class myDownload : public QObject
{
Q_OBJECT
//url du fichier à telecharger
QUrl m_url;
//nom du ficher de destination
QString m_dest;
//une progress bar pour avoir un visualisation de l'avancement
QProgressDialog m_bar;
QHttp m_http;
QFile m_file;
int m_id;
public :
myDownload(const QUrl & url,const QString & text):m_url(url),m_dest (text)
{
m_bar.setWindowModality(Qt::ApplicationModal);
m_bar.show();
m_file.setFileName(text);
m_file.open(QIODevice::WriteOnly);
m_http.setHost(url.host());
m_id =m_http.get(url.path(),&m_file);
connect(&m_http,SIGNAL(requestFinished(int , bool )),this,SLOT(downloadFinish(int)));
connect(&m_http,SIGNAL(dataReadProgress(int,int )),this,SLOT(progress(int, int )));
m_bar.exec();
};
public slots :
void downloadFinish(int id)
{
if (id ==m_id)
{
m_http.close();
m_file.close();
m_bar.close();
}
}
//progression du download
void progress (int bytesReceived, int bytesTotal )
{
m_bar.setValue(100.*bytesReceived/bytesTotal-1);
}
};
#include "main.moc"
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
//donner l'url du fichier
QString url =QInputDialog::getText (0, "url", "url");
//donner le nom du fichier de sortie
QString dest =QInputDialog::getText (0, "dest", "dest");
myDownload download(url,dest);
return 0;
} |
Partager