c++qtpointersparent-child

C++ Qt: How widgets actually contained inside parent?


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?


Solution

  • 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.

    1. a parent always destroys all its children.
    2. 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.