Qt 事件处理机制 (下篇)
创始人
2024-08-02 08:11:20
0

继续我们上一篇文章继续介绍,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_CALLBACK QtWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 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)   
  8.  
  9. // (续上文Section 7) Section 2-1:     
  10. QT_WIN_CALLBACK QtWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)      
  11. {     
  12.    ...     
  13.    //检查message是否属于Qt可转义的鼠标事件     
  14.    if (qt_is_translatable_mouse_event(message)) {     
  15.         if (QApplication::activePopupWidget() != 0) {                 
  16.             POINT curPos = msg.pt;     
  17.             //取得鼠标点击坐标所在的QWidget指针,它指向我们在main创建的widget实例     
  18.             QWidget* w = QApplication::widgetAt(curPos.x, curPos.y);     
  19.             if (w)     
  20.                 widget = (QETWidget*)w;     
  21.         }     
  22.         if (!qt_tabletChokeMouse) {     
  23.             //对,就在这里。Windows的回调函数将鼠标事件分发回给了Qt Widget      
  24.             // => Section 2-2     
  25.             result = widget->translateMouseEvent(msg);            
  26.      ...     
  27. }     
  28. // Section 2-2  $QTDIR\src\gui\kernel\qapplication_win.cpp     
  29. //该函数所在与Windows平台相关,主要职责就是把已windows格式打包的鼠标事件解包、翻译成QApplication可识别的QMouseEvent,QWidget.     
  30. bool QETWidget::translateMouseEvent(const MSG &msg)     
  31. {     
  32.      //.. 这里很长的代码给以忽略       
  33.       // 让我们看一下sendMouseEvent的声明     
  34.      // widget是事件的接受者; e是封装好的QMouseEvent     
  35.      // ==> Section 2-3     
  36.      res = QApplicationPrivate::sendMouseEvent(widget, &e, alienWidget, this, &qt_button_down, qt_last_mouse_receiver);     
  37. }     
  38. // Section 2-3 $QTDIR\src\gui\kernel\qapplication.cpp     
  39. bool QApplicationPrivate::sendMouseEvent(QWidget *receiver, QMouseEvent *event,     
  40.                                          QWidget *alienWidget, QWidget *nativeWidget,     
  41.                                          QWidget **buttonDown, QPointer &lastMouseReceiver,     
  42.                                          bool spontaneous)     
  43. {     
  44.      //至此与平台相关代码处理完毕     
  45.      //MouseEvent默认的发送方式是spontaneous, 所以将执行sendSpontaneousEvent。 sendSpontaneousEvent() 与 sendEvent的代码实现几乎相同,
  46. 除了将QEvent的属性spontaneous标记不同。 这里是解释什么spontaneous事件:如果事件由应用程序之外产生的,比如一个系统事件。 
  47. 显然MousePress事件是由视窗系统产生的一个的事件(详见上文Section 1~ Section 7),因此它是   spontaneous事件     
  48.        
  49.     if (spontaneous)     
  50.         result = QApplication::sendSpontaneousEvent(receiver, event);  ==〉Section 2-4     
  51.     else    
  52.         result = QApplication::sendEvent(receiver, event);     
  53. }    
  54. // (续上文Section 7) Section 2-1:  
  55. QT_WIN_CALLBACK QtWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)   
  56. {  
  57.    ...  
  58.    //检查message是否属于Qt可转义的鼠标事件  
  59.    if (qt_is_translatable_mouse_event(message)) {  
  60.         if (QApplication::activePopupWidget() != 0) {              
  61.             POINT curPos = msg.pt;  
  62.             //取得鼠标点击坐标所在的QWidget指针,它指向我们在main创建的widget实例  
  63.             QWidget* w = QApplication::widgetAt(curPos.x, curPos.y);  
  64.             if (w)  
  65.                 widget = (QETWidget*)w;  
  66.         }  
  67.         if (!qt_tabletChokeMouse) {  
  68.             //对,就在这里。Windows的回调函数将鼠标事件分发回给了Qt Widget   
  69.             // => Section 2-2  
  70.             result = widget->translateMouseEvent(msg);         
  71.      ...  
  72. }  
  73. // Section 2-2  $QTDIR\src\gui\kernel\qapplication_win.cpp  
  74. //该函数所在与Windows平台相关,主要职责就是把已windows格式打包的鼠标事件解包、翻译成QApplication可识别的QMouseEvent,QWidget.  
  75. bool QETWidget::translateMouseEvent(const MSG &msg)  
  76. {  
  77.      //.. 这里很长的代码给以忽略    
  78.       // 让我们看一下sendMouseEvent的声明  
  79.      // widget是事件的接受者; e是封装好的QMouseEvent  
  80.      // ==> Section 2-3  
  81.      res = QApplicationPrivate::sendMouseEvent(widget, &e, alienWidget, this, &qt_button_down, qt_last_mouse_receiver);  
  82. }  
  83. // Section 2-3 $QTDIR\src\gui\kernel\qapplication.cpp  
  84. bool QApplicationPrivate::sendMouseEvent(QWidget *receiver, QMouseEvent *event,  
  85.                                          QWidget *alienWidget, QWidget *nativeWidget,  
  86.                                          QWidget **buttonDown, QPointer &lastMouseReceiver,  
  87.                                          bool spontaneous)  
  88. {  
  89.      //至此与平台相关代码处理完毕  
  90.      //MouseEvent默认的发送方式是spontaneous, 所以将执行sendSpontaneousEvent。 sendSpontaneousEvent() 与 sendEvent的代码实现几乎相同,
  91. 除了将QEvent的属性spontaneous标记不同。 这里是解释什么spontaneous事件:如果事件由应用程序之外产生的,比如一个系统事件。 
  92. 显然MousePress事件是由视窗系统产生的一个的事件(详见上文Section 1~ Section 7),因此它是spontaneous事件  
  93.     if (spontaneous)  
  94.         result = QApplication::sendSpontaneousEvent(receiver, event);  ==〉Section 2-4  
  95.     else  
  96.         result = QApplication::sendEvent(receiver, event);  

 

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

小结:Qt 事件处理机制 (下篇)的内容介绍完了,希望本文对你 有所帮助!更多相关资料请参考编辑推荐!

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...