Results 1 to 12 of 12

Thread: QMainWindow destroying

  1. #1
    Join Date
    Mar 2013
    Posts
    34
    Thanks
    1

    Default QMainWindow destroying

    Hello,

    I try to study an example from Qt Docs:

    http://qt-project.org/doc/qt-5.0/qtw...ndows-sdi.html

    I added a destructor to its QMainWindow:

    Qt Code:
    1. MainWindow::~MainWindow()
    2. {
    3. qDebug() << "Hello from Destructor!";
    4. }
    To copy to clipboard, switch view to plain text mode 

    and I would have expected that it prints out the message when I close the window as there is:

    Qt Code:
    1. setAttribute(Qt::WA_DeleteOnClose);
    To copy to clipboard, switch view to plain text mode 

    But it does not print anything on window close event. It prints out the message number of times (= number of windows) when all of the windows are closed first.

    Please, can anyone tell me why is this happening? How to make it print out the message on close event (or in the next loop of event loop)?

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QMainWindow destroying

    Yes, destructor is not the same as close event. If you want the message on close event, then put the message in the close event handler!
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Mar 2013
    Posts
    34
    Thanks
    1

    Default Re: QMainWindow destroying

    I apologize for writing it in a bit confusing way.

    When I put the message in overloaded close event handler ("void MainWindow::closeEvent(QCloseEvent *event)") it gets displayed immediately after closing the window. The problem is, when I put the message in destructor instead, it gets displayed first when the whole app quits.

    So my question is: why is the destructor not called immediately after I close the window? And how to make it being called after the window is closed?
    Last edited by ecir.hana; 1st April 2013 at 00:33.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QMainWindow destroying

    When you click the [x] button (or equivalent) you are triggering a close event and (maybe) hiding the window. You are not destroying the QMainWindow object hence the destructor is not called. The QMainWindow object is not destroyed until C++ scope rules dictate (stack allocated objects) or operator delete is called (heap allocated objects).

    If you use the Qt::WA_DeleteOnClose attribute a heap-allocated QWidget object will be destroyed if the object's closeEvent() accepts the closure. It uses the QObject::deleteLater() call, so the deletion is deferred until the program returns to the Qt event loop. This attribute makes no sense if the QWidget object is stack-based (often the case for the program's main window).

  5. #5
    Join Date
    Mar 2013
    Posts
    34
    Thanks
    1

    Default Re: QMainWindow destroying

    Thank you for the reply!

    Yes, I read that clicking [x] button merely hides the window and I thought Qt::WA_DeleteOnClose should destroy the window when the call returns to the event loop. The problem is that it is not happening, I run exactly the same example as linked above from the Qt Docs (I only added that debug destructor). And yes, the windows are allocated on the heap:

    Qt Code:
    1. MainWindow *mainWin = new MainWindow;
    To copy to clipboard, switch view to plain text mode 

    I'm running Mac OS 10.8 and Qt 5.

    I also tried to run the example in Qt 4.8 under Linux and it works. Could this be a bug on Mac? Can someone please confirm it?

    Thank you for the reply!

    Yes, I read that clicking [x] button merely hides the window and I thought Qt::WA_DeleteOnClose should destroy the window when the call returns to the event loop. The problem is that it is not happening, I run exactly the same example as linked above from the Qt Docs (I only added that debug destructor). And yes, the windows are allocated on the heap:

    Qt Code:
    1. MainWindow *mainWin = new MainWindow;
    To copy to clipboard, switch view to plain text mode 

    I'm running Mac OS 10.8 and Qt 5.

    I also tried to run the example in Qt 4.8 under Linux and it works. Could this be a bug on Mac? Can someone please confirm it?


    Added after 7 minutes:


    Please find the attached example bellow.

    SDI.zip
    Last edited by ecir.hana; 1st April 2013 at 10:49.

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QMainWindow destroying

    Try declaring dtor as virtual
    Qt Code:
    1. virtual ~MainWindow();
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  7. #7
    Join Date
    Mar 2013
    Posts
    34
    Thanks
    1

    Default Re: QMainWindow destroying

    Does not help.

  8. #8
    Join Date
    Mar 2013
    Posts
    34
    Thanks
    1

    Default Re: QMainWindow destroying

    But thanks...

  9. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,318
    Thanks
    316
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QMainWindow destroying

    So what happens if you do this in your closeEvent handler?

    Qt Code:
    1. void MainWindow::closeEvent ( QCloseEvent * )
    2. {
    3. deleteLater();
    4. }
    To copy to clipboard, switch view to plain text mode 

    Does the destructor get called once this method exits and control goes back to the event loop? And does your program crash shortly after that?

  10. #10
    Join Date
    Mar 2013
    Posts
    34
    Thanks
    1

    Default Re: QMainWindow destroying

    If I add the code above then yes, the destructor gets called immediately. And if I open several new windows, close one of them and the quit the whole application then yes, the program crashes too.

  11. #11
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,318
    Thanks
    316
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QMainWindow destroying

    Then it sounds like your program logic depends on having the MainWindow alive until the end of the program. If you close and delete it, and your program then crashes, then other parts of the program must be trying to use the MainWindow pointer even though it is destroyed.

    The whole design idea behind QMainWindow is that it serves as the top-level "container" for all other widgets used in the application. Closing the main window implies that the application is shutting down. If you close the main window while trying to keep other windows open and the application running, then this is not the way QMainWindow was intended to be used.

  12. #12
    Join Date
    Mar 2013
    Posts
    34
    Thanks
    1

    Default Re: QMainWindow destroying

    Couple of points:

    - if I don't add the "deleteLater()" as you suggested, it works on Linux but doesn't work on Mac. Wouldn't that imply that the bug is not in the example code?

    - if I add "deleteLater()", and I close and delete the MainWindow and it crashes, couldn't it be because I left WA_DeleteOnClose set to true in closeEvent()?

    - Ad "your program logic" - I took the verbatim copy of Qt oficial documentation I presume it has its logic well thought out. In fact, this is the point of my original question - I followed the documentation and it doesn't work. Why?

Similar Threads

  1. Trouble while destroying thread
    By Ocsidot in forum Qt Programming
    Replies: 9
    Last Post: 10th August 2012, 10:54
  2. Replies: 3
    Last Post: 13th November 2011, 08:12
  3. destroying object...
    By mind_freak in forum General Programming
    Replies: 3
    Last Post: 22nd July 2009, 18:49
  4. Destroying a QList the right way
    By Cruz in forum Newbie
    Replies: 1
    Last Post: 19th January 2009, 10:52
  5. Window not closing or destroying
    By spraff in forum Qt Programming
    Replies: 5
    Last Post: 13th December 2008, 15:10

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.