What is parent for in Qt?

后端 未结 2 1156
我在风中等你
我在风中等你 2021-01-01 12:54

Almost every QtWidgets class can have parent. And usually it\'s optional to set parent at object initialization. For example,If I create a class that inherits QWidget<

2条回答
  •  长情又很酷
    2021-01-01 13:38

    Besides helping with draw order in GUI objects, it also helps with memory management, so that when you destroy a QObject, all of it's children are destroyed too. See http://doc.qt.io/qt-4.8/objecttrees.html for more details. When something changes in the parent (e.g. when it is resized), it can notify its children to update themselves too.

    To answer your question, you're not required to set the parent for everything (that's why it's an optional parameter, after all), but most of the time it's better to set it correctly.

提交回复
热议问题