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;
}
void Dialog::replyProgress(qint64 bytesReceived, qint64 bytesTotal)
{
ui->progressBar->setValue( bytesReceived );
ui->progressBar->setMaximum( bytesTotal );
qDebug() << bytesReceived;
qDebug() << bytesTotal;
}
To copy to clipboard, switch view to plain text mode
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>
{
Q_OBJECT
public:
{
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( )
{
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
#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
To copy to clipboard, switch view to plain text mode
Bookmarks