Results 1 to 4 of 4

Thread: Why some QT elements can be created as local and lives outside?

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Why some QT elements can be created as local and lives outside?

    This is a question that I have from time to time.
    In example, the analog clock example shows how you can create a Qtimer object inside analog_clock constructor and it lives until you close the app.
    The question, 'Why I have the impression that I see (somtimes) at Qt examples, objects that needs to be pointer based, created and deleted and some times I see objects created as 'locals' inside a method (and they works fine during the life of the prog..'
    Or Maybe I see this always at constructor and in this case they have a singular treatment ?

    I hope you understand this doubt.

    Thanks.
    Last edited by tonnot; 7th December 2011 at 20:30.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Why some QT elements can be created as local and lives outside?

    Quote Originally Posted by tonnot View Post
    This is a question that I have from time to time.
    In example, the analog clock example shows how you can create a Qtimer object inside analog_clock constructor and it lives until you close the app.
    The question, 'Why I have the impression that I see (somtimes) at Qt examples, objects that needs to be pointer based, created and deleted and some times I see objects created as 'locals' inside a method (and they works fine during the life of the prog..'
    Or Maybe I see this always at constructor and in this case they have a singular treatment ?
    The analog clock example creates a QTimer on the heap. The pointer to that object is a local, not the object itself. The pointer is discarded at the end of the constructor and is no longer available to locate the QTimer object. The object on the heap persists until delete is called on it. The author of the example has used the QObject ownership mechanisms to ensure this QTimer object is deleted at the time the AnalogClock object that created it is deleted.

    To use a non-computing analogy: I give you and another person each a piece of paper with the address of a new house. If you throw away your piece of paper you can no longer find the house but the house continues to exist. The other person can still find the house and can come around later to demolish it.

    In short: You use heap created objects when they must survive outside the scope in which you create them, and stack-based/local objects the remainder of the time. You are responsible for ensuring the heap-based object is deleted by some mechanism.

  3. #3
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Why some QT elements can be created as local and lives outside?

    Hi tonnot,

    in addition to what Chris explained: "pointer vs. local variables" or "creating objects on the heap vs. on the stack" is basic c++ and generally not Qt-related.

    I suggest to use smart pointers for handling pointers. In the "house"-example of chris, I would use a boost::shared_ptr<house>. This type of smart pointer tracks the number of references to the stored object. In the example, the counter would be 2. When the first person throws away its paper, the counter decreases to 1. When the last adress paper gets thrown away, the counter decreaes to zero which automatically destroys the house. This avoids memory leaks.

    I assume, Qt offers some smart pointers, too. But I never used them (besides of QPointer), so I can't tell you details about that.

    Felix

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Why some QT elements can be created as local and lives outside?

    If you use it, like the referenced example does, then the QObject parenting system looks after deletion in the case of heap allocated QObjects and QWidgets. For other objects there are QScopedPointer, QSharedPointer and a few other variations in the Qt arsenal, but boost::shared_ptr and std::tr1::shared_ptr work also.

Similar Threads

  1. Ready to use QML elements
    By .:saeed:. in forum Qt Quick
    Replies: 0
    Last Post: 22nd February 2011, 16:25
  2. Where a QThread lives
    By Althor in forum Newbie
    Replies: 1
    Last Post: 5th July 2010, 09:07
  3. Ui Elements (QLineEdit) + QML
    By NoRulez in forum Qt Programming
    Replies: 1
    Last Post: 3rd April 2010, 16:04
  4. elements not updated
    By danesh in forum Qt Programming
    Replies: 2
    Last Post: 2nd April 2009, 08:35
  5. Elements out of Range!!
    By Kapil in forum Newbie
    Replies: 9
    Last Post: 3rd April 2006, 10:28

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
  •  
Qt is a trademark of The Qt Company.