我应该用delete
删除 QDialogProgress指针吗?我已经看到在某些代码中没有删除指向堆上分配的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;
}
答案 0 :(得分:3)
这完全取决于您使用进度对话框的方式。如果你只需要在一个范围内,那么使它成为一个指针是毫无意义的。但是如果你确实把它作为一个指针,你可以设置Qt :: WA_DeleteOnClose标志,这样它就会在关闭时自动删除。
QProgressDialog *dialog = new QProgressDialog;
dialog->setAttribute(Qt::WA_DeleteOnClose);