#ifndef DOWNLOADITEM_H
#define DOWNLOADITEM_H
#include <QtCore>
#include <QtNetwork/QtNetwork>
#include <QDebug>
#include <QFile>
#include <QtGui>
#include <QString>
class downLoadItem : QMainWindow(QWidget *parent)
{
public:
downLoadItem(QString link);
QFile file;
QNetworkAccessManager *pNetManager;
public slots:
void slotReadyRead();
void slotError(QNetworkReply::NetworkError);
void slotFinished();
};
#endif // DOWNLOADITEM_H
// .cpp
#include <downLoadItem.h>
#include <QtCore>
downLoadItem::downLoadItem
{
pNetManager = new QNetworkAccessManager(this);
QNetworkRequest request;
request.setUrl(QUrl(link));
QNetworkReply *reply = pNetManager->get(request);
connect(reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
this, SLOT(slotError(QNetworkReply::NetworkError)));
connect(reply, SIGNAL(finished()), this, SLOT(slotFinished()));
file.setFileName("lost.avi");
file.open(QIODevice::WriteOnly);
}
void downLoadItem::slotError(QNetworkReply::NetworkError)
{
}
void downLoadItem::slotFinished()
{
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
file.close();
reply->deleteLater();
}
void downLoadItem::slotReadyRead()
{
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
file.write(reply->readAll());
}
Add a code snippet to your website: www.paste.org