[solved!]What's the proper way to delete/null a pointer to another dialog that has closed?
-
So I have a mainwindow class, all it has right now is a button. This button opens another dialog, and sets a member pointer in mainwindow to that new dialog. The second dialog has a button that sends a signal to close itself. So the window is gone but my pointer in mainwindow is still pointing to it, it doesn't know it should delete/null the pointer.
@
class AnotherSubDialog;
class MainWindow
{
//stuff
AnotherSubDialog* mpSubDialog; // this object will call close() when it's "close" button is clicked. However the mainwindow class is not aware of this and doesn't delete/null the pointer.
};
@What is the proper way to handle this behavior? I would image this kind of thing is done all the time.
-
You can have the dialog delete itself by setting the Qt::WA_DeleteOnClose attribute.
You can use a QPointer instead of a standard pointer to have it clear itself when the memory its pointing to is deleted.Alternately, you can create a slot in your MainWindow and connect the mpSubDialog finished(int) signal to that slot, then take care of deleting the pointer there.
-
You could always connect the signal emitted when your dialog closes to it's "deleteLater() slot": http://qt-project.org/doc/qt-4.8/qobject.html#deleteLater
-
[quote author="Noah" date="1347989118"]You can have the dialog delete itself by setting the Qt::WA_DeleteOnClose attribute.
You can use a QPointer instead of a standard pointer to have it clear itself when the memory its pointing to is deleted.Alternately, you can create a slot in your MainWindow and connect the mpSubDialog finished(int) signal to that slot, then take care of deleting the pointer there.[/quote]
I did the signal slot method you mentioned. When I had the slot delete the sub dialog pointer it would crash the app. When I only nulled the pointer it works fine. Is it crashing cause I am deleting the dialog for a second time? Meaning the sub dialog is deleting itself and any associated things I have added?
-
[quote author="pwnstar23" date="1347992140"]
I did the signal slot method you mentioned. When I had the slot delete the sub dialog pointer it would crash the app. When I only nulled the pointer it works fine. Is it crashing cause I am deleting the dialog for a second time? Meaning the sub dialog is deleting itself and any associated things I have added? [/quote]It's crashing because you're deleting the object emiting the signal. You should call mpSubDialog->deleteLater() instead, as Terence suggested.
-
If I use a QPointer then can I get away with just making the SubWindows finished signal, link to a custom slot that has deletelater() in it?
@
mpSubWindow = new SubWindow(this);
connect( mpSubWindow, SIGNAL( finished(int) ), mpSubWindow, SLOT( DeleteSlot( int )));
mpSubWindow->show();private slots:
void DeleteSlot(int value)
{
this->deleteLater();
}
@Or is there a better place for the deleteLater() call to be located?
-
[quote author="pwnstar23" date="1348004498"]If I use a QPointer then can I get away with just making the SubWindows finished signal, link to a custom slot that has deletelater() in it?
[/quote]You certainly can. In fact, you can connect it directly, since you don't need a perfect match in the parameters from the signal and slot you're connecting. Of course if there's anything else you want to do when the dialog closes your method is better.
@connect(mpSubWindow, SIGNAL(finished(int)), mpSubWindow, SLOT(deleteLater()))@
-
Noah is right. Though, if there is anything else you would like to do on finish, you could also simply use two connects. I usually prefer that: have one function that does one thing, rather than one function that reacts to one signal.
Note that instead of using the deleteLater, you could also set the delete-on-close flag of the window and have Qt take care of deleting it. You do that by calling
@
mpSubWindow->setAttribute(Qt::WA_DeleteOnClose, true);
@
It doesn't matter which you use really. -
[quote author="Andre" date="1348034642"]
Note that instead of using the deleteLater, you could also set the delete-on-close flag of the window and have Qt take care of deleting it. You do that by calling
@
mpSubWindow->setAttribute(Qt::WA_DeleteOnClose, true);
@
It doesn't matter which you use really.
[/quote]This seems like the simplest, less error prone way of doing it after reading all the responses.
Thanks guys, this thread is now solved!
Note for future people: mpSubWindow is a QPointer<SubWindowClass>.