January 23, 2012

Ketan Shah Ketan Shah
Lab Rat
95 posts

QProgressBar with QNetworkReply

 

Hi,
I am making an application which downloads zip files from internet and extracts it in a particular folder.
The code below shows a progressbar for downloading the files from internet.
what I want to do is to show a progressbar for both downloading and extracting, at present the progressbar for downloading is shown properly but unable to show it for extracting.
I want to set around 80% for downloading and the remaining 20% for extracting, how can I do this, can any one help me find out a solution for this.

  1. update::update(QWidget *parent) :
  2.     QMainWindow(parent),
  3.     ui(new Ui::update)
  4.  {
  5.  
  6.   bar = new QProgressBar (this);
  7.   bar->setStyle(new QPlastiqueStyle);
  8.   QNetworkRequest request(url);
  9.   currentDownload = manager.get(request);
  10.   connect(currentDownload, SIGNAL(downloadProgress(qint64,qint64)),
  11.             SLOT(downloadProgress(qint64,qint64)));    
  12.   connect(currentDownload, SIGNAL(finished()),
  13.             SLOT(downloadFinished()));
  14.    
  15.  }
  16.  
  17. void update::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
  18. {
  19.     bar->setMaximum(bytesTotal);
  20.     bar->setValue(bytesReceived);
  21.     bar->show();
  22. }

Thanks in advance.

5 replies

January 23, 2012

Lukas Geyer Lukas Geyer
Lab Rat
2074 posts

Why not just set a higher maximum? This way the progress bars end at 80% after the download.

  1. void update::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
  2. {
  3.     bar->setMaximum(bytesTotal + (bytesTotal * 0.25));
  4.     bar->setValue(bytesReceived);
  5.     bar->show();
  6. }

January 23, 2012

Ketan Shah Ketan Shah
Lab Rat
95 posts

Thanks it worked, downloading gets completed on 80%.
Now how can I allot the remaining 20% to unzip process, if you can then please help.

January 23, 2012

Lukas Geyer Lukas Geyer
Lab Rat
2074 posts

Well, just increase the value until you reach the maximum.

  1. class update
  2. {
  3.     ...
  4.  
  5. private:
  6.     int _extractionStep;
  7. }
  8.  
  9. void update::startExtraction()
  10. {
  11.     // intermediateSteps depends on how your extraction works. if your
  12.     // extractionProgress() slot is for example called every percent
  13.     // extracted intermediateSteps will be 100.
  14.  
  15.     _extractionStep = (bar->maximum() - bar->value()) / intermediateSteps;
  16. }
  17.  
  18. void update::extractionProgress()
  19. {
  20.     bar->setValue(bar->()value + _extractionStep);
  21. }

January 25, 2012

Ketan Shah Ketan Shah
Lab Rat
95 posts

Thanks for the help your code is working as expected, but for extracting I am using QProcess::execute, but this makes the GUI and the progressbar inactive.
The GUI and progressbar becomes active after QProcess::execute have finished its work, also I cannot use QProcess::start as it goes in the background and returns the exit code immediately.
The reason why I am using QProcess::execute is that it returns the exit code after completing the work.

Is there any way that can make QProcess::execute and progressbar work simulataneuosly ?
Can I use QThread ? if yes, how ?

Thanks for the help.

January 25, 2012

Lukas Geyer Lukas Geyer
Lab Rat
2074 posts

Use QProcess::start() and connect to the QProcess::finished() [developer.qt.nokia.com] signal, which is emitted as soon as the process has finished and passes the exit code to the slot.

 
  ‹‹ How can we set linespacing in Qtextedit?      QProgressDialog, bug in Qt 4.8.0 or something else ? ››

You must log in to post a reply. Not a member yet? Register here!