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:
Code:
#ifndef CLOSETEST_H
#define CLOSETEST_H
#include <QtGui>
public:
};
#endif
Code:
#include "CloseTest.h"
CloseTest
::CloseTest(QWidget *parent
){
layout->addWidget(label);
setLayout(layout);
}
Code:
#include <QApplication>
#include "CloseTest.h"
int main(int argc, char *argv[])
{
QLabel main
("This is the main label");
main.resize(300,150);
main.show();
CloseTest ct;
ct.show();
return app.exec();
}
Re: How to close all windows that were created in main()?
I tried this:
Code:
QObject::connect(&main,
SIGNAL(destroyed
()),
&ct,
SLOT(close
()));
but to no avail.
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:
Code:
#include <QtGui>
int main(int argc, char *argv[])
{
label1->setAttribute(Qt::WA_DeleteOnClose, true);
a.connect(label1, SIGNAL(destroyed()), &a, SLOT(quit()));
label1->show();
label2->setAttribute(Qt::WA_DeleteOnClose, true);
a.connect(label2, SIGNAL(destroyed()), &a, SLOT(quit()));
label2->show();
return a.exec();
}
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...
Re: How to close all windows that were created in main()?
Quote:
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:
Code:
Object *example = new Object;
Anyways it works like you suggested, thank you! :)
Re: How to close all windows that were created in main()?
Quote:
Originally Posted by
Mister_Crac
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.. ;)
Re: How to close all windows that were created in main()?
Quote:
Originally Posted by
Mister_Crac
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.
Re: How to close all windows that were created in main()?
Quote:
Originally Posted by
Matt Smith
Try the static/slot function QApplication::closeAllWindows(). Documentation to be found
here.
Hooray for another possibility! ;)