Results 1 to 12 of 12

Thread: QMainWindow destroying

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3
    Thanked 127 Times in 126 Posts

    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 01:33.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    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).

Similar Threads

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