zar
zar

Reputation: 12227

How to make QProgressDialog stay when process function is complete?

I am using QProgressDialog in button click handler but when the process finishes, the dialog auto closes as well. Do I have to create this with pointer of member variabale instead to make it not auto close? Here is jist of my code.

void MainWindow::on_pushButton_clicked()
{
    QProgressDialog progress("Counting files...", "App", 0, 100, this, Qt::Dialog);
    progress.setAutoClose( false );
    progress.setMinimumDuration(0);
    progress.setWindowModality(Qt::WindowModal);
    progress.setModal( true );

    for (int i = 0; i < 100; i++)
    {
        progress.setValue(i);
    }

}

As you can see I am doing anything possibly that would make it modal but it auto closes as soon as the loop finishes. What is the proper way to make it stay when the process function is complete?

Upvotes: 0

Views: 1051

Answers (1)

VP.
VP.

Reputation: 16685

The problem is that your progress object is destroyed at the end of the MainWindow::on_pushButton_clicked() slot. You should define it as class member and show it when you want or create it dynamically.

class MainWindow {
    private:
        QSharedPointer<QProgressDialog> progress;

    public slots:
        void on_pushButton_clicked() {
            progress = QSharedPointer<QProgressDialog>(new QProgressDialog("Counting files...", "App", 0, 100, this, Qt::Dialog));
            progress->setAutoClose( false );
            progress->setMinimumDuration(0);
            progress->setWindowModality(Qt::WindowModal);
            progress->setModal( true );

            for (int i = 0; i < 100; i++)
            {
                progress->setValue(i);
            }
        }
};

Upvotes: 3

Related Questions