简体   繁体   English

过程功能完成后,如何使QProgressDialog停留?

[英]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. 我在按钮单击处理程序中使用QProgressDialog,但是当过程完成时,对话框也会自动关闭。 Do I have to create this with pointer of member variabale instead to make it not auto close? 我是否必须使用成员variabale的指针来创建此对象,以使其不自动关闭? 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. 问题是您的progress对象在MainWindow::on_pushButton_clicked()插槽的末尾被破坏了。 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);
            }
        }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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