Results 1 to 4 of 4

Thread: deleteLater of Custom Class

  1. #1
    Join Date
    May 2007
    Posts
    110
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X

    Default deleteLater of Custom Class

    Hi

    I am using Qt 4.8.7 on Windows.

    Sample of some code
    Qt Code:
    1. class MyWidgt: public QWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. MyWidgt(QWidget *parent = 0);
    6. ~MyWidget();
    7. private:
    8. QLabel *p2;
    9. };
    10. MyWidgt::MyWidgt(QWidget)
    11. {
    12. p1 = new QPushButton;
    13. p2 = new QPushButton;
    14. }
    15. MyWidgt::~MyWidgt()
    16. {
    17. p1->deleteLater();
    18. p2->deleteLater();
    19. }
    20. int main(int argc, char*argv[])
    21. {
    22. MyWidgt *qq = new MyWidgt;
    23. qq->deleteLater();
    24.  
    25. or delete qq;
    26. }
    To copy to clipboard, switch view to plain text mode 
    Kindly tell me which is the best process...
    qq->deleteLater();
    or delete qq;
    Last edited by anda_skoa; 5th November 2016 at 13:01. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: deleteLater of Custom Class

    Usually p1 and p2 would be children of MyWidget and thus be deleted by MyWidget anyway.

    But if you have objects that need deleting like that, just "delete"

    Cheers,
    _

  3. #3
    Join Date
    May 2007
    Posts
    110
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: deleteLater of Custom Class

    Hi
    If I do not delete p1 and p2 in destructor, simply qq->deleteLater(), can p1 and p2 as a child will automatically deleted, kindly confirm it. I have a lot of widget allocated in constructor, if I do not delete it individually and simply delete class's object using delete later then it would delete all or not. Kindly confirm it. I am confused..

    Thanks
    Santosh

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: deleteLater of Custom Class

    It depends if those other widgets, in your case those buttons, are children of another widget.
    Usually that will be the case, i.e. the buttons are part of a widget, not stand-alone windows.

    In your code snippet above they are not, but then again that is almost certainly not your real code.

    Cheers,
    _

Similar Threads

  1. Replies: 7
    Last Post: 18th August 2011, 14:43
  2. Using custom class as a key (QSet)
    By daujeroti in forum Qt Programming
    Replies: 4
    Last Post: 28th April 2011, 20:39
  3. Custom QwtPlot class
    By Carlton in forum Qwt
    Replies: 8
    Last Post: 6th September 2010, 14:44
  4. qhash with custom class
    By dognzhe in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2009, 06:30
  5. custom class for tslib
    By nrabara in forum Newbie
    Replies: 1
    Last Post: 28th April 2009, 13:15

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.