浅议Qt的事件处理机制 二上文

我们在上文中,介绍了Qt框架的事件处理机制:事件的产生、分发、接受和处理,并以视窗系统鼠标点击QWidget为例,对代码进行了剖析,向大家分析了Qt框架如何通过Event Loop处理进入处理消息队列循环,如何一步一步委派给平台相关的函数获取、打包用户输入事件交给视窗系统处理,函数调用栈如下:
  1. main(int, char **)
  2. QApplication::exec()
  3. QCoreApplication::exec()
  4. QEventLoop::exec(ProcessEventsFlags)
  5. QEventLoop::processEvents(ProcessEventsFlags)
  6. QEventDispatcherWin32::processEvents(QEventLoop::ProcessEventsFlags)

本文将介绍Qt app在视窗系统回调后,事件又是怎么一步步通过QApplication分发给最终事件的接受和处理者QWidget::event, (QWidget继承Object,重载其虚函数event),以下所有的讨论都将嵌入在源码之中。

  1. QT_WIN_CALLBACKQtWndProc(HWNDhwnd,UINTmessage,WPARAMwParam,LPARAMlParam)bool QETWidget::translateMouseEvent(const MSG &msg)
  2. bool QApplicationPrivate::sendMouseEvent(...)
  3. inline bool QCoreApplication::sendSpontaneousEvent(QObject *receiver, QEvent *event)
  4. bool QCoreApplication::notifyInternal(QObject *receiver, QEvent *event)
  5. bool QApplication::notify(QObject *receiver, QEvent *e)
  6. bool QApplicationPrivate::notify_helper(QObject *receiver, QEvent * e)
  7. bool QWidget::event(QEvent *event)

  1. //(续上文Section7)Section2-1:
  2. QT_WIN_CALLBACKQtWndProc(HWNDhwnd,UINTmessage,WPARAMwParam,LPARAMlParam)
  3. {
  4. ...
  5. //检查message是否属于Qt可转义的鼠标事件
  6. if(qt_is_translatable_mouse_event(message)){
  7. if(QApplication::activePopupWidget()!=0){
  8. POINTcurPos=msg.pt;
  9. //取得鼠标点击坐标所在的QWidget指针,它指向我们在main创建的widget实例
  10. QWidget*w=QApplication::widgetAt(curPos.x,curPos.y);
  11. if(w)
  12. widget=(QETWidget*)w;
  13. }
  14. if(!qt_tabletChokeMouse){
  15. //对,就在这里。Windows的回调函数将鼠标事件分发回给了QtWidget
  16. //=>Section2-2
  17. result=widget->translateMouseEvent(msg);
  18. ...
  19. }
  20. //Section2-2$QTDIR/src/gui/kernel/qapplication_win.cpp
  21. //该函数所在与Windows平台相关,主要职责就是把已windows格式打包的鼠标事件解包、翻译成QApplication可识别的QMouseEvent,QWidget.
  22. boolQETWidget::translateMouseEvent(constMSG&msg)
  23. {
  24. //..这里很长的代码给以忽略
  25. //让我们看一下sendMouseEvent的声明
  26. //widget是事件的接受者;e是封装好的QMouseEvent
  27. //==>Section2-3
  28. res=QApplicationPrivate::sendMouseEvent(widget,&e,alienWidget,this,&qt_button_down,qt_last_mouse_receiver);
  29. }
  30. //Section2-3$QTDIR/src/gui/kernel/qapplication.cpp
  31. boolQApplicationPrivate::sendMouseEvent(QWidget*receiver,QMouseEvent*event,
  32. QWidget*alienWidget,QWidget*nativeWidget,
  33. QWidget**buttonDown,QPointer<QWidget>&lastMouseReceiver,
  34. boolspontaneous)
  35. {
  36. //至此与平台相关代码处理完毕
  37. //MouseEvent默认的发送方式是spontaneous,所以将执行sendSpontaneousEvent。sendSpontaneousEvent()与sendEvent的代码实现几乎相同,除了将QEvent的属性spontaneous标记不同。这里是解释什么spontaneous事件:如果事件由应用程序之外产生的,比如一个系统事件。显然MousePress事件是由视窗系统产生的一个的事件(详见上文Section1~Section7),因此它是
  38. spontaneous事件
  39. if(spontaneous)
  40. result=QApplication::sendSpontaneousEvent(receiver,event);==〉Section2-4
  41. else
  42. result=QApplication::sendEvent(receiver,event);
  43. }

  1. //Section2-4C:/Qt/4.7.1-Vs/src/corelib/kernel/qcoreapplication.h
  2. inlineboolQCoreApplication::sendSpontaneousEvent(QObject*receiver,QEvent*event)
  3. {
  4. //将event标记为自发事件
  5. //进一步调用2-5QCoreApplication::notifyInternal
  6. if(event)event->spont=true;returnself?self->notifyInternal(receiver,event):false;
  7. }
  8. //Section2-5:$QTDIR/gui/kernel/qapplication.cpp
  9. boolQCoreApplication::notifyInternal(QObject*receiver,QEvent*event)
  10. {
  11. //几行代码对于QtJambi(QTJava绑定版本)和QSA(QTScriptforApplication)的支持
  12. ...
  13. //以下代码主要意图为Qt强制事件只能够发送给当前线程里的对象,也就是说receiver->d_func()->threadData应该等于QThreadData::current()。注意,跨线程的事件需要借助EventLoop来派发
  14. QObjectPrivate*d=receiver->d_func();
  15. QThreadData*threadData=d->threadData;
  16. ++threadData->loopLevel;
  17. boolreturnValue;
  18. QT_TRY{
  19. //哇,终于来到大名鼎鼎的函数QCoreApplication::nofity()了==>Section2-6
  20. returnValue=notify(receiver,event);
  21. }QT_CATCH(...){
  22. --threadData->loopLevel;
  23. QT_RETHROW;
  24. }
  25. }
  26. //Section2-6:$QTDIR/gui/kernel/qapplication.cpp
  27. //QCoreApplication::notify和它的重载函数QApplication::notify在Qt的派发过程中起到核心的作用,Qt的官方文档时这样说的:任何线程的任何对象的所有事件在发送时都会调用notify函数。
  28. boolQApplication::notify(QObject*receiver,QEvent*e)
  29. {
  30. //代码很长,最主要的是一个大大的Switch,Case
  31. ..
  32. switch(e->type())
  33. {
  34. ...
  35. caseQEvent::MouseButtonPress:
  36. caseQEvent::MouseButtonRelease:
  37. caseQEvent::MouseButtonDblClick:
  38. caseQEvent::MouseMove:
  39. ...
  40. //让自己私有类(d是私有类的句柄)来进一步处理==>Section2-7
  41. res=d->notify_helper(w,w==receiver?mouse:&me);
  42. e->spont=false;
  43. break;
  44. }
  45. ...
  46. }
  47. //Section2-7:$QTDIR/gui/kernel/qapplication.cpp
  48. boolQApplicationPrivate::notify_helper(QObject*receiver,QEvent*e)
  49. {
  50. ...
  51. //向事件过滤器发送该事件,这里介绍一下EventFilters.事件过滤器是一个接受即将发送给目标对象所有事件的对象。
  52. //如代码所示它开始处理事件在目标对象行动之前。过滤器的QObject::eventFilter()实现被调用,能接受或者丢弃过滤,允许或者拒绝事件的更进一步的处理。如果所有的事件过滤器允许更进一步的事件处理,事件将被发送到目标对象本身。如果他们中的一个停止处理,目标和任何后来的事件过滤器不能看到任何事件。
  53. if(sendThroughObjectEventFilters(receiver,e))
  54. returntrue;
  55. //递交事件给receiver=>Section2-8
  56. boolconsumed=receiver->event(e);
  57. e->spont=false;
  58. }
  59. //Section2-8$QTDIR/gui/kernel/qwidget.cpp
  60. //QApplication通过notify及其私有类notify_helper,将事件最终派发给了QObject的子类-QWidget.
  61. boolQWidget::event(QEvent*event)
  62. {
  63. ...
  64. switch(event->type()){
  65. caseQEvent::MouseButtonPress:
  66. //Don'tresetinputcontexthere.Whetherresetornotis
  67. //aresponsibilityofinputmethod.reset()willbe
  68. //calledbymouseHandler()ofinputmethodifnecessary
  69. //viamousePressEvent()oftextwidgets.
  70. #if0
  71. resetInputContext();
  72. #endif
  73. //mousePressEvent是虚函数,QWidget的子类可以通过重载重新定义mousePress事件的行为
  74. mousePressEvent((QMouseEvent*)event);
  75. break;
  76. }

  1. main(int, char **)
  2. QApplication::exec()
  3. QCoreApplication::exec()
  4. QEventLoop::exec(ProcessEventsFlags)
  5. QEventLoop::processEvents(ProcessEventsFlags)
  6. QEventDispatcherWin32::processEvents(QEventLoop::ProcessEventsFlags)

本文将介绍Qt app在视窗系统回调后,事件又是怎么一步步通过QApplication分发给最终事件的接受和处理者QWidget::event, (QWidget继承Object,重载其虚函数event),以下所有的讨论都将嵌入在源码之中。

  1. QT_WIN_CALLBACKQtWndProc(HWNDhwnd,UINTmessage,WPARAMwParam,LPARAMlParam)bool QETWidget::translateMouseEvent(const MSG &msg)
  2. bool QApplicationPrivate::sendMouseEvent(...)
  3. inline bool QCoreApplication::sendSpontaneousEvent(QObject *receiver, QEvent *event)
  4. bool QCoreApplication::notifyInternal(QObject *receiver, QEvent *event)
  5. bool QApplication::notify(QObject *receiver, QEvent *e)
  6. bool QApplicationPrivate::notify_helper(QObject *receiver, QEvent * e)
  7. bool QWidget::event(QEvent *event)

  1. //(续上文Section7)Section2-1:
  2. QT_WIN_CALLBACKQtWndProc(HWNDhwnd,UINTmessage,WPARAMwParam,LPARAMlParam)
  3. {
  4. ...
  5. //检查message是否属于Qt可转义的鼠标事件
  6. if(qt_is_translatable_mouse_event(message)){
  7. if(QApplication::activePopupWidget()!=0){
  8. POINTcurPos=msg.pt;
  9. //取得鼠标点击坐标所在的QWidget指针,它指向我们在main创建的widget实例
  10. QWidget*w=QApplication::widgetAt(curPos.x,curPos.y);
  11. if(w)
  12. widget=(QETWidget*)w;
  13. }
  14. if(!qt_tabletChokeMouse){
  15. //对,就在这里。Windows的回调函数将鼠标事件分发回给了QtWidget
  16. //=>Section2-2
  17. result=widget->translateMouseEvent(msg);
  18. ...
  19. }
  20. //Section2-2$QTDIR/src/gui/kernel/qapplication_win.cpp
  21. //该函数所在与Windows平台相关,主要职责就是把已windows格式打包的鼠标事件解包、翻译成QApplication可识别的QMouseEvent,QWidget.
  22. boolQETWidget::translateMouseEvent(constMSG&msg)
  23. {
  24. //..这里很长的代码给以忽略
  25. //让我们看一下sendMouseEvent的声明
  26. //widget是事件的接受者;e是封装好的QMouseEvent
  27. //==>Section2-3
  28. res=QApplicationPrivate::sendMouseEvent(widget,&e,alienWidget,this,&qt_button_down,qt_last_mouse_receiver);
  29. }
  30. //Section2-3$QTDIR/src/gui/kernel/qapplication.cpp
  31. boolQApplicationPrivate::sendMouseEvent(QWidget*receiver,QMouseEvent*event,
  32. QWidget*alienWidget,QWidget*nativeWidget,
  33. QWidget**buttonDown,QPointer<QWidget>&lastMouseReceiver,
  34. boolspontaneous)
  35. {
  36. //至此与平台相关代码处理完毕
  37. //MouseEvent默认的发送方式是spontaneous,所以将执行sendSpontaneousEvent。sendSpontaneousEvent()与sendEvent的代码实现几乎相同,除了将QEvent的属性spontaneous标记不同。这里是解释什么spontaneous事件:如果事件由应用程序之外产生的,比如一个系统事件。显然MousePress事件是由视窗系统产生的一个的事件(详见上文Section1~Section7),因此它是
  38. spontaneous事件
  39. if(spontaneous)
  40. result=QApplication::sendSpontaneousEvent(receiver,event);==〉Section2-4
  41. else
  42. result=QApplication::sendEvent(receiver,event);
  43. }

  1. //Section2-4C:/Qt/4.7.1-Vs/src/corelib/kernel/qcoreapplication.h
  2. inlineboolQCoreApplication::sendSpontaneousEvent(QObject*receiver,QEvent*event)
  3. {
  4. //将event标记为自发事件
  5. //进一步调用2-5QCoreApplication::notifyInternal
  6. if(event)event->spont=true;returnself?self->notifyInternal(receiver,event):false;
  7. }
  8. //Section2-5:$QTDIR/gui/kernel/qapplication.cpp
  9. boolQCoreApplication::notifyInternal(QObject*receiver,QEvent*event)
  10. {
  11. //几行代码对于QtJambi(QTJava绑定版本)和QSA(QTScriptforApplication)的支持
  12. ...
  13. //以下代码主要意图为Qt强制事件只能够发送给当前线程里的对象,也就是说receiver->d_func()->threadData应该等于QThreadData::current()。注意,跨线程的事件需要借助EventLoop来派发
  14. QObjectPrivate*d=receiver->d_func();
  15. QThreadData*threadData=d->threadData;
  16. ++threadData->loopLevel;
  17. boolreturnValue;
  18. QT_TRY{
  19. //哇,终于来到大名鼎鼎的函数QCoreApplication::nofity()了==>Section2-6
  20. returnValue=notify(receiver,event);
  21. }QT_CATCH(...){
  22. --threadData->loopLevel;
  23. QT_RETHROW;
  24. }
  25. }
  26. //Section2-6:$QTDIR/gui/kernel/qapplication.cpp
  27. //QCoreApplication::notify和它的重载函数QApplication::notify在Qt的派发过程中起到核心的作用,Qt的官方文档时这样说的:任何线程的任何对象的所有事件在发送时都会调用notify函数。
  28. boolQApplication::notify(QObject*receiver,QEvent*e)
  29. {
  30. //代码很长,最主要的是一个大大的Switch,Case
  31. ..
  32. switch(e->type())
  33. {
  34. ...
  35. caseQEvent::MouseButtonPress:
  36. caseQEvent::MouseButtonRelease:
  37. caseQEvent::MouseButtonDblClick:
  38. caseQEvent::MouseMove:
  39. ...
  40. //让自己私有类(d是私有类的句柄)来进一步处理==>Section2-7
  41. res=d->notify_helper(w,w==receiver?mouse:&me);
  42. e->spont=false;
  43. break;
  44. }
  45. ...
  46. }
  47. //Section2-7:$QTDIR/gui/kernel/qapplication.cpp
  48. boolQApplicationPrivate::notify_helper(QObject*receiver,QEvent*e)
  49. {
  50. ...
  51. //向事件过滤器发送该事件,这里介绍一下EventFilters.事件过滤器是一个接受即将发送给目标对象所有事件的对象。
  52. //如代码所示它开始处理事件在目标对象行动之前。过滤器的QObject::eventFilter()实现被调用,能接受或者丢弃过滤,允许或者拒绝事件的更进一步的处理。如果所有的事件过滤器允许更进一步的事件处理,事件将被发送到目标对象本身。如果他们中的一个停止处理,目标和任何后来的事件过滤器不能看到任何事件。
  53. if(sendThroughObjectEventFilters(receiver,e))
  54. returntrue;
  55. //递交事件给receiver=>Section2-8
  56. boolconsumed=receiver->event(e);
  57. e->spont=false;
  58. }
  59. //Section2-8$QTDIR/gui/kernel/qwidget.cpp
  60. //QApplication通过notify及其私有类notify_helper,将事件最终派发给了QObject的子类-QWidget.
  61. boolQWidget::event(QEvent*event)
  62. {
  63. ...
  64. switch(event->type()){
  65. caseQEvent::MouseButtonPress:
  66. //Don'tresetinputcontexthere.Whetherresetornotis
  67. //aresponsibilityofinputmethod.reset()willbe
  68. //calledbymouseHandler()ofinputmethodifnecessary
  69. //viamousePressEvent()oftextwidgets.
  70. #if0
  71. resetInputContext();
  72. #endif
  73. //mousePressEvent是虚函数,QWidget的子类可以通过重载重新定义mousePress事件的行为
  74. mousePressEvent((QMouseEvent*)event);
  75. break;
  76. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值