PDA

View Full Version : simple question on declare



mickey
6th August 2006, 13:47
hi, I declare it inside mainform costructor:


QLabel* label1 = new QLabel(PushButton);
label1->setPixmap(QPixmap::fromMimeSource("end.png"));
//put a label on a pushbutton created in Designer

I wonder this: when the mainForm costructor is executed, and program is out of the scope of mainform costructor, should be label1 destroyed?!?!
If it is so: why I can see the pixmap? But I see the pixmap for all life of program....thanks.

jacek
6th August 2006, 13:51
label1 is a pointer and indeed it gets destroyed when constructor ends, but the object that this pointer points to remains untouched --- since it was allocated using new operator, the programmer is responsible for destroying it (note: if it has a parent, it will be destroyed automatically by Qt).

PS. Please, read some book about C++ programming --- everything will become clear for you.

mickey
6th August 2006, 13:59
thanks; but label1 is deleted, the object pointed isn't deleted (I'm lucky because QT do this for me at the death of parent) but: is this beautiful? can it cause any problems? (not in general but with QT)

jacek
6th August 2006, 14:14
If you don't need to access that label later, you don't have to keep a pointer to it.