Time for action – showing the download progress
In order to achieve this, we can use the reply's downloadProgress()
signal. As the first argument, it passes the information on how many bytes have already been received and as the second argument, how many bytes there are in total. This gives us the possibility to indicate the progress of the download with QProgressBar
. As the passed arguments are of the qint64
type, we can't use them directly with QProgressBar
, as it only accepts int
. So, in the connected slot, we first calculate the percentage of the download progress:
void SomeClass::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) { qreal progress = (bytesTotal < 1) ? 1.0 : bytesReceived * 100.0 / bytesTotal; progressBar->setValue(progress * progressBar->maximum()); }
What just happened?
With the percentage, we set the new value for the progress bar where progressBar
is the pointer to this bar. However, what value will progressBar->maximum()
...