简体   繁体   中英

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?

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);
            }
        }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM