Results 1 to 5 of 5

Thread: destructor not working

  1. #1
    Join Date
    Sep 2014
    Posts
    10
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    3

    Default destructor not working

    Hi.. I have make a simple project and try to understand destructor. but I
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "dialog.h"
    4. #include "QDebug.h"
    5.  
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11.  
    12. qDebug()<<"start MainWindow";
    13.  
    14. }
    15.  
    16. MainWindow::~MainWindow()
    17. {
    18. delete ui;
    19. qDebug()<<"end MainWindow";
    20. }
    21.  
    22. void MainWindow::on_pushButton_clicked()
    23. {
    24. Dialog *dia = new Dialog;
    25. dia->show();
    26. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "dialog.h"
    2. #include "ui_dialog.h"
    3. #include "QDebug.h"
    4.  
    5. Dialog::Dialog(QWidget *parent) :
    6. QDialog(parent),
    7. ui(new Ui::Dialog)
    8. {
    9. ui->setupUi(this);
    10. qDebug()<<"start Dialog";
    11. }
    12.  
    13. Dialog::~Dialog()
    14. {
    15. delete ui;
    16. qDebug()<<"end Dialog";
    17. }
    To copy to clipboard, switch view to plain text mode 

    The problem is that when I close qDialog... it doesnot goes to the ~dialog and print "end dialog"....
    however, when I close mainWindow... goes to the ~mainWindow and print end mainWindow....

    Please tell me how to go to the destructor for dialog and the reason behind it....

    Best Regards,

    Haider

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 284 Times in 279 Posts

    Default Re: destructor not working

    You must call destructor somewhere in code. First option is remeber pointers to Dialog objects in MainWindow and delete them in MainWindow destructor. Second is set MainWindow as parent of Dialog (change line 24 to this) :
    Qt Code:
    1. Dialog *dia = new Dialog(this);
    To copy to clipboard, switch view to plain text mode 
    Third option is add this line in Dialog constructor :
    Qt Code:
    1. setAttribute(Qt::WA_DeleteOnClose);
    To copy to clipboard, switch view to plain text mode 
    Last edited by Lesiok; 28th September 2016 at 14:07.

  3. The following user says thank you to Lesiok for this useful post:

    habbas33 (29th September 2016)

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

    Default Re: destructor not working

    Quote Originally Posted by habbas33 View Post
    The problem is that when I close qDialog... it doesnot goes to the ~dialog and print "end dialog"....
    however, when I close mainWindow... goes to the ~mainWindow and print end mainWindow....
    As Lesiok already explained you are not deleting the dialog.
    You are apparently deleting the main window, probably because it is allocated on the stack in main() and deleted when the scope of that function ends.

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    habbas33 (29th September 2016)

  6. #4
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    520
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 77 Times in 75 Posts

    Default Re: destructor not working

    Hi, you can either pass the mainwindow as a parent to the constructor of Dialog when you create the object (then MainWindow's destructor will take care of destruction), or you have to delete the dialog yourself.

    Ginsengelf

  7. The following user says thank you to Ginsengelf for this useful post:

    habbas33 (29th September 2016)

  8. #5
    Join Date
    Sep 2014
    Posts
    10
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    3

    Default Re: destructor not working

    Thanks it works

Similar Threads

  1. QOpenGLTexture destructor
    By kangaba in forum Qt Programming
    Replies: 2
    Last Post: 29th November 2013, 10:41
  2. Destructor not being called
    By vieraci in forum Qt Programming
    Replies: 12
    Last Post: 25th January 2012, 15:06
  3. Destructor not called
    By satoshi in forum General Programming
    Replies: 2
    Last Post: 23rd April 2010, 13:55
  4. QTemporaryFile and his destructor
    By SABROG in forum Qt Programming
    Replies: 3
    Last Post: 19th May 2009, 19:55
  5. Destructor in QT
    By hgedek in forum Newbie
    Replies: 1
    Last Post: 18th September 2007, 10:54

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.