Results 1 to 2 of 2

Thread: DownloadProgress Help

  1. #1

    Default DownloadProgress Help

    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

    Qt Code:
    1. class CDownloadManager : public QObject {
    2. Q_OBJECT
    3.  
    4. public:
    5. CDownloadManager(QObject *parent = 0) : QObject(parent), m_pNetworkManager(NULL) { };
    6. void Initialize();
    7. ~CDownloadManager() { delete m_pNetworkManager; };
    8.  
    9. public:
    10. QByteArray GetDownloadedData() { return m_DownloadData; }
    11. void DownloadFile(const QString& strUrl);
    12.  
    13. signals:
    14. void DownloadReady(QByteArray& byteArray);
    15.  
    16. private slots:
    17. __slot void DownloadFinished(QNetworkReply *pNetReply);
    18.  
    19. private:
    20. QNetworkAccessManager* m_pNetworkManager;
    21. QByteArray m_DownloadData;
    22. };
    To copy to clipboard, switch view to plain text mode 

    Here is my implementation of this class




    Qt Code:
    1. #include "CDownloadManager.h"
    2.  
    3.  
    4. void CDownloadManager::Initialize() {
    5.  
    6. m_pNetworkManager = new QNetworkAccessManager;
    7.  
    8. connect(m_pNetworkManager, SIGNAL(finished(QNetworkReply*)), SLOT(DownloadFinished(QNetworkReply*)));
    9. }
    10.  
    11.  
    12. void CDownloadManager::DownloadFile(const QString& strUrl) {
    13.  
    14. QNetworkRequest netRequest(strUrl);
    15. m_pNetworkManager->get(netRequest);
    16. }
    17.  
    18. __slot void CDownloadManager::DownloadFinished(QNetworkReply *pNetReply) {
    19.  
    20. // Once download is finished save data
    21. m_DownloadData = pNetReply->readAll();
    22.  
    23. emit DownloadReady(m_DownloadData);
    24. }
    To copy to clipboard, switch view to plain text mode 



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

    Qt Code:
    1. class Dialog : QObject {
    2.  
    3. public:
    4. ...
    5. ...
    6. public slots:
    7. void UpdateDownloadProgress(qint64,qint64)
    8.  
    9. private:
    10. CDownloadManager m_Downloader;
    11. }
    To copy to clipboard, switch view to plain text mode 


    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
    Last edited by maybnxtseasn; 5th March 2012 at 01:23.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: DownloadProgress Help

    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:
    Qt Code:
    1. connect(reply, SIGNAL(downloadProgress(qint64,qint64)), SIGNAL(madeSomeProgress(qint64,qint64)));
    To copy to clipboard, switch view to plain text mode 
    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.

Similar Threads

  1. Replies: 1
    Last Post: 29th June 2011, 23:07

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.