#include <QApplication>
#include <QEvent>
#include <QObject>
#include <QDebug>
const QEvent::Type MyEventType = (QEvent::Type)QEvent::registerEventType(QEvent::User+100);
class MyEvent: public QEvent
{
public:
MyEvent(Type MyEventType):QEvent(MyEventType){}
};
class MyObject: public QObject
{
public:
void MyEventHandler(QEvent *event);
bool event(QEvent *event);
};
void MyObject::MyEventHandler(QEvent *event)
{
qDebug()<<"The event is being handled!";
event->accept();
}
bool MyObject::event(QEvent *event)
{
if(event->type() == MyEventType)
{
qDebug()<<"event() is dispathing MyEvent";
MyEventHandler(event);
if((MyEvent*)event->isAccepted())
{
qDebug()<<"The event has been handled!";
return true;
}
}
return QObject::event(event);
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyObject mywindow;
MyEvent myEvent(MyEventType);
a.sendEvent(&mywindow,&myEvent);
return a.exec();
}
运行结果