0

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?

3
  • 1
    "Does parent widgets have some kind of array with raw pointers to child objects?" - of course. Why would you think a parent wouldn't have that? "Or maybe some kind of smart pointers?" - no, because smart pointers aren't used by Qt to pass around widgets between parents and children. Commented Oct 18, 2024 at 20:26
  • @RemyLebeau nothing prevents Qt from making this an array of unique_ptr, or boost::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 collection Commented Oct 18, 2024 at 20:42
  • 1
    @AhmedAEK it can't use an array of smart pointers because QWidget's constructor takes a raw QWidget* 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. Commented Oct 18, 2024 at 21:23

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.