Suppose some slot was called by some QDialog based class ,
I create the dialog elsewhere , e.g
MyDialog *dlg = new MyDialog (this);
connect (dlg , SIGNAL(valueSet(QString)) , SLOT(slotGetValue(QString)));
dlg->exec ();
And in the slot , I delete the object by using deleting it’s “deepest” parent class , which is QObject:
void slotGetValue (const QString & key)
{
// process the value we retrieved
// now delete the dialog created
sender()->deletLater ();
}
Is that the correct way of doing this ? Is that safe ?
There should be no reason to delete a dialog that is modal. Since QDialog::exec() blocks, the dialog can be safely deleted immediately after that returns.
From that, you can probably guess there isn’t any need for using new and delete. You can just put it on the stack, and it will be destroyed when leaving scope. Like this:
And unless you need the this pointer in the MyDialog constructor, there’s no reason to pass it.