一、定義一個c++類:MyApplicationc++
MyApplication.happ
#ifndef MYAPPLICATION_H #define MYAPPLICATION_H #include <QApplication> class MyApplication:public QApplication { public: MyApplication(int argc, char*argv[]):QApplication(argc, argv) { } bool notify(QObject *, QEvent *); }; #endif // MYAPPLICATION_H
MyApplication.cppthis
#include "myapplication.h" #include <QApplication> #include <QEvent> #include <QDebug> bool MyApplication::notify(QObject *object, QEvent *event){ if(this->topLevelWidgets().count()>0) { QWidget* mainWnd = this->topLevelWidgets().at(0); if(object==(QObject*)mainWnd && event->type() == QEvent::Move) { // do ... qDebug() << "mainwnd is clicked"; } } return QApplication::notify(object, event); }
main.cppcode
#include "widget.h" #include "myapplication.h" #include <QApplication> int main(int argc, char *argv[]) { MyApplication a(argc, argv); Widget w; w.show(); return a.exec(); }