Results 1 to 5 of 5

Thread: lifetime of foreach

  1. #1
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default lifetime of foreach

    hi,
    just a basic question.
    just to confirm im asking.

    what will be the lifetime of the lineedit ?
    Qt Code:
    1. foreach(QLineEdit* le1,qw->findChildren<QLineEdit*>())
    2. {
    3. le1->clear();
    4. }
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: lifetime of foreach

    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.

  3. The following user says thank you to Lykurg for this useful post:

    BalaQT (4th March 2010)

  4. #3
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: lifetime of foreach

    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,

    Qt Code:
    1. foreach(QLineEdit* le1,qw->findChildren<QLineEdit*>())
    2. {
    3. le1->clear();
    4. delete le1;
    5. }
    To copy to clipboard, switch view to plain text mode 

    Bala
    Last edited by BalaQT; 4th March 2010 at 15:53.

  5. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: lifetime of foreach

    is it right?
    le1 is in the scope of forach,, it wont be accessible after it.

    What exactly do you want to do ?

  6. The following user says thank you to aamer4yu for this useful post:

    BalaQT (4th March 2010)

  7. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: lifetime of foreach

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. The following user says thank you to wysota for this useful post:

    BalaQT (4th March 2010)

Similar Threads

  1. Replies: 1
    Last Post: 27th February 2010, 10:33
  2. Replies: 2
    Last Post: 6th February 2010, 16:31
  3. Foreach performance
    By jano_alex_es in forum General Programming
    Replies: 2
    Last Post: 17th November 2009, 13:26
  4. QTimer lifetime
    By milend in forum Qt Programming
    Replies: 1
    Last Post: 6th August 2008, 20:33
  5. Expected lifetime of QModelIndex?
    By Ishark in forum Qt Programming
    Replies: 1
    Last Post: 15th August 2007, 21:18

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.