how to detect if a widget is destroyed?
-
Hello everyone again!
I faced a such situation:
I have widget A that contains a pointer to child widget B and I want widget A to hide when X is clicked (it's by default), and widget B to destroy when its X is clicked (usedB->setAttribute(Qt::WA_DeleteOnClose);
). But I also want widget B to destroy when A is closed.
I can't use the pointer to B, because it can be already destroyed if widget B is closed(destroyed) by the user:void A::closeEvent(QCloseEvent* event) { if (p_B) { delete(p_B); } p_B = nullptr; }
Can I get out of this situation preferably without changing widget B?
-
Hello everyone again!
I faced a such situation:
I have widget A that contains a pointer to child widget B and I want widget A to hide when X is clicked (it's by default), and widget B to destroy when its X is clicked (usedB->setAttribute(Qt::WA_DeleteOnClose);
). But I also want widget B to destroy when A is closed.
I can't use the pointer to B, because it can be already destroyed if widget B is closed(destroyed) by the user:void A::closeEvent(QCloseEvent* event) { if (p_B) { delete(p_B); } p_B = nullptr; }
Can I get out of this situation preferably without changing widget B?
@Atr0p0s You can simply make B a child of A, i.e. pass A as a parent to B constructor or use the
setParent
method. A will delete its child when it is deleted itself.
If you prefer to do it manually for some reason use QPointer instead of a raw pointer. It is automatically nulled when the object it points to is deleted. -
Hello everyone again!
I faced a such situation:
I have widget A that contains a pointer to child widget B and I want widget A to hide when X is clicked (it's by default), and widget B to destroy when its X is clicked (usedB->setAttribute(Qt::WA_DeleteOnClose);
). But I also want widget B to destroy when A is closed.
I can't use the pointer to B, because it can be already destroyed if widget B is closed(destroyed) by the user:void A::closeEvent(QCloseEvent* event) { if (p_B) { delete(p_B); } p_B = nullptr; }
Can I get out of this situation preferably without changing widget B?
@Atr0p0s
void QObject::destroyed(QObject *obj = nullptr).Use
deleteLater()
rather thandelete
.Don't do any of this stuff, just use parentage (assuming you can here): child widget B should have A as its parent.
You shouldn't be in a complex situation like this, nobody else is.
-
Hello everyone again!
I faced a such situation:
I have widget A that contains a pointer to child widget B and I want widget A to hide when X is clicked (it's by default), and widget B to destroy when its X is clicked (usedB->setAttribute(Qt::WA_DeleteOnClose);
). But I also want widget B to destroy when A is closed.
I can't use the pointer to B, because it can be already destroyed if widget B is closed(destroyed) by the user:void A::closeEvent(QCloseEvent* event) { if (p_B) { delete(p_B); } p_B = nullptr; }
Can I get out of this situation preferably without changing widget B?
@Atr0p0s You can simply make B a child of A, i.e. pass A as a parent to B constructor or use the
setParent
method. A will delete its child when it is deleted itself.
If you prefer to do it manually for some reason use QPointer instead of a raw pointer. It is automatically nulled when the object it points to is deleted. -
Hello everyone again!
I faced a such situation:
I have widget A that contains a pointer to child widget B and I want widget A to hide when X is clicked (it's by default), and widget B to destroy when its X is clicked (usedB->setAttribute(Qt::WA_DeleteOnClose);
). But I also want widget B to destroy when A is closed.
I can't use the pointer to B, because it can be already destroyed if widget B is closed(destroyed) by the user:void A::closeEvent(QCloseEvent* event) { if (p_B) { delete(p_B); } p_B = nullptr; }
Can I get out of this situation preferably without changing widget B?
@Atr0p0s said in how to detect if a widget is destroyed?:
if (p_B) { delete(p_B);
On the pedantic side, testing for a nullptr prior to deleting isn't generally necessary.
https://en.cppreference.com/w/cpp/language/delete:If expression evaluates to a null pointer value, no destructors are called, and the deallocation function may or may not be called (it's unspecified), but the default deallocation functions are guaranteed to do nothing when passed a null pointer.
-
@Atr0p0s
void QObject::destroyed(QObject *obj = nullptr).Use
deleteLater()
rather thandelete
.Don't do any of this stuff, just use parentage (assuming you can here): child widget B should have A as its parent.
You shouldn't be in a complex situation like this, nobody else is.
-
@Atr0p0s You can simply make B a child of A, i.e. pass A as a parent to B constructor or use the
setParent
method. A will delete its child when it is deleted itself.
If you prefer to do it manually for some reason use QPointer instead of a raw pointer. It is automatically nulled when the object it points to is deleted.@Chris-Kawa Thank you! QPointer does exactly what I want.
-
A Atr0p0s has marked this topic as solved on
-
@Atr0p0s said in how to detect if a widget is destroyed?:
if (p_B) { delete(p_B);
On the pedantic side, testing for a nullptr prior to deleting isn't generally necessary.
https://en.cppreference.com/w/cpp/language/delete:If expression evaluates to a null pointer value, no destructors are called, and the deallocation function may or may not be called (it's unspecified), but the default deallocation functions are guaranteed to do nothing when passed a null pointer.
-
@JonB parentage isn't enough because I have to delete widget B when A is hidden (closed).
And thank you again! I didn't know aboutdeleteLater()
:)@Atr0p0s said in how to detect if a widget is destroyed?:
@JonB parentage isn't enough because I have to delete widget B when A is hidden (closed).
So you could use the void QObject::destroyed(QObject *obj = nullptr) I referenced you to for detecting if
B
gets destroyed and then setA.p_B
tonullptr
. Which I suspect is just whatQPointer
does. -
@Atr0p0s said in how to detect if a widget is destroyed?:
@JonB parentage isn't enough because I have to delete widget B when A is hidden (closed).
So you could use the void QObject::destroyed(QObject *obj = nullptr) I referenced you to for detecting if
B
gets destroyed and then setA.p_B
tonullptr
. Which I suspect is just whatQPointer
does.