Construction / destruction order of QT object tree and QObject

Time:2021-3-20

QT uses object tree to organize QObject

When a QObject is created with an object as the parent object, the object will be added to the children() list of the parent object. When the parent object is deleted, the object will also be released.

When an object obj is destructed, two operations are performed:

  • If it has a parent, it is removed from its parent.
  • If it has children, all of its children will be automatically deleted.

Remember, QT is primarily used to develop graphical user interface programs. This method of organizing objects is very suitable for GUI objects,

For example, qshortcut (keyboard shortcut) is a child of the related window. When the user closes the window, the shortcut will also be deleted.

Moreover, QWidget is the basic class of QT widgets module (the basic class of all user interface objects), and it also extends the parent-child relationship of QObject.
A child of a QWidget becomes a child widgetDisplays in the coordinate system of its parentAnd according to itsThe boundaries of the parent are clipped graphically

For example, when an application deletes a message box after it is closed, the buttons and labels of the message box are also deleted, because the buttons and labels are children of the message box.

Build / destroy sequence of QObject

According to the object creation method, it can be divided into two situations to discuss

1. Create on the heap (using new)

When creating qobjects on the heap (that is, creating them with new), you can create objects and construct object trees in any order. You can also destroy objects in the tree in any order.

2. Create on stack

When creating qobjects on the stack, usually the destruction order will not be a problem.
Here’s an example:

int main()
{
    QWidget window;
    QPushButton quit("Quit", &window);
    ...
}

quityeswindowThe child object of. According to the C + + language standard, theContrary to constructorsCall the destructor of the local object in the order of,quitThe destructor of will be called first, and it will remove itself from its parent object. Then,windowThe destructor of is called becausequitFromwindowRemoved from children of,quitThe destructor of is not called twice. Destruction is normal.


It should be noted that this situation:

int main()
{
    QPushButton quit("Quit");
    QWidget window;

    quit.setParent(&window);
    ...
}

According to the order of creation,windowWill be destructed first, and it will also call all of its children’s destructors, that isquitThe destructor of is called at this point. Then,quitThe destructor of the local variable is called again, that is to sayquitThe destructor of was called twice.
In this case, there is a problem with the destruction sequence. Therefore, when encoding, theYou need to pay attention to the order in which objects are createdIt’s best to create them in the order that the father comes first and the son comes second.