子类化QApplication在主函数中使用自己子类化的类建立事件循环。那么在自己子类化的QApplication中重新实现notify为什么打印两次内容。
myQApplication.c
#include "myqapplication.h"
#include <QDebug>
myQApplication::myQApplication(int &argc, char **argv):
QApplication(argc, argv)
{
}
bool myQApplication::notify(QObject *obj, QEvent *e)
{
if(e->type() == QEvent::KeyPress)
{
qDebug()<<"myQApplication KeyPress event";
}
return QApplication::notify(obj, e);
}
myQApplication.h
#ifndef MYQAPPLICATION_H
#define MYQAPPLICATION_H
#include <QApplication>
class myQApplication : public QApplication
{
public:
myQApplication(int &argc, char **argv);
public:
bool notify(QObject *, QEvent *);
};
#endif // MYQAPPLICATION_H
main.c
#include "widget.h"
#include "myqapplication.h"
#include <QApplication>
int main(int argc, char *argv[])
{
myQApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
但是打印的时候连续打印了两次。这是为什么呢?
https://blog.csdn.net/u014770862/article/details/72917061?utm_source=blogxgwz1