Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set object to nullptr after self deletion

Tags:

c++

qt

I have a dialog window with a class of QDialog. I declare it in the header files with:

MyDialogClass myDialogWindow = nullptr;

and create the window on runtime somewhere with

myDialogWindow = new MyDialogClass(this);

It is also delf destroying, because of it's attributes:

this->setAttribute(Qt::WA_DeleteOnClose);

Does anyone now how can I set the myDialogWindow pointer to nullptr after the self deletion without connection signals?

Thanks in advance.

like image 341
Hoehli Avatar asked Oct 26 '25 01:10

Hoehli


1 Answers

This isn't possible without some signal or event logic. The object does not own the pointer, the pointer is pointing at the object.

However you can use a QPointer which wraps the signal handling and nulling for you.

QPointer<MyDialogClass> myDialogWindow;
myDialogWindow = new MyDialogClass(this);
myDialogWindow->setAttribute(Qt::WA_DeleteOnClose);

// some time later, check dialog has not been closed
if( ! myDialogWindow.isNull() )
{
    // do something with dialog
}
like image 165
David Avatar answered Oct 28 '25 16:10

David



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!