The advantage of having widget pointers in headings rather than on real objects is that you do not need to include all the headings for widgets, but simply forward their declarations. This increases compilation time, which can be noticeable in large projects.
In addition, if you have a dialog and you just add many widgets, such as QLabel, you can make the code more accurate by doing this in the implementation: -
layout.addWidget(new QLabel('label1 text')); layout.addWidget(new QLabel('label2 text')); layout.addWidget(new QLabel('label3 text')); layout.addWidget(new QLabel('label4 text'));
As already mentioned, the parent system of Qt will take care of clearing the widgets when their parent is removed.
Of course, if you want to change a property, for example text, you will need to find a widget from layouts, but often QLabel, as its name implies, simply imposes an element and its properties do not change.
source share