0

There is an operation in my application that can take up-to few minutes to finish. I wish to show a simple dialog box which opens when the operation begins, displays a simple wait message like 'Operation in progress..' and closes automatically when the operation finishes.

To implement above functionality, I am trying to make use of a QProgressDialog which does not have a QProgressBar. It seems to work fine but I am unable to set the wait message. Following is the code:

QProgressDialog progress(this);  
QLabel *lblCustom = new QLabel(&progress);  
progress.setBar(new QProgressBar());  
progress.setValue(0);  
progress.setLabel(lblCustom);  
progress.setLabelText("Operation in progress. Please wait...");  
progress.setMaximum(0);  
progress.setMinimum(0);  
progress.setModal(true);  
progress.setWindowTitle("Validate field data");  
progress.setCancelButton(0);  
progress.setFixedSize(400, 100);  
progress.setWindowFlags(progress.windowFlags() & ~Qt::WindowCloseButtonHint);  
progress.show();  
// operation  
progress.close();

Setting label text does not seem to work. Please let me know what is wrong here? I am relatively new to Qt.

Dialog without wait message

Nishant Kumar
  • 363
  • 4
  • 20
  • Why don't you just use the `QLabel` as your message? After all, any widget that is not a child of another will form a popup window. – RobbieE Dec 18 '15 at 07:14
  • Hi @RobbieE, I tried using QLabel to work as a popup. But, here again the message is _not_ appearing :) Can you please check what is wrong ? following is the code: QLabel *lblCustom = new QLabel(); lblCustom->setFixedSize(400, 80); lblCustom->setText("Operation in progress. Please wait..."); lblCustom->show(); // Some Operation .... lblCustom->close(); – Nishant Kumar Dec 18 '15 at 08:11
  • 1
    I assume the problem is that the code at `// operation` blocks the event loop and thus the progress window never gets painted. You should implement lengthy operations asynchronously to avoid blocking the main thread. – Frank Osterfeld Dec 18 '15 at 08:25
  • You're not running the event loop. – Kuba hasn't forgotten Monica Dec 18 '15 at 14:14

2 Answers2

2

To solve the actual problem, I was used QLabel as a dialog instead of QProgressDialog and invoked QApplication::processEvents(), to make sure QLabel window gets painted with suitable text as well.

Nishant Kumar
  • 363
  • 4
  • 20
0

I suggest you create a new widget, then you can customize as you like. You can insert images and labels and you can create your own methods (including signals and slots). It will take a little longer to implement but it will be much more flexible and extensible.

KelvinS
  • 2,870
  • 8
  • 34
  • 67