Results 1 to 5 of 5

Thread: Close All Windows

  1. #1
    Join Date
    Jul 2009
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Close All Windows

    I wrote this to prevent the user from closing the main dialog and leaving other windows open:

    Qt Code:
    1. void Dialog::closeEvent(QCloseEvent *e) {
    2. if (QMessageBox::question(this, tr("Close ALL windows?"),
    3. tr("Close ALL windows?"), QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes)
    4. {
    5. qApp->closeAllWindows();
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    But when I click yes nothing happens and the program is blocked.
    What I do wrong?

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Close All Windows

    I have no means for a quick test at this moment, but try accepting the event.

    Qt Code:
    1. void Dialog::closeEvent(QCloseEvent *e) {
    2. if (QMessageBox::question(this, tr("Close ALL windows?"),
    3. tr("Close ALL windows?"), QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes)
    4. {
    5. qApp->closeAllWindows();
    6. e->accept();
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jul 2009
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Close All Windows

    Thank you. but whether I accept or ignore the event, still don't work.
    I tried to call qApp->quit() instead of qApp->closeAllWindows(), it seems to work, but the destructor of the other windows opened are not called.

    I open this other dialogs like this:
    Qt Code:
    1. DlgTabla *t = new DlgTabla();
    2. t->show();
    To copy to clipboard, switch view to plain text mode 

    A possible solution could be: construct the dialog with parent the main dialog:
    Qt Code:
    1. DlgTabla *t = new DlgTabla(this);
    2. t->show();
    To copy to clipboard, switch view to plain text mode 
    The destructor is called, however all windows I show shares the same element of the main dialog in the task bar.

    There another way to close all windows and to call their destructors?
    Or as alternative solution, show this other dialogs without sharing the same element in the task bar?

    Thanks

  4. #4
    Join Date
    Jan 2010
    Posts
    10
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Close All Windows

    I think the problem comes from the difference between closeEvent end close methods.
    When ever a widget is attempted to be closed, QT calls closeEvent. And if you call another acrion causing close it will recursively call itself

    So you SHOULD NOT call qApp->closeAllWindows in close event. just accept the event.
    And if you want your app closed whenever your widget is closed, write the aboce closing code to close method.

    Happy QTing

  5. #5
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Close All Windows

    The destructor is called, however all windows I show shares the same element of the main dialog in the task bar.

    There another way to close all windows and to call their destructors?
    Or as alternative solution, show this other dialogs without sharing the same element in the task bar?
    If your widgets are properly parented, I dont see need to even handle closeEvent. From what you posted it seems you want to call the destructor of custom widgets before the application or main window closes.
    Am I right in understanding your requirement ?

Similar Threads

  1. [PyQt4] Want to connect a window's close button
    By Dodle in forum Qt Programming
    Replies: 7
    Last Post: 21st April 2010, 11:13
  2. Replies: 5
    Last Post: 15th January 2009, 09:03
  3. Close all windows
    By nowire75 in forum Newbie
    Replies: 4
    Last Post: 30th October 2007, 12:28
  4. Replies: 2
    Last Post: 5th February 2007, 17:42
  5. How to close all windows that were created in main()?
    By Mister_Crac in forum Qt Programming
    Replies: 6
    Last Post: 13th November 2006, 10:57

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.