
Originally Posted by
daemonna
i'm still little bit confused about constructor/destructor working in QT4. Is it only children of QObject that will be automatically destructed??? so anything else i have to write destructors myself??
Only classes that are derived from QObject can be part of a parent-child relationship, and the parent is responsable to delete his own childrens, when the parent goes out of scope (if the parent is created on the stack) or when the parent gets deleted (if the parent was created on the heap and does not have another parent)
Here is a simple Hello world example:
#include <QtGui>
int main(int argc, char** argv) {
w->show();
int ret_val = app.exec();
delete w;
//we must delete 'w' because: 1) we create it on the heap 2) it doesn't have a parent
//we don't delete 'l', because 'w' it's his parent and will take care of deleting 'l' for us
//if you try to delete 'l' you might get some "segmentation fault" errors
return ret_val;
}
#include <QtGui>
int main(int argc, char** argv) {
QApplication app(argc, argv);
QWidget *w = new QWidget(0);
QLabel *l = new QLabel("Hello Qt World!", w);
w->show();
int ret_val = app.exec();
delete w;
//we must delete 'w' because: 1) we create it on the heap 2) it doesn't have a parent
//we don't delete 'l', because 'w' it's his parent and will take care of deleting 'l' for us
//if you try to delete 'l' you might get some "segmentation fault" errors
return ret_val;
}
To copy to clipboard, switch view to plain text mode
Bookmarks