PDA

View Full Version : aboutToQuit() signal from QCoreApplication



googie
30th September 2009, 08:26
Is there any possibility if slot connected to aboutToQuit() signal is really called? The cout doesn't work (which is also described in docs). Is there anything I can do to make sure?

wysota
30th September 2009, 09:27
Is there any possibility if slot connected to aboutToQuit() signal is really called?

Why not?


#include <QApplication>
#include <QPushButton>
#include <QtDebug>

class Button : public QPushButton {
Q_OBJECT
public slots:
void doSomething() { qDebug() << "About to quit!"; }
};

#include "main.moc"

int main(int argc, char **argv){
QApplication app(argc, argv);
Button button;
button.setText("Click me");
QObject::connect(&app, SIGNAL(aboutToQuit()), &button, SLOT(doSomething()));
QObject::connect(&button, SIGNAL(clicked()), &app, SLOT(quit()));
button.show();
return app.exec();
}

googie
30th September 2009, 10:11
Does it work for you when you close application from command line by pressing Ctrl+c ?

wysota
30th September 2009, 10:45
It will work if you install an appropriate signal handler. Otherwise the application is instantly terminated by the operating system and doesn't have a chance to do anything.


#include <QApplication>
#include <QPushButton>
#include <QtDebug>
#include <signal.h>


class Button : public QPushButton {
Q_OBJECT
public slots:
void doSomething() { qDebug() << "About to quit!"; }
};

#include "main.moc"

void signalhandler(int sig){
if(sig==SIGINT){
qApp->quit();
}
}

int main(int argc, char **argv){
QApplication app(argc, argv);
Button button;
button.setText("Click me");
QObject::connect(&app, SIGNAL(aboutToQuit()), &button, SLOT(doSomething()));
QObject::connect(&button, SIGNAL(clicked()), &app, SLOT(quit()));
button.show();
signal(SIGINT, signalhandler);
return app.exec();
}