Question

should I delete QDialogProgress pointer with delete or not? I have seen that pointer to QDialogProgress allocated on heap hasn't been deleted in some code. Also, should it be placed on stack? Most examples I have seen allocate QDialogProgress on stack.

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;
}
Was it helpful?

Solution

This all depends on how you're using your progress dialog. If you only need it in one scope, then making it a pointer is pointless. But if you do make it a pointer, you can set the Qt::WA_DeleteOnClose flag so it will be deleted automatically when it is closed.

QProgressDialog *dialog = new QProgressDialog;
dialog->setAttribute(Qt::WA_DeleteOnClose);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top