PDA

View Full Version : lifetime of foreach



BalaQT
4th March 2010, 15:35
hi,
just a basic question.
just to confirm im asking.

what will be the lifetime of the lineedit ?

foreach(QLineEdit* le1,qw->findChildren<QLineEdit*>())
{
le1->clear();
}

im using le1->clear() for clearing the lineedit content.

i want to know, le1 will be deleted @ the end of the brace right?
or do i need to delete?
Bala

Lykurg
4th March 2010, 15:40
clear only sets the content to "". The object will life on. Only the pointer to the line edit is not accessible after the loop. If you want delete the object itself, use deleteLater(). or delete.

BalaQT
4th March 2010, 15:49
thank you lykurg,
yeah i know le1->clear only clears the control.

just i want to knw , how le1 will be deleted.

below code is right?

if i put delete before the close brace like below,


foreach(QLineEdit* le1,qw->findChildren<QLineEdit*>())
{
le1->clear();
delete le1;
}


Bala

aamer4yu
4th March 2010, 15:55
is it right?
le1 is in the scope of forach,, it wont be accessible after it.

What exactly do you want to do ?

wysota
4th March 2010, 15:55
This code won't compile.

If you put "delete le1" inside the loop, it would delete the object pointed by le1 so effectively all line edits would vanish from qw.