#include class Operation : public QObject { Q_OBJECT; public: Operation(QObject *parent); public slots: void perform(); void cancel(); private: int steps; QProgressDialog* pd; QTimer* t; }; Operation::Operation(QObject *parent) : QObject(parent) , steps(1) //NOTE 1, and no dialog is ever shown, 0 and it's ok { pd = new QProgressDialog("Operation in progress.", "Cancel",0,100); connect(pd, SIGNAL(canceled()), this, SLOT(cancel())); t = new QTimer(this); connect(t, SIGNAL(timeout()), this, SLOT(perform()));; t->start(100); } void Operation::perform() { pd->setValue(steps); //... perform one percent of the operation steps++; if (steps > pd->maximum()) t->stop(); } void Operation::cancel() { t->stop(); //... cleanup } class MyWidget : public QWidget { Q_OBJECT public: MyWidget(QWidget *parent = 0) : QWidget(parent) { QPushButton *pb = new QPushButton("Start", this); connect(pb, SIGNAL(clicked()), this, SLOT(start())); } public slots: void start() { new Operation(this); } }; #include "main.moc" int main(int argc, char **argv) { QApplication a(argc, argv); MyWidget w; w.show(); return a.exec(); }