Results 1 to 7 of 7

Thread: How to close all windows that were created in main()?

  1. #1
    Join Date
    Oct 2006
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default How to close all windows that were created in main()?

    If you create more than one window inside of main(), how do destroy all of them when the main window is closed by the user? So he does not have to close all windows manually?

    Code example:
    Qt Code:
    1. #ifndef CLOSETEST_H
    2. #define CLOSETEST_H
    3. #include <QtGui>
    4.  
    5. class CloseTest : public QWidget {
    6.  
    7. public:
    8. CloseTest(QWidget *parent = 0);
    9. };
    10.  
    11. #endif
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "CloseTest.h"
    2.  
    3. CloseTest::CloseTest(QWidget *parent)
    4. {
    5. QHBoxLayout *layout = new QHBoxLayout;
    6. QLabel *label = new QLabel("This is a test");
    7.  
    8. layout->addWidget(label);
    9. setLayout(layout);
    10. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include <QApplication>
    2. #include "CloseTest.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. QLabel main("This is the main label");
    9. main.resize(300,150);
    10. main.show();
    11.  
    12. CloseTest ct;
    13. ct.show();
    14.  
    15. return app.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2006
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to close all windows that were created in main()?

    I tried this:

    Qt Code:
    1. QObject::connect(&main, SIGNAL(destroyed()), &ct, SLOT(close()));
    To copy to clipboard, switch view to plain text mode 

    but to no avail.

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to close all windows that were created in main()?

    You have to set the Qt::WA_DeleteOnClose attribute to true so that the widget gets deleted upon close:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6.  
    7. QLabel* label1 = new QLabel("a");
    8. label1->setAttribute(Qt::WA_DeleteOnClose, true);
    9. a.connect(label1, SIGNAL(destroyed()), &a, SLOT(quit()));
    10. label1->show();
    11.  
    12. QLabel* label2 = new QLabel("b");
    13. label2->setAttribute(Qt::WA_DeleteOnClose, true);
    14. a.connect(label2, SIGNAL(destroyed()), &a, SLOT(quit()));
    15. label2->show();
    16.  
    17. return a.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 

    EDIT: Btw, make sure you allocate widgets on stack if you set Qt::WA_DeleteOnClose attribute! Otherwise you'll get a crash due to double delete...
    J-P Nurmi

  4. #4
    Join Date
    Oct 2006
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to close all windows that were created in main()?

    EDIT: Btw, make sure you allocate widgets on stack if you set Qt::WA_DeleteOnClose attribute! Otherwise you'll get a crash due to double delete...
    On the stack? I thought this allocates an object on the heap:
    Qt Code:
    1. Object *example = new Object;
    To copy to clipboard, switch view to plain text mode 

    Anyways it works like you suggested, thank you!

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to close all windows that were created in main()?

    Quote Originally Posted by Mister_Crac View Post
    On the stack? I thought this allocates an object on the heap:
    Argh, sorry. I was in a hurry when answering. I meant heap while I still wrote stack..
    J-P Nurmi

  6. #6
    Join Date
    Jan 2006
    Location
    New Malden (near London)
    Posts
    32
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: How to close all windows that were created in main()?

    Quote Originally Posted by Mister_Crac View Post
    If you create more than one window inside of main(), how do destroy all of them when the main window is closed by the user? So he does not have to close all windows manually?
    Try the static/slot function QApplication::closeAllWindows(). Documentation to be found here.

  7. #7
    Join Date
    Oct 2006
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to close all windows that were created in main()?

    Quote Originally Posted by Matt Smith View Post
    Try the static/slot function QApplication::closeAllWindows(). Documentation to be found here.
    Hooray for another possibility!

Similar Threads

  1. windows code troubling Qt
    By munna in forum Qt Programming
    Replies: 9
    Last Post: 9th November 2006, 19:52
  2. converting unix exe to windows binary
    By deekayt in forum General Programming
    Replies: 2
    Last Post: 17th September 2006, 01:00
  3. Replies: 5
    Last Post: 4th August 2006, 23:44
  4. Replies: 4
    Last Post: 12th January 2006, 04:16
  5. Qt and windows vista
    By munna in forum General Discussion
    Replies: 8
    Last Post: 11th January 2006, 22:33

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.