PDA

View Full Version : DownloadProgress Help



maybnxtseasn
5th March 2012, 02:07
Made a simple little downloader class and im not sure how to implement it so this class can send out downloadprogress signals so my other DIALOG applications that use this class can catch the downloadprogress signals and send them to its slot to handle the signal appropriatly to display a progressbar in action. Below is my class definition


class CDownloadManager : public QObject {
Q_OBJECT

public:
CDownloadManager(QObject *parent = 0) : QObject(parent), m_pNetworkManager(NULL) { };
void Initialize();
~CDownloadManager() { delete m_pNetworkManager; };

public:
QByteArray GetDownloadedData() { return m_DownloadData; }
void DownloadFile(const QString& strUrl);

signals:
void DownloadReady(QByteArray& byteArray);

private slots:
__slot void DownloadFinished(QNetworkReply *pNetReply);

private:
QNetworkAccessManager* m_pNetworkManager;
QByteArray m_DownloadData;
};


Here is my implementation of this class





#include "CDownloadManager.h"


void CDownloadManager::Initialize() {

m_pNetworkManager = new QNetworkAccessManager;

connect(m_pNetworkManager, SIGNAL(finished(QNetworkReply*)), SLOT(DownloadFinished(QNetworkReply*)));
}


void CDownloadManager::DownloadFile(const QString& strUrl) {

QNetworkRequest netRequest(strUrl);
m_pNetworkManager->get(netRequest);
}

__slot void CDownloadManager::DownloadFinished(QNetworkReply *pNetReply) {

// Once download is finished save data
m_DownloadData = pNetReply->readAll();

emit DownloadReady(m_DownloadData);
}




now i have the above class inside my dialog class like so


class Dialog : QObject {

public:
...
...
public slots:
void UpdateDownloadProgress(qint64,qint64)

private:
CDownloadManager m_Downloader;
}


I want my m_Downloader object to emite a downloadprogress(qint64,qint64) signals and catch them within my UpdateDownloadProgress(qint64,qint64) slot for my dialog

ChrisW67
5th March 2012, 02:50
QNetworkReply::downloadProgress() already exists, so define a signal in your class with the same signature as downloadProgress() and connect each reply downloadProgress() signal to the new signal immediately after the get() call:


connect(reply, SIGNAL(downloadProgress(qint64,qint64)), SIGNAL(madeSomeProgress(qint64,qint64)));

The signal will be relayed to the outside world when emitted. Signal to signal connections are mentioned here.

You should also reply->deleteLater() in the finished handler unless you have some other mechanism ensuring the memory is freed.