Solved How to make a progress bar for downloads?
-
@Armin
hi,You don't give us much information here :-)
But I'll just asume, you use QT's Networkaccess Manager to do your downloading:
So I'll post a section from my own project.First: You get the SIGNAL of your Networkreply:
QNetworkReply *reply = NetworkManager->get(downloadRequest); connect(reply, &QNetworkReply::downloadProgress,this, &NetworkAccess::downloadProgress);
Second: In your downloadProgess slot you set the values of your Progressbar:
void NetworkAccess::downloadProgress(qint64 ist, qint64 max) { pBar->setRange(0,max); pBar->setValue(ist); if(max < 0) hideProgress(); }
simple and straight forward :-)
-
@J.Hilk why in first connect it doesn't have argument?
Can you explain more?
Thanks -
-
sure,
connect(reply, &QNetworkReply::downloadProgress,this, &NetworkAccess::downloadProgress);
is written in the new Syntax, introduced a while ago, can't remember the exact Qt 5.x version.
its equivalent to
connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
I prefere the new over the old one. All the times I missed a bracket or added one to much..., I'm glad its now easier, at least for me :-)
-
@J.Hilk what are ist and max arguments ? where they come from?
thnaks -
```
connect(manager , SIGNAL(finished(QNetworkReply*)) , this , SLOT(replyFinished(QNetworkReply*)));
manager->get(QNetworkRequest(QUrl(downloadlink)));void MainWindow::replyFinished(QNetworkReply *reply)
{
connect(reply, &QNetworkReply::downloadProgress,this, &MainWindow:: progressbar);
.
.
.
.Where i did wrong?
-
Hi,
Aren't you connecting the downloadProgress signal once the reply is finished ? By finished it means that the query has completed so you are trying to get the download information after the download already happened.
-
@SGaist Thanks
QNetworkAccessManager *manager = new QNetworkAccessManager(this); QNetworkReply *reply; connect(manager , SIGNAL(downloadProgress(qint64,qint64)) , this , SLOT(progressbar(qint64,qint64))); connect(manager , SIGNAL(finished(QNetworkReply*)) , this , SLOT(replyFinished(QNetworkReply*))); manager->get(QNetworkRequest(QUrl(downloadlink)));
What i do?
-
Meaning ?
-
@SGaist Where i did wrong in that code?
-
Since it's outside any context the only thing that can be said is that
reply
is not used and that you are likely going to have lots of memory eaten if you create a new QNetworkAccessManager per query and not deleting it afterward. -
@Armin
hi,in your sample, not the
QNetworkAccessManager
has thedownloadProgress
Signal, but theQNetworkReply
->
QNetworkAccessManager *manager = new QNetworkAccessManager(this); QNetworkReply *reply = manager->get(QNetworkRequest(QUrl(downloadlink))); connect(reply, SIGNAL(downloadProgress(qint64,qint64)) , this , SLOT(progressbar(qint64,qint64))); connect(manager , SIGNAL(finished(QNetworkReply*)) , this , SLOT(replyFinished(QNetworkReply*))); ``
-
To add to @J-Hilk which got the wrong connection I missed, you should provide more context on how you are using that code in your application.
-
Thanks for all
My problem solved. -
What was the problem ?