Results 1 to 8 of 8

Thread: Use of "delete"

  1. #1
    Join Date
    May 2007
    Posts
    37
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Use of "delete"

    When I use things like:

    file.h
    Qt Code:
    1. QLabel *lblUserName;
    To copy to clipboard, switch view to plain text mode 
    file.cpp
    Qt Code:
    1. lblUserName = new QLabel(tr("Mailadres:"));
    To copy to clipboard, switch view to plain text mode 
    Do I have to do this in the .cpp file (e.g. in the destructor):

    Qt Code:
    1. delete lblUserName;
    To copy to clipboard, switch view to plain text mode 
    Or isn't this needed when using Qt/Qt functions/Qt classes?
    Last edited by Voldemort; 2nd May 2007 at 20:45.
    Using Qt version 4.3.0

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Using of "delete"

    Qt doesn't offer any magical all-covering garbage collection but a QObject deletes its children. For more information see:
    J-P Nurmi

  3. #3
    Join Date
    May 2007
    Posts
    37
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Using of "delete"

    1) I use 2 classes, these are 2 Widgets (inheritance), there are all Qt classes (like QAction, QPushButton, ...) in the functions, will they be delete automatically?

    2) But what when I don't inheritance QObject or QWidget between an other class, will I have to delete all the others (like QAction, QPushButton)?
    Using Qt version 4.3.0

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using of "delete"

    1. If you create your classes with a parent that is not created by you explicitly, but by qt ( a main window, etc ), then your objs will be deleted automatically by their parent.
    But if your objs are parentless, besides the fact that layouts and other things won't work properly, you'll have to delete them manually.

    2. Manually, of course ( unless you have some object manager that takes care of them - disposes them as soon as no none uses them anymore ).

    Regards

  5. #5
    Join Date
    May 2007
    Posts
    37
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Using of "delete"

    1) So you mean when my class does not inheritance QObject or QWidget I have to delete them manually?

    2) Can I delete all objects (parent or parentless) without problems (QObject complaining?)? Then I won't make any mistakes .
    Using Qt version 4.3.0

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using of "delete"

    1. Yes. Is that so hard to do? Isn't c/c++ all about this? This is a great thing! More control, more flexibility... . Of course, you can choose not to delete any objects , but then everyone who uses your app will need a lot of memory.

    2. Yes, you can delete them , but be sure to make them NULL right after you delete them.

    Regards

  7. #7
    Join Date
    May 2007
    Posts
    37
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Using of "delete"

    I'll only use delete when the class doesn't inheritance QWidget or QObject. Then everything should be ok.

    @marcel: That's the reason why I program in C++, I like the control an flexibility and C++ is one of the fastest languages.
    Using Qt version 4.3.0

  8. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Use of "delete"

    Very well then

    Regards

Similar Threads

  1. When is the best time to delete a QCanvasItem
    By irudkin in forum Qt Programming
    Replies: 12
    Last Post: 8th March 2007, 21:28
  2. c++, placement delete upon exception
    By stinos in forum General Programming
    Replies: 6
    Last Post: 31st October 2006, 15:38
  3. Delete all members in a QGraphicsItemGroup
    By vmferreira in forum Qt Programming
    Replies: 3
    Last Post: 17th August 2006, 18:47
  4. QListWidget + Delete Key
    By bpetty in forum Newbie
    Replies: 5
    Last Post: 16th August 2006, 20:38
  5. How to explicitely delete a QDir?
    By alan in forum Newbie
    Replies: 2
    Last Post: 13th February 2006, 17:48

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.