bool TForm::listExec(QSqlDatabase db, QStringList &list){ sabQuery query(db); if (db.transaction()) { QProgressDialog pd(tr("Operation in progress."), tr("Cancel"),0,list.size(),this); pd.setWindowModality(Qt::WindowModal); for (int i=0; i < list.size(); i++) { if (!query.exec(list.at(i))) { if (!db.rollback()) { qDebug(qPrintable(db.lastError().text())); } return false; } pd.setValue(i); //qApp->processEvents(); if (pd.wasCanceled()) { if (!db.rollback()) { qDebug(qPrintable(db.lastError().text())); } return false; } } if (!db.commit()) { qDebug(qPrintable(db.lastError().text())); } else { pd.setValue(list.size()); return true; } } return false;}
Using a modal QProgressDialog is simpler for the programmer, but you must call QApplication::processEvents() or QEventLoop::processEvents(ExcludeUserInput) to keep the event loop running to ensure that the application doesn't freeze. Do the operation in a loop, call setValue() at intervals, and check for cancellation with wasCanceled(). For example: QProgressDialog progress("Copying files...", "Abort Copy", 0, numFiles, this); progress.setWindowModality(Qt::WindowModal); for (int i = 0; i < numFiles; i++) { progress.setValue(i); qApp->processEvents(); if (progress.wasCanceled()) break; //... copy one file } progress.setValue(numFiles);