深入了解事件处理系统对于每个学习Qt人来说非常重要,可以说,Qt是以事件驱动的UI工具集。 大家熟知Signals/Slots在多线程的实现也依赖于Qt的事件处理机制。
在Qt中,事件被封装成一个个对象,所有的事件均继承自抽象类QEvent. 接下来依次谈谈Qt中有谁来产生、分发、接受和处理事件:
1.谁来产生事件: 最容易想到的是我们的输入设备,比如键盘、鼠标产生的
keyPressEvent,keyReleaseEvent,mousePressEvent,mouseReleaseEvent事件(他们被封装成QMouseEvent和QKeyEvent),这些事件来自于底层的操作系统,它们以异步的形式通知Qt事件处理系统,后文会仔细道来。当然Qt自己也会产生很多事件,比如QObject::startTimer()会触发QTimerEvent. 用户的程序可还以自己定制事件。
2.谁来接受和处理事件:答案是QObject。在Qt的内省机制剖析一文已经介绍QObject 类是整个Qt对象模型的心脏,事件处理机制是QObject三大职责(内存管理、内省(intropection)与事件处理制)之一。任何一个想要接受并处理事件的对象均须继承自QObject,可以选择重载QObject::event()函数或事件的处理权转给父类。
3. 谁来负责分发事件:对于non-GUI的Qt程序,是由QCoreApplication负责将QEvent分发给QObject的子类-Receiver. 对于Qt GUI程序,由QApplication来负责。
接下来,将通过对代码的解析来看看QT是利用event loop从事件队列中获取用户输入事件,又是如何将事件转义成QEvents,并分发给相应的QObject处理。
- #include<QApplication>
- #include"widget.h"
-
- intmain(intargc,char*argv[])
- {
- QApplicationapp(argc,argv);
- Widgetwindow;
- window.show();
- returnapp.exec();
- }
-
- intQApplication::exec()
- {
-
-
- returnQCoreApplication::exec();
- }
-
- intQCoreApplication::exec()
- {
-
- QThreadData*threadData=self->d_func()->threadData;
- if(threadData!=QThreadData::current()){
- qWarning("%s::exec:Mustbecalledfromthemainthread",self->metaObject()->className());
- return-1;
- }
-
- if(!threadData->eventLoops.isEmpty()){
- qWarning("QCoreApplication::exec:Theeventloopisalreadyrunning");
- return-1;
- }
- ...
- QEventLoopeventLoop;
- self->d_func()->in_exec=true;
- self->d_func()->aboutToQuitEmitted=false;
-
- intreturnCode=eventLoop.exec();
- ....
- }
- returnreturnCode;
- }
-
- intQEventLoop::exec(ProcessEventsFlagsflags)
- {
-
- Q_D(QEventLoop);
- try{
-
- while(!d->exit)
- processEvents(flags|WaitForMoreEvents|EventLoopExec);
- }catch(...){}
- }
-
- boolQEventLoop::processEvents(ProcessEventsFlagsflags)
- {
- Q_D(QEventLoop);
- if(!d->threadData->eventDispatcher)
- returnfalse;
- if(flags&DeferredDeletion)
- QCoreApplication::sendPostedEvents(0,QEvent::DeferredDelete);
-
- returnd->threadData->eventDispatcher->processEvents(flags);
- }
-
-
-
-
- boolQEventDispatcherWin32::processEvents(QEventLoop::ProcessEventsFlagsflags)
- {
- Q_D(QEventDispatcherWin32);
- if(!d->internalHwnd)
- createInternalHwnd();
- d->interrupt=false;
- emitawake();
- boolcanWait;
- boolretVal=false;
- boolseenWM_QT_SENDPOSTEDEVENTS=false;
- boolneedWM_QT_SENDPOSTEDEVENTS=false;
- do{
- DWORDwaitRet=0;
- HANDLEpHandles[MAXIMUM_WAIT_OBJECTS-1];
- QVarLengthArray<MSG>processedTimers;
- while(!d->interrupt){
- DWORDnCount=d->winEventNotifierList.count();
- Q_ASSERT(nCount<MAXIMUM_WAIT_OBJECTS-1);
- MSGmsg;
- boolhaveMessage;
- if(!(flags&QEventLoop::ExcludeUserInputEvents)&&!d->queuedUserInputEvents.isEmpty()){
-
- haveMessage=true;
-
- msg=d->queuedUserInputEvents.takeFirst();
- }elseif(!(flags&QEventLoop::ExcludeSocketNotifiers)&&!d->queuedSocketEvents.isEmpty()){
-
- haveMessage=true;
- msg=d->queuedSocketEvents.takeFirst();
- }else{
- haveMessage=PeekMessage(&msg,0,0,0,PM_REMOVE);
- if(haveMessage&&(flags&QEventLoop::ExcludeUserInputEvents)
- &&((msg.message>=WM_KEYFIRST
- &&msg.message<=WM_KEYLAST)
- ||(msg.message>=WM_MOUSEFIRST
- &&msg.message<=WM_MOUSELAST)
- ||msg.message==WM_MOUSEWHEEL
- ||msg.message==WM_MOUSEHWHEEL
- ||msg.message==WM_TOUCH
- #ifndefQT_NO_GESTURES
- ||msg.message==WM_GESTURE
- ||msg.message==WM_GESTURENOTIFY
- #endif
- ||msg.message==WM_CLOSE)){
-
- haveMessage=false;
- d->queuedUserInputEvents.append(msg);
- }
- if(haveMessage&&(flags&QEventLoop::ExcludeSocketNotifiers)
- &&(msg.message==WM_QT_SOCKETNOTIFIER&&msg.hwnd==d->internalHwnd)){
-
- haveMessage=false;
- d->queuedSocketEvents.append(msg);
- }
- }
- ....
- if(!filterEvent(&msg)){
- TranslateMessage(&msg);
-
-
- DispatchMessage(&msg);
- }
- }
- }
- }while(canWait);
- ...
- returnretVal;
- }
-
- extern"C"LRESULTQT_WIN_CALLBACKQtWndProc(HWNDhwnd,UINTmessage,WPARAMwParam,LPARAMlParam)
- {
- ...
-
- result=widget->translateMouseEvent(msg);
- ...
- }
-
从Section 1~Section7, Qt进入QApplication的event loop,经过层层委任,最终QEventloop的processEvent将通过与平台相关的QAbstractEventDispatcher的子类QEventDispatcherWin32获得用户的用户输入事件,并将其打包成message后,通过标准Windows API ,把消息传递给了Windows OS,Windows OS得到通知后回调QtWndProc,至此事件的分发与处理完成了一半的路程。
在下文中,我们将进一步讨论当我们收到来在Windows的回调后,事件又是怎么一步步打包成QEvent并通过QApplication分发给最终事件的接受和处理者QObject::event.
在Qt中,事件被封装成一个个对象,所有的事件均继承自抽象类QEvent. 接下来依次谈谈Qt中有谁来产生、分发、接受和处理事件:
1.谁来产生事件: 最容易想到的是我们的输入设备,比如键盘、鼠标产生的
keyPressEvent,keyReleaseEvent,mousePressEvent,mouseReleaseEvent事件(他们被封装成QMouseEvent和QKeyEvent),这些事件来自于底层的操作系统,它们以异步的形式通知Qt事件处理系统,后文会仔细道来。当然Qt自己也会产生很多事件,比如QObject::startTimer()会触发QTimerEvent. 用户的程序可还以自己定制事件。
2.谁来接受和处理事件:答案是QObject。在Qt的内省机制剖析一文已经介绍QObject 类是整个Qt对象模型的心脏,事件处理机制是QObject三大职责(内存管理、内省(intropection)与事件处理制)之一。任何一个想要接受并处理事件的对象均须继承自QObject,可以选择重载QObject::event()函数或事件的处理权转给父类。
3. 谁来负责分发事件:对于non-GUI的Qt程序,是由QCoreApplication负责将QEvent分发给QObject的子类-Receiver. 对于Qt GUI程序,由QApplication来负责。
接下来,将通过对代码的解析来看看QT是利用event loop从事件队列中获取用户输入事件,又是如何将事件转义成QEvents,并分发给相应的QObject处理。
- #include<QApplication>
- #include"widget.h"
-
- intmain(intargc,char*argv[])
- {
- QApplicationapp(argc,argv);
- Widgetwindow;
- window.show();
- returnapp.exec();
- }
-
- intQApplication::exec()
- {
-
-
- returnQCoreApplication::exec();
- }
-
- intQCoreApplication::exec()
- {
-
- QThreadData*threadData=self->d_func()->threadData;
- if(threadData!=QThreadData::current()){
- qWarning("%s::exec:Mustbecalledfromthemainthread",self->metaObject()->className());
- return-1;
- }
-
- if(!threadData->eventLoops.isEmpty()){
- qWarning("QCoreApplication::exec:Theeventloopisalreadyrunning");
- return-1;
- }
- ...
- QEventLoopeventLoop;
- self->d_func()->in_exec=true;
- self->d_func()->aboutToQuitEmitted=false;
-
- intreturnCode=eventLoop.exec();
- ....
- }
- returnreturnCode;
- }
-
- intQEventLoop::exec(ProcessEventsFlagsflags)
- {
-
- Q_D(QEventLoop);
- try{
-
- while(!d->exit)
- processEvents(flags|WaitForMoreEvents|EventLoopExec);
- }catch(...){}
- }
-
- boolQEventLoop::processEvents(ProcessEventsFlagsflags)
- {
- Q_D(QEventLoop);
- if(!d->threadData->eventDispatcher)
- returnfalse;
- if(flags&DeferredDeletion)
- QCoreApplication::sendPostedEvents(0,QEvent::DeferredDelete);
-
- returnd->threadData->eventDispatcher->processEvents(flags);
- }
-
-
-
-
- boolQEventDispatcherWin32::processEvents(QEventLoop::ProcessEventsFlagsflags)
- {
- Q_D(QEventDispatcherWin32);
- if(!d->internalHwnd)
- createInternalHwnd();
- d->interrupt=false;
- emitawake();
- boolcanWait;
- boolretVal=false;
- boolseenWM_QT_SENDPOSTEDEVENTS=false;
- boolneedWM_QT_SENDPOSTEDEVENTS=false;
- do{
- DWORDwaitRet=0;
- HANDLEpHandles[MAXIMUM_WAIT_OBJECTS-1];
- QVarLengthArray<MSG>processedTimers;
- while(!d->interrupt){
- DWORDnCount=d->winEventNotifierList.count();
- Q_ASSERT(nCount<MAXIMUM_WAIT_OBJECTS-1);
- MSGmsg;
- boolhaveMessage;
- if(!(flags&QEventLoop::ExcludeUserInputEvents)&&!d->queuedUserInputEvents.isEmpty()){
-
- haveMessage=true;
-
- msg=d->queuedUserInputEvents.takeFirst();
- }elseif(!(flags&QEventLoop::ExcludeSocketNotifiers)&&!d->queuedSocketEvents.isEmpty()){
-
- haveMessage=true;
- msg=d->queuedSocketEvents.takeFirst();
- }else{
- haveMessage=PeekMessage(&msg,0,0,0,PM_REMOVE);
- if(haveMessage&&(flags&QEventLoop::ExcludeUserInputEvents)
- &&((msg.message>=WM_KEYFIRST
- &&msg.message<=WM_KEYLAST)
- ||(msg.message>=WM_MOUSEFIRST
- &&msg.message<=WM_MOUSELAST)
- ||msg.message==WM_MOUSEWHEEL
- ||msg.message==WM_MOUSEHWHEEL
- ||msg.message==WM_TOUCH
- #ifndefQT_NO_GESTURES
- ||msg.message==WM_GESTURE
- ||msg.message==WM_GESTURENOTIFY
- #endif
- ||msg.message==WM_CLOSE)){
-
- haveMessage=false;
- d->queuedUserInputEvents.append(msg);
- }
- if(haveMessage&&(flags&QEventLoop::ExcludeSocketNotifiers)
- &&(msg.message==WM_QT_SOCKETNOTIFIER&&msg.hwnd==d->internalHwnd)){
-
- haveMessage=false;
- d->queuedSocketEvents.append(msg);
- }
- }
- ....
- if(!filterEvent(&msg)){
- TranslateMessage(&msg);
-
-
- DispatchMessage(&msg);
- }
- }
- }
- }while(canWait);
- ...
- returnretVal;
- }
-
- extern"C"LRESULTQT_WIN_CALLBACKQtWndProc(HWNDhwnd,UINTmessage,WPARAMwParam,LPARAMlParam)
- {
- ...
-
- result=widget->translateMouseEvent(msg);
- ...
- }
-
从Section 1~Section7, Qt进入QApplication的event loop,经过层层委任,最终QEventloop的processEvent将通过与平台相关的QAbstractEventDispatcher的子类QEventDispatcherWin32获得用户的用户输入事件,并将其打包成message后,通过标准Windows API ,把消息传递给了Windows OS,Windows OS得到通知后回调QtWndProc,至此事件的分发与处理完成了一半的路程。
在下文中,我们将进一步讨论当我们收到来在Windows的回调后,事件又是怎么一步步打包成QEvent并通过QApplication分发给最终事件的接受和处理者QObject::event.