Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove QObject from parent

Tags:

qt

qobject

How can I break parent-child ownership for a QObject? It seems that there is no longer an explicit way of doing this. Is it enough to call

QObject::setParent(NULL)
like image 463
BigONotation Avatar asked Jan 22 '14 17:01

BigONotation


2 Answers

According to the Qt5 Doc

You can also delete child objects yourself, and they will remove themselves from their parents.

like image 77
Charles Sun Avatar answered Oct 14 '22 10:10

Charles Sun


You're correct. To make a QObject an orphan, simply do

// on C++11 compiler
object->setParent(nullptr); 

// on a pre-C++11 compiler
object->setParent(0);
like image 21
Kuba hasn't forgotten Monica Avatar answered Oct 14 '22 11:10

Kuba hasn't forgotten Monica