From 89f03f85b7bef6a1d9883493efa4325d5cd2bbdb Mon Sep 17 00:00:00 2001 From: xmuli Date: Thu, 8 Jul 2021 01:19:34 +0800 Subject: [PATCH] feat: QtEvent --- QtEvent/QtEvent.pro | 17 +++++++ QtEvent/main.cpp | 107 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 QtEvent/QtEvent.pro create mode 100644 QtEvent/main.cpp diff --git a/QtEvent/QtEvent.pro b/QtEvent/QtEvent.pro new file mode 100644 index 0000000..176abfe --- /dev/null +++ b/QtEvent/QtEvent.pro @@ -0,0 +1,17 @@ +QT -= gui +QT += widgets + +CONFIG += c++11 console +CONFIG -= app_bundle + +# You can make your code fail to compile if it uses deprecated APIs. +# In order to do so, uncomment the following line. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +SOURCES += \ + main.cpp + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target diff --git a/QtEvent/main.cpp b/QtEvent/main.cpp new file mode 100644 index 0000000..2a7c1b9 --- /dev/null +++ b/QtEvent/main.cpp @@ -0,0 +1,107 @@ +//#include +//#include +//#include +//#include +//using namespace std; + +//QEvent::Type t1=(QEvent::Type)1333; +//QEvent e(t1); //使用 QEvent 的构造函数在堆栈上创建自定义事件 + +//class E:public QEvent +//{public: //子类化 QEvent 以创建自定义事件 +// //方式 1:使用静态成员。 +// //使用静态成员主要是为了正确初始化父类部分 QEvent,比如 +// //E():t2((QEvent::Type)1324),QEvent(t2){},若 t2 不是静态的,则则初始化之后 t2 为 1324,但传递 +// //给 QEvent 的 t2 是一个不确定的值,因为按照 C++规则,对父类部分的初始化先于数据成员的初始化。 +// static QEvent::Type t2; //注意:不要使用名称 t,因为 QEvent 类之中有一个名称为 t 的成员变量。 +// E():QEvent(t2){} +// //方式 2:使用带一个参数的构造函数 +// QEvent::Type t3; +// explicit E(QEvent::Type t4):t3(t4),QEvent(t4){} +//}; + +//QEvent::Type E::t2=(QEvent::Type)1334; + +//class A:public QWidget +//{public: +// bool event(QEvent* e){ //重写 event 函数以处理自定义事件 +// if(e->type()==t1) //判断事件类型是否为 t1 +// {cout<<"AE"<type()<<","; +// f1((E*)e); //调用自定义的处理函数处理该事件 +// return 1; } +// if(e->type()==E::t2) {cout<<"BE"<type()<<","; f2((QEvent*)e); return 1; } +// if(e->type()==((E*)e)->t3){cout<<"CE"<type()<<","; f3((E*)e); return 1;} +// return QWidget::event(e); +// } //event 结束 +// //以下为处理自定义事件的事件处理函数 +// void f1(E *e){cout<<"F1"< +#include +#include +#include +using namespace std; +QEvent::Type t1=(QEvent::Type)QEvent::registerEventType(1333); +QEvent e1(t1); //使用 QEvent 的构造函数创建自定义事件 +//t2 的值与 t1 重复,使用 registerEventType 会自动产生一个合法的值 +QEvent::Type t2=(QEvent::Type)QEvent::registerEventType(1333); +QEvent e2(t2); +class A:public QWidget{public: + bool event(QEvent* e){ + if(e->type()==t1) { + cout<<"AE"<type()<<","; + f1((QEvent*)e); + return 1; + } + + if(e->type()==t2) { + cout<<"BE"<type()<<","; + f2((QEvent*)e); + return 1; + } + + return QWidget::event(e); + } //event 结束 + + void f1(QEvent *e){cout<<"F1"<type()==t1){ + cout<<"A"<type()==t2){ + cout<<"B"<