Results 1 to 5 of 5

Thread: qt smart pointer

  1. #1
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default qt smart pointer

    Hello forum,

    I have declared a class as follows:


    Qt Code:
    1. class Item : public QObject, QGraphicsItem
    2. {
    3.  
    4. private:
    5.  
    6. //the following is turn also a sub-class of QObject
    7. TextGraphicsItem *m_text;
    8.  
    9.  
    10.  
    11. };
    To copy to clipboard, switch view to plain text mode 


    Do i have to delete explicitly m_text in the destrucor or Qt will take care of it to delete the memory?



    Thanks
    Sajjad

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: qt smart pointer

    QObject automatically deletes it's child objects, so if you pass a parent pointer to m_text constructor, it will be deleted if the parent is deleted.

  3. #3
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: qt smart pointer

    It depends:
    If TextGraphicsItem isn't an QObject derived class, or if it doesn't have a parent (parent pointer is 0) you will need to delete it in ~Item().

    If it take this (or other QObject pointer) as a parent you don't need to delete it manually, it will be deleted by the parent.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: qt smart pointer

    So far you just have a pointer so you don't have to delete anything. Once you initialize it with some object we can start talking about it.
    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.


  5. #5
    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: qt smart pointer

    Or use QScopedPointer and we don't need to talk about

Similar Threads

  1. Smart Pointer Design
    By lexfridman in forum Qt Programming
    Replies: 1
    Last Post: 18th January 2011, 01:41
  2. Replies: 1
    Last Post: 4th December 2010, 17:20
  3. smart pointers in Qt and Boost
    By pospiech in forum Qt Programming
    Replies: 0
    Last Post: 18th April 2010, 14:24
  4. Qt 4.6: Which Qt smart pointer to use for this case?
    By ShaChris23 in forum Qt Programming
    Replies: 1
    Last Post: 12th November 2009, 03:11
  5. Smart Code completion , making default
    By udit in forum Qt Tools
    Replies: 1
    Last Post: 14th September 2009, 10:08

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.