PDA

View Full Version : Download Progress of the html-content of the webpage



8Observer8
19th June 2014, 07:13
Hello!

I want to watch the download progress of the html-content of the webpage. I use for it the QProgressBar.

But this function receives -1 for bytesTotal:


void Dialog::replyProgress(qint64 bytesReceived, qint64 bytesTotal)
{
ui->progressBar->setValue( bytesReceived );
ui->progressBar->setMaximum( bytesTotal );

qDebug() << bytesReceived;
qDebug() << bytesTotal;
}


This is my module which gets content from the webpage:


#ifndef DOWNLOADER_H
#define DOWNLOADER_H

#include <memory>

#include <QObject>
#include <QString>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QNetWorkAccessManager>

class Downloader : public QObject
{
Q_OBJECT

public:

void fetch( const QString &url )
{
m_reply.reset(m_manager->get( QNetworkRequest( QUrl( url ) ) ) );
connect( m_reply.get( ), SIGNAL( finished( ) ),
this, SLOT( replyFinished( ) ) );
connect( m_reply.get( ), SIGNAL( downloadProgress( qint64, qint64 ) ),
this, SLOT( slotDownloadProgress(qint64, qint64 ) ) );
}

signals:
void signalWithContent( QString * );
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal);

private slots:
void replyFinished( )
{
QByteArray data = m_reply->readAll( );
QString content( data );
emit signalWithContent( &content );
}

void slotDownloadProgress( qint64 bytesReceived, qint64 bytesTotal )
{
emit downloadProgress( bytesReceived, bytesTotal );
}

private:
std::shared_ptr<QNetworkAccessManager> m_manager =
std::make_shared<QNetworkAccessManager>( this );

std::shared_ptr<QNetworkReply> m_reply;
};

#endif // DOWNLOADER_H

yeye_olive
19th June 2014, 09:09
Your question is answered in the documentation of the signal your slot is connected to.