0

In the function - loadData(), First, show the dialog with QProgressBar,and then, call setValue() for the progressbar base on the business. when the value of progressbar increase to 100%, hide the dialog and set the value to 0.

My question is: When I enter the loadData() function again, after exec the dlgo->show(), the value of progressbar isn't start from 0, but jump from 100 to 0, and then go on.

What can I do to make the value of progressbar start from 0 when I tried show the dialog again? Thank you!

void loadData() {
    mProcessBarDlg->show();

    {
      mProcessBarDlg->ui.progressBar->setValue(XX);
    }

    mProcessBarDlg->hide();
    mProcessBarDlg->ui.progressBar->setValue(0);
}
zack chen
  • 137
  • 1
  • 1
  • 10

2 Answers2

0

Set it to zero before you show it.

EDIT: The following code, derived from the poster's question, works:

#include <qapplication.h>
#include <qdialog.h>
#include <qprogressbar.h>

QDialog *mProcessBarDlg;
QProgressBar *progressBar;

void loadData() {
    mProcessBarDlg->setValue(0);
    mProcessBarDlg->show();

    for (int i = 0; i < 100000000; ++i){
        if (i % 100 == 0){
            qApp->processEvents();
        }
    }

    {
      progressBar->setValue(50);
      for (int i = 0; i < 100000000; ++i){
        if (i % 100 == 0){
            qApp->processEvents();
        }
      }
    }

    mProcessBarDlg->hide();
}


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    mProcessBarDlg = new QDialog;
    progressBar = new QProgressBar(mProcessBarDlg);

    loadData();

    for (int i = 0; i < 100000000; ++i){
        if (i % 100 == 0){
            a.processEvents();
        }
    }

    loadData();

    QMetaObject::invokeMethod(&a, "quit", Qt::QueuedConnection);

    return a.exec();
}
RobbieE
  • 4,280
  • 3
  • 22
  • 36
  • Can you please elaborate on the need for qApp->processEvents()? why is it needed? and why "only" every 100 times? Thanks – RanH Jul 16 '17 at 14:31
  • The point of the loops is to cause an artificial delay, simulating some kind of processing. the reason processEvents() is called is to allow the event queue to be processed, otherwise the user interface could freeze up. It's not necessary to do this every iteration of the loop so an interval of 100 was chosen. This was a completely arbitrary number. – RobbieE Jul 17 '17 at 19:57
0

You should set the progressBar value to 0 before showing it.

mProcessBarDlg->ui.progressBar->setValue(0);
mProcessBarDlg->show();
Lucian M.
  • 1
  • 1