Question:
Specifying a parent with Qt is always seem to be a good idea. However I don’t understand why it is required in some particular cases. There is plenty examples of using QGraphicsView in the following way:
MainWindow::MainWindow()
{
QGraphicsView *view = new QGraphicsView(this);
...
}
So why this is better than QGraphicsView *view = new QGraphicsView(NULL)? in this specific case?
Thank you,
Alex
I’d like to thank UmNyobe and Anthony for helping me to figure it out.
Anyway, I had to look on QT sources to figure out what’s happening behind the curtain.
So I understand the whole QObjects idea as following. When a new QObject is created with a specified parent, the hidden
QObjectPrivate::setParent_helper(QObject *parent)function is called. It indeed registers the child with the parent:So technically speaking, creating a child with a parent allows the parent to register the child. Therefore, specifying a parent when creating a child is a useful habit.
In some cases, the parent-child relation is specified later anyway (see Anthony’s answer), therefore the parent may not be specified.