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>
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){
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();
}
#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();
}
To copy to clipboard, switch view to plain text mode
Bookmarks