
Originally Posted by
d_stranz
And IMO, a "member variable" can be a value that is stack-allocated during construction, a reference assigned during construction, or a pointer to a heap-allocated instance created and assigned at any time during the lifetime of an object instance. You imply that it is only the first type. If so, then what do you call a pointer variable that is defined as a member of a class if not a "member variable"?
I didn't say anything like that. I said the pointer is a member variable and not the object it points to. If you have "int *x" then the variable which is a memeber of the object that contains it is "x" and not "*x".
Nomenclature aside, if I create a pointer to a QObject type using another QObject instance as its parent, store that pointer in a pointer member variable of that class, then delete that pointer in my class destructor, why doesn't that cause a memory fault?
Because QObject destructor is called after your destructor and the object you delete has time to detach itself from its parent and thus is not deleted again when the parent is deleted. The code is equivalent to:
if(parent()) parent()->children().remove(this); // of course ignoring that children() returns a copy
m_parent = newParent;
if(m_parent) m_parent->children().append(this); // of course ignoring that children() returns a copy
}
QObject::~QObject() { qDeleteAll(children); setParent(0); }
void QObject::setParent(QObject *newParent) {
if(parent()) parent()->children().remove(this); // of course ignoring that children() returns a copy
m_parent = newParent;
if(m_parent) m_parent->children().append(this); // of course ignoring that children() returns a copy
}
To copy to clipboard, switch view to plain text mode
If this is true, then is there ever a case in Qt where a call to delete is required, except at the very top of a heap-allocated object hierarchy?
A call to delete is only required if a QObject-derived (and heap allocated) object has no parent (and is not assigned to some kind of smart pointer). I usually have 0 QObject-related "delete" calls in my programs 
Oh... one more thing. Never create QObjects with a parent on the stack (unless they are modal QDialogs which is usually safe). Then you can have double deletions (the behaviour is compiler-dependent).
Added after 8 minutes:

Originally Posted by
d_stranz
This first line creates a QHBoxLayout. The second line assigns it as the parent of glWidget. The third line assigns Window as the parent of mainLayout. So, mainLayout is owned by Window, and glWidget is owned by mainLayout. Each of these children will be deleted automatically when Window goes out of scope.
To be exact:
#include <QtGui>
int main(int argc, char **argv){
l->addWidget(w);
qDebug() << ( w->parent() ? w->parent()->metaObject()->className() : "no parent");
dlg.setLayout(l);
qDebug() << ( w->parent() ? w->parent()->metaObject()->className() : "no parent");
qDebug() << ( l->parent() ? l->parent()->metaObject()->className() : "no parent");
return 0;
}
#include <QtGui>
int main(int argc, char **argv){
QApplication app(argc, argv);
QWidget *w = new QWidget;
QHBoxLayout *l = new QHBoxLayout;
l->addWidget(w);
qDebug() << ( w->parent() ? w->parent()->metaObject()->className() : "no parent");
QDialog dlg;
dlg.setLayout(l);
qDebug() << ( w->parent() ? w->parent()->metaObject()->className() : "no parent");
qDebug() << ( l->parent() ? l->parent()->metaObject()->className() : "no parent");
return 0;
}
To copy to clipboard, switch view to plain text mode
yields:
no parent
QDialog
QDialog
The child widget is reparented to the widget and not to the layout.
Bookmarks