Results 1 to 2 of 2

Thread: How to declare Objects (A bit out of context ...but very Confusing)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How to declare Objects (A bit out of context ...but very Confusing)

    This is not a Qt question per se, but a C++ question.

    When you declare an object like this:

    QWidget wid;

    It is created on the stack and only has local scope, in other words when you get to the end of the function that declared it, it is destroyed. But normally for GUI elements, you want them to stick around, so you create them instead on the heap with "new" like this:

    QWidget * wid = new QWidget();

    Here you've created a new widget on the heap that will stick around until you specifically delete it, your only reference to it being a pointer variable. You use a pointer in this case because its often more efficient to store a pointer to an object instead of storing the object itself.
    Last edited by JimDaniel; 2nd June 2008 at 15:23.

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.