How Qt internally holds widgets to process and render them? Does parent widgets have some kind of array with raw pointers to child objects? Or maybe some kind of smart pointers?
1 Answer
Each parent holds a vector of raw pointers to children Github QList<QObject*> children;.
since you cannot access it, as it is private, Qt makes sure there is no problem with this list because.
- a parent always destroys all its children.
- a destroyed child always unparents itself, and destroys all connected connections.
If you manually destroy an object with delete it will remove itself from this list, hence no reference count is needed, it is uniquely owned by its parent, and is dropped on external destruction.
Qt is open-source, you can inspect its source-code to answer any question you have.
unique_ptr, orboost::intrusive_ptr, specifically GTK uses an intrusive mechanism and has roughly the same interface as QT with raw pointers, and also Unreal Engine roughly does this with its garbage collectionQWidget's constructor takes a rawQWidget*pointer for the parent, and makes no assumption about its lifetime management, other than a parent must outlive its children. It's dangerous to convert raw pointers into smart pointers when you don't know where the raw pointers are coming from. Qt predates C++11, so changing this mechanism isn't really an option for backwards compatibility.