show QDialog when called from QMessageBox
-
Hi all
What is the correct method to show and update a QProgressDialog that is shown when I execute a slot from a QMessageBox?
For example I have a QMessageBox that looks like this:
QMessageBox::warning(this,"Logout Warning","All data will be deleted. Continue?",QMessageBox::Yes,QMessageBox::No);
If Yes is clicked, a slot is executed that deletes all data (delete rows from a database).
I want to show the progress in a QDialog with ProgressBar and close the QDialog when removing is finished, but neitherQDialog::show()
norQDialog::open()
do what I expect. It does open and close the dialog, but I it seems the dialog is frozen.If i call
QDialog::exec()
the dialog is shown and i also see the ProgressBar, but its not updated (as expected because of blocking until closed). -
How do you update the progress bar in the dialogue?
-
//in header file QProgressbar *pbar; QDialog *dialog;
//in constructor dialog = new QDialog; pbar = new QProgressbar; pbar->setMinimum(0); QVBoxLayout *v = new QVBoxLayout(dialog); v->addWidget(new QLabel("deleting..."); v->addWidget(pbar); //after clicking QMessageBox::Yes pbar->setMaximum(list.count()); pbar->setValue(0); dialog->show(); // or dialog->open()? or what ever is correct to let me see the content of the dialog ;) //list is a QList<int> foreach(int x,list) { q.bindValue(":id",x); q.exec(); pbar->setValue(pbar->value()++); } dialog->hide();
-
I don't really understand your code.
You show a dialog when the user clicks Yes button, but the progress bar is not part of that dialog?
What is q?
Your foreach loop blocks the event loop, so the progress bar will not be updated before the loop ends. -
pbar is in the VBoxLayout of the dialog, q is a QSqlQuery.
I Just modified the code so that the dialog also contains the yes and no buttons and no QMessageBox is shown. When i now show the QDialog instead of the QMessageBox and then execute the slot, everything looks good and the progressbar is updated at each step of the foreach() loop.