PDA

View Full Version : Creating the QListView instance without a parent



yashaka
3rd February 2010, 13:01
Hi, I found in many Qt-examples, either from the Official Documentation, or from the books, that the QListView (or any other View) can be created dinamically without specifying the parent, like here:


QListView *listView = new QListView;
listView->setModel(&model); // model was created previously on the stack


So the question is: why have it sense?

high_flyer
3rd February 2010, 14:02
The parent defines the context in which a widget is created.
If it has a parent, it will be drwan in its parent context.
If it has no parent. it will become a top level window.
For non visual QObjects, parents will delete their children when they them selves get destroyed, which makes your code safer and cleaner.

yashaka
4th February 2010, 06:54
Thank you! You made me think about this ones more and I found that didn't draw attention to the fact that in all that examples the following code happens a bit further:


hboxLayout->addWidget(listView);
mainWgt.setLayout(hboxLayout);

So listView will be safely deleted when mainWgt will close.