Qt 事件處理機制 (下篇)

繼續我們上一篇文章繼續介紹,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)
  2. bool QETWidget::translateMouseEvent(const MSG &msg)   
  3. bool QApplicationPrivate::sendMouseEvent(...)   
  4. inline bool QCoreApplication::sendSpontaneousEvent(QObject *receiver, QEvent *event)   
  5. bool QCoreApplication::notifyInternal(QObject *receiver, QEvent *event)   
  6. bool QApplication::notify(QObject *receiver, QEvent *e)   
  7. bool QApplicationPrivate::notify_helper(QObject *receiver, QEvent * e)   
  8. bool QWidget::event(QEvent *event)   
  9.  
  10. // (續上文Section 7) Section 2-1:     
  11. QT_WIN_CALLBACK QtWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)      
  12. {     
  13.    ...     
  14.    //檢查message是否屬於Qt可轉義的鼠標事件     
  15.    if (qt_is_translatable_mouse_event(message)) {     
  16.         if (QApplication::activePopupWidget() != 0) {                 
  17.             POINT curPos = msg.pt;     
  18.             //取得鼠標點擊座標所在的QWidget指針,它指向我們在main創建的widget實例     
  19.             QWidget* w = QApplication::widgetAt(curPos.x, curPos.y);     
  20.             if (w)     
  21.                 widget = (QETWidget*)w;     
  22.         }     
  23.         if (!qt_tabletChokeMouse) {     
  24.             //對,就在這裏。Windows的回調函數將鼠標事件分發回給了Qt Widget      
  25.             // => Section 2-2     
  26.             result = widget->translateMouseEvent(msg);            
  27.      ...     
  28. }     
  29. // Section 2-2  $QTDIR\src\gui\kernel\qapplication_win.cpp     
  30. //該函數所在與Windows平臺相關,主要職責就是把已windows格式打包的鼠標事件解包、翻譯成QApplication可識別的QMouseEvent,QWidget.     
  31. bool QETWidget::translateMouseEvent(const MSG &msg)     
  32. {     
  33.      //.. 這裏很長的代碼給以忽略       
  34.       // 讓我們看一下sendMouseEvent的聲明     
  35.      // widget是事件的接受者; e是封裝好的QMouseEvent     
  36.      // ==> Section 2-3     
  37.      res = QApplicationPrivate::sendMouseEvent(widget, &e, alienWidget, this, &qt_button_down, qt_last_mouse_receiver);     
  38. }         
  39. // Section 2-3 $QTDIR\src\gui\kernel\qapplication.cpp  
  40. bool QApplicationPrivate::sendMouseEvent(QWidget *receiver, QMouseEvent *event,QWidget *alienWidget, QWidget *nativeWidget,  QWidget **buttonDown, QPointer<QWidget> &lastMouseReceiver, bool spontaneous)  
  41. {  
  42.      //至此與平臺相關代碼處理完畢  
  43.      //MouseEvent默認的發送方式是spontaneous, 所以將執行sendSpontaneousEvent。 sendSpontaneousEvent() 與 sendEvent的代碼實現幾乎相同,
  44. 除了將QEvent的屬性spontaneous標記不同。 這裏是解釋什麼spontaneous事件:如果事件由應用程序之外產生的,比如一個系統事件。 
  45. 顯然MousePress事件是由視窗系統產生的一個的事件(詳見上文Section 1~ Section 7),因此它是spontaneous事件  
  46.     if (spontaneous)  
  47.         result = QApplication::sendSpontaneousEvent(receiver, event);  ==〉Section 2-4  
  48.     else  
  49.         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.     if (sendThroughObjectEventFilters(receiver, e))     
  57.         return true;     
  58.      // 遞交事件給receiver  => Section 2-8     
  59.     bool consumed = receiver->event(e);     
  60.     e->spont = false;     
  61. }     
  62. // Section 2-8  $QTDIR\gui\kernel\qwidget.cpp     
  63. // QApplication通過notify及其私有類notify_helper,將事件最終派發給了QObject的子類- QWidget.     
  64. bool QWidget::event(QEvent *event)     
  65. {     
  66.     ...     
  67.     switch(event->type()) {     
  68.     case QEvent::MouseButtonPress:     
  69.         // Don't reset input context here. Whether reset or not is     
  70.         // a responsibility of input method. reset() will be     
  71.         // called by mouseHandler() of input method if necessary     
  72.         // via mousePressEvent() of text widgets.     
  73. #if 0     
  74.         resetInputContext();     
  75. #endif     
  76.         //mousePressEvent是虛函數,QWidget的子類可以通過重載重新定義mousePress事件的行爲     
  77.         mousePressEvent((QMouseEvent*)event);     
  78.         break;        
  79. }   
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章