vaibhav
25th May 2011, 07:48
Hi,
I'm trying to maintain strong references to Qt objects in my application.
Given the following code snippet:
// ...
QWidget bar;
QLabel* foo = new QLabel(&bar);
// ...
'bar' now owns 'foo' and is responsible for deleting it. Now I know that the following is valid:
// ...
QWidget bar;
QLabel* foo = new QLabel(&bar);
delete foo;
// ...
Because in 'foo's destructor it removes itself from its parent's to-delete list.
But the problem is that - given a parent's child was not deleted - whenever the parent is destroyed it destroys its children and any external references to the children in the application become dangling pointers.
So what I need is something that tells the parent that there are still external references to the children, so it should not delete them. Since widgets don't inherit from QSharedData, I have no idea how to do this.
Not setting parents is not an option, because in that case, widgets don't appear inside other widgets, since there is no relation between them.
Please help me to find a solution to this.
I'm trying to maintain strong references to Qt objects in my application.
Given the following code snippet:
// ...
QWidget bar;
QLabel* foo = new QLabel(&bar);
// ...
'bar' now owns 'foo' and is responsible for deleting it. Now I know that the following is valid:
// ...
QWidget bar;
QLabel* foo = new QLabel(&bar);
delete foo;
// ...
Because in 'foo's destructor it removes itself from its parent's to-delete list.
But the problem is that - given a parent's child was not deleted - whenever the parent is destroyed it destroys its children and any external references to the children in the application become dangling pointers.
So what I need is something that tells the parent that there are still external references to the children, so it should not delete them. Since widgets don't inherit from QSharedData, I have no idea how to do this.
Not setting parents is not an option, because in that case, widgets don't appear inside other widgets, since there is no relation between them.
Please help me to find a solution to this.