Results 1 to 6 of 6

Thread: Window not closing or destroying

  1. #1
    Join Date
    Nov 2008
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Window not closing or destroying

    Hello. I have a QMainWindow with a few layers of subwidgets. One of the deeper ones needs to know when the application is closed.

    Its destructor isn't called. Its closeEvent isn't called. I've tried setting WA_DestroyOnClose. (The main window is being closed by clicking the X, but I need to capture all reasons of closing/destroying, which is why I wanted to use the destructor.)

    What's going on?

    Thanks for reading.

  2. #2
    Join Date
    Nov 2008
    Posts
    142
    Thanks
    3
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Window not closing or destroying

    According to the documentation of QWidget::closeEvent() it is only called for top-level widgets. So you have to reimplement closeEvent() in your main window if you want to do something when the window is closed.

  3. #3
    Join Date
    Nov 2008
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Unhappy Re: Window not closing or destroying

    Are you sure?

    "This event handler is called with the given event when Qt receives a window close request for a top-level widget from the window system."

    I read that to mean close events would only be triggered by requesting top-level windows to close, I don't see how that implies only the top-level window receives it.

    Reimplementing the close event all the way down the layers to pass it on won't work in my case, and it's a crappy tactic anyway.

    Qt normally doesn't prevent you from doing intuitive things -- I must be missing something obvious here. At the moment is looks like Qt is breaking RAII by design, which would be the first truly awful feature I've come across if it's true...

  4. #4
    Join Date
    Nov 2008
    Posts
    142
    Thanks
    3
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Window not closing or destroying

    Quote Originally Posted by spraff View Post
    I read that to mean close events would only be triggered by requesting top-level windows to close, I don't see how that implies only the top-level window receives it.
    That's how I understand that part, though I haven't tried it. As your event handler isn't called, this might be the reason. But I might be wrong on this.

    Quote Originally Posted by spraff View Post
    Reimplementing the close event all the way down the layers to pass it on won't work in my case, and it's a crappy tactic anyway.
    I agree that this wouldn't be elegant, but do you have to do so? Can't you just initiate from the mainwindow's handler whatever it is your widget does when it is closed?

  5. #5
    Join Date
    Nov 2008
    Posts
    142
    Thanks
    3
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Window not closing or destroying

    Ok, if have tried it now. It really looks like only the top level window receives the close event, so either my understanding of that part of the documentation is correct or there is something else about the way how widget closing works that I missed. If the latter is the case, I too would be grateful for an explanation.

    BTW, if you explicitly call QWidget::close() on a subwidget, then the closeEvent() of that widget is called. Otherwise it doesn't. So, in the closeEvent() handler of the main window you could close your subwidget explicitly and it should solve your problem.

  6. #6
    Join Date
    Nov 2008
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Window not closing or destroying

    I just tried to get around it with this static object:
    Qt Code:
    1. struct Temp{~Temp();}Temp temp;Temp::~Temp(){
    2. // Find and explicitly destroy all relevant non-top-level windows
    3. }
    To copy to clipboard, switch view to plain text mode 

    Funnily enough, this destructor never got called! I did an strace and found out that Qt ends with an exit_group call. I'm guessing this kills the threads/process without bothering with any clean-up code.

    Easier than being strict about one's resource handling, I suppose, but not exactly kosher.

Similar Threads

  1. Error: BadMatch --> what's it?
    By mattia in forum Newbie
    Replies: 4
    Last Post: 9th June 2008, 12:20
  2. QMain Window Problem
    By maveric in forum Qt Programming
    Replies: 1
    Last Post: 24th May 2008, 15:51
  3. closing of window on pressing ESC key
    By raghvendramisra in forum Newbie
    Replies: 2
    Last Post: 7th January 2008, 02:36
  4. Set a window as child at runtime
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 26th November 2007, 09:30
  5. Change shape of window / animate window
    By sabeesh in forum Qt Programming
    Replies: 3
    Last Post: 31st October 2007, 08:16

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.