PDA

View Full Version : Reparenting a QObject



ghorwin
13th April 2007, 18:14
Hi there,

I created a QObject based class passing another QObject as parent. Now I want to use the QObject somewhere else and need to reparent it. So far I use


// first
MyClass * c1 = new MyClass(containerWidget);
containerWidget->widgetList.append(c1);

// later
MyClass * c2 = containerWidget->widgetList->takeAt(14);
c2->setParent(newContainerWidget);


Is that enough? What happens, if containerWidget gets destroyed before newContainerWidget? Will it delete my class instance as well or does the setParent() call sever the parent-children connection completely?

Bye

marcel
13th April 2007, 18:21
Yes, it will work like this. Once you reparent the object, the old parent won't have anything to do with your object.

But make sure to disconnect any signals/slots you have set between the child and the old parent and connect them to the new parent.