Archived:Create progress dialog using QProgressDialog
This code snippet shows how to use the QProgressDialog class to provide feedback on the progress of a slow operation. Note that the QProgressDialog is similar to QProgressBar, but additionally gives users to opportunity to abort the operation - see How to use QProgressBar in Qt.
int main(int argc, char *argv)
QApplication a(argc, argv);
QVBoxLayout* layout = new QVBoxLayout;
QWidget* win = new QWidget;
//The minimum and maximum is the number of steps in the operation for which this progress dialog shows progress.
//for example here 0 and 100.
QProgressDialog* progress = new QProgressDialog("Fetching data...", "Cancel", 0, 100);
//Set dialog as modal dialog, if you want.
for (int i = 0; i < 100; i++)
//set progress value.
//if user click cancel button of dialog.
Other useful methods of QProgressDialog
Slot cancel() resets the progress dialog. Signal canceled() is emitted when the cancel button is clicked. It is connected to the cancel() slot by default.
The code snippet is expected to show a progress dialog with cancel button, as shown in below image.
- Download the working Code Example.