C++ (Qt)#include <QtCore/QtGlobal>#include <QtCore/QtDebug>#include <QtCore/QTimer>#include <QApplication>#include <QFile> class MyObject : public QObject{ Q_OBJECTpublic: MyObject(QObject* parent = 0) : QObject(parent) { QObject::connect(qApp, SIGNAL(aboutToQuit()), SLOT(onQuit())); } ~MyObject() { }public slots: void onQuit() { QFile file("temp.txt"); file.open(QIODevice::WriteOnly); file.write("ShutDown"); file.flush(); file.close(); }};int main(int argc, char* argv[]){ QCoreApplication a(argc, argv); MyObject mo; //QTimer::singleShot(1000, qApp, SLOT(quit())); a.exec(); qDebug() << "Shutdown"; return 0;} #include "moc_main.cpp"
bool QApplication::x11EventFilter ( XEvent * event ) [virtual]
C++ (Qt)#ifndef MYAPPLICATION_H#define MYAPPLICATION_H #include <QApplication>#include <QMessageBox>#include <QDebug> #include <qt_windows.h> class MyApplication : public QApplication{public: MyApplication( int argc, char ** argv ) : QApplication( argc, argv ) { } bool winEventFilter(MSG * msg, long * retVal) { // qDebug() << *msg; if (msg->message == WM_QUERYENDSESSION ) { QMessageBox::information( NULL, "Session End","Session End", "OK" ); *retVal = false; return true; } return false; } }; #endif // MYAPPLICATION_H
C++ (Qt)#include <QtGui/QApplication>#include "mainwindow.h"#include "MyApplication.h" int main(int argc, char *argv[]){ MyApplication a(argc, argv); MainWindow w; w.show(); return a.exec();}