0

我是否应该删除QDialogProgress 指针delete?我已经看到在某些代码中没有删除指向堆上分配的 QDialogProgress 的指针。另外,它应该放在堆栈上吗?我见过的大多数示例都在堆栈上分配 QDialogProgress。

int f(void)
{

    //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);
          progress->setAutoClose(true);
    progress->setAutoReset(true);
    progress->setWindowTitle
    (QCoreApplication::translate("title",
                                 "Please wait"));
    progress->setMinimumDuration(1000);

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

    progress->setValue(100);

    //delete progress; ???????????????? is there a leak if not?
    return 500;
}
4

1 回答 1

3

这一切都取决于您如何使用进度对话框。如果您只需要在一个范围内使用它,那么将其设为指针是没有意义的。但是,如果您确实将其设为指针,则可以设置 Qt::WA_DeleteOnClose 标志,以便在关闭时自动将其删除。

QProgressDialog *dialog = new QProgressDialog;
dialog->setAttribute(Qt::WA_DeleteOnClose);
于 2013-07-03T11:44:45.873 回答