自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Qt 事件處理機(jī)制 (下篇)

移動(dòng)開(kāi)發(fā)
在Qt中,事件被封裝成一個(gè)個(gè)對(duì)象,所有的事件均繼承自抽象類QEvent. 接下來(lái)依次談?wù)凲t中有誰(shuí)來(lái)產(chǎn)生、分發(fā)、接受和處理事件。

繼續(xù)我們上一篇文章繼續(xù)介紹,Qt 事件處理機(jī)制 (上篇) 介紹了Qt框架的事件處理機(jī)制:事件的產(chǎn)生、分發(fā)、接受和處理,并以視窗系統(tǒng)鼠標(biāo)點(diǎn)擊QWidget為例,對(duì)代碼進(jìn)行了剖析,向大家分析了Qt框架如何通過(guò)Event Loop處理進(jìn)入處理消息隊(duì)列循環(huán),如何一步一步委派給平臺(tái)相關(guān)的函數(shù)獲取、打包用戶輸入事件交給視窗系統(tǒng)處理,函數(shù)調(diào)用棧如下:

  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在視窗系統(tǒng)回調(diào)后,事件又是怎么一步步通過(guò)QApplication分發(fā)給最終事件的接受和處理者QWidget::event, (QWidget繼承Object,重載其虛函數(shù)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. // (續(xù)上文Section 7) Section 2-1:     
  10. QT_WIN_CALLBACK QtWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)      
  11. {     
  12.    ...     
  13.    //檢查message是否屬于Qt可轉(zhuǎn)義的鼠標(biāo)事件     
  14.    if (qt_is_translatable_mouse_event(message)) {     
  15.         if (QApplication::activePopupWidget() != 0) {                 
  16.             POINT curPos = msg.pt;     
  17.             //取得鼠標(biāo)點(diǎn)擊坐標(biāo)所在的QWidget指針,它指向我們?cè)趍ain創(chuàng)建的widget實(shí)例     
  18.             QWidget* w = QApplication::widgetAt(curPos.x, curPos.y);     
  19.             if (w)     
  20.                 widget = (QETWidget*)w;     
  21.         }     
  22.         if (!qt_tabletChokeMouse) {     
  23.             //對(duì),就在這里。Windows的回調(diào)函數(shù)將鼠標(biāo)事件分發(fā)回給了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. //該函數(shù)所在與Windows平臺(tái)相關(guān),主要職責(zé)就是把已windows格式打包的鼠標(biāo)事件解包、翻譯成QApplication可識(shí)別的QMouseEvent,QWidget.     
  30. bool QETWidget::translateMouseEvent(const MSG &msg)     
  31. {     
  32.      //.. 這里很長(zhǎng)的代碼給以忽略       
  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<QWidget> &lastMouseReceiver,     
  42.                                          bool spontaneous)     
  43. {     
  44.      //至此與平臺(tái)相關(guān)代碼處理完畢     
  45.      //MouseEvent默認(rèn)的發(fā)送方式是spontaneous, 所以將執(zhí)行sendSpontaneousEvent。 sendSpontaneousEvent() 與 sendEvent的代碼實(shí)現(xiàn)幾乎相同,
  46. 除了將QEvent的屬性spontaneous標(biāo)記不同。 這里是解釋什么spontaneous事件:如果事件由應(yīng)用程序之外產(chǎn)生的,比如一個(gè)系統(tǒng)事件。 
  47. 顯然MousePress事件是由視窗系統(tǒng)產(chǎn)生的一個(gè)的事件(詳見(jiàn)上文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. // (續(xù)上文Section 7) Section 2-1:  
  55. QT_WIN_CALLBACK QtWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)   
  56. {  
  57.    ...  
  58.    //檢查message是否屬于Qt可轉(zhuǎn)義的鼠標(biāo)事件  
  59.    if (qt_is_translatable_mouse_event(message)) {  
  60.         if (QApplication::activePopupWidget() != 0) {              
  61.             POINT curPos = msg.pt;  
  62.             //取得鼠標(biāo)點(diǎn)擊坐標(biāo)所在的QWidget指針,它指向我們?cè)趍ain創(chuàng)建的widget實(shí)例  
  63.             QWidget* w = QApplication::widgetAt(curPos.x, curPos.y);  
  64.             if (w)  
  65.                 widget = (QETWidget*)w;  
  66.         }  
  67.         if (!qt_tabletChokeMouse) {  
  68.             //對(duì),就在這里。Windows的回調(diào)函數(shù)將鼠標(biāo)事件分發(fā)回給了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. //該函數(shù)所在與Windows平臺(tái)相關(guān),主要職責(zé)就是把已windows格式打包的鼠標(biāo)事件解包、翻譯成QApplication可識(shí)別的QMouseEvent,QWidget.  
  75. bool QETWidget::translateMouseEvent(const MSG &msg)  
  76. {  
  77.      //.. 這里很長(zhǎng)的代碼給以忽略    
  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<QWidget> &lastMouseReceiver,  
  87.                                          bool spontaneous)  
  88. {  
  89.      //至此與平臺(tái)相關(guān)代碼處理完畢  
  90.      //MouseEvent默認(rèn)的發(fā)送方式是spontaneous, 所以將執(zhí)行sendSpontaneousEvent。 sendSpontaneousEvent() 與 sendEvent的代碼實(shí)現(xiàn)幾乎相同,
  91. 除了將QEvent的屬性spontaneous標(biāo)記不同。 這里是解釋什么spontaneous事件:如果事件由應(yīng)用程序之外產(chǎn)生的,比如一個(gè)系統(tǒng)事件。 
  92. 顯然MousePress事件是由視窗系統(tǒng)產(chǎn)生的一個(gè)的事件(詳見(jiàn)上文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標(biāo)記為自發(fā)事件     
  5.      //進(jìn)一步調(diào)用 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.     // 幾行代碼對(duì)于Qt Jambi (QT Java綁定版本) 和QSA (QT Script for Application)的支持     
  13.      ...     
  14.      // 以下代碼主要意圖為Qt強(qiáng)制事件只能夠發(fā)送給當(dāng)前線程里的對(duì)象,也就是說(shuō)receiver->d_func()->threadData應(yīng)該等于QThreadData::current()。
  15.  注意,跨線程的事件需要借助Event Loop來(lái)派發(fā)     
  16.      QObjectPrivate *d = receiver->d_func();     
  17.     QThreadData *threadData = d->threadData;     
  18.     ++threadData->loopLevel;     
  19.     bool returnValue;     
  20.     QT_TRY {     
  21.         //哇,終于來(lái)到大名鼎鼎的函數(shù)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和它的重載函數(shù)QApplication::notify在Qt的派發(fā)過(guò)程中起到核心的作用,Qt的官方文檔時(shí)這樣說(shuō)的:
  30. 任何線程的任何對(duì)象的所有事件在發(fā)送時(shí)都會(huì)調(diào)用notify函數(shù)。     
  31. bool QApplication::notify(QObject *receiver, QEvent *e)     
  32. {     
  33.    //代碼很長(zhǎng),最主要的是一個(gè)大大的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是私有類的句柄)來(lái)進(jìn)一步處理 ==> 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.     // 向事件過(guò)濾器發(fā)送該事件,這里介紹一下Event Filters. 事件過(guò)濾器是一個(gè)接受即將發(fā)送給目標(biāo)對(duì)象所有事件的對(duì)象。     
  55.    //如代碼所示它開(kāi)始處理事件在目標(biāo)對(duì)象行動(dòng)之前。過(guò)濾器的QObject::eventFilter()實(shí)現(xiàn)被調(diào)用,能接受或者丟棄過(guò)濾,
  56. 允許或者拒絕事件的更進(jìn)一步的處理。如果所有的事件過(guò)濾器允許更進(jìn)一步的事件處理,事件將被發(fā)送到目標(biāo)對(duì)象本身。
  57. 如果他們中的一個(gè)停止處理,目標(biāo)和任何后來(lái)的事件過(guò)濾器不能看到任何事件。     
  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通過(guò)notify及其私有類notify_helper,將事件最終派發(fā)給了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是虛函數(shù),QWidget的子類可以通過(guò)重載重新定義mousePress事件的行為     
  79.         mousePressEvent((QMouseEvent*)event);     
  80.         break;        
  81. }   

小結(jié):Qt 事件處理機(jī)制 (下篇)的內(nèi)容介紹完了,希望本文對(duì)你 有所幫助!更多相關(guān)資料請(qǐng)參考編輯推薦!

責(zé)任編輯:zhaolei 來(lái)源: 互聯(lián)網(wǎng)
相關(guān)推薦

2011-07-01 14:14:34

Qt 事件

2009-09-02 18:34:28

C#鼠標(biāo)事件

2011-07-05 18:40:19

QT 信號(hào) 機(jī)制

2011-03-17 09:20:05

異常處理機(jī)制

2011-06-17 13:39:47

Qt 文件

2011-07-21 15:20:41

java異常處理機(jī)制

2011-06-22 16:08:40

Qt 多線程 事件循環(huán)

2011-06-23 14:05:32

Qt 事件機(jī)制

2021-07-03 17:53:52

Java異常處理機(jī)制

2009-06-02 10:32:30

Oracle并發(fā)處理

2010-03-05 15:40:16

Python異常

2011-04-06 10:27:46

Java異常處理

2009-07-09 18:15:42

JDBC事務(wù)處理

2024-03-04 10:00:35

數(shù)據(jù)庫(kù)處理機(jī)制

2009-08-05 18:09:17

C#異常處理機(jī)制

2011-06-23 14:40:13

Qt 信號(hào)

2023-02-08 08:11:58

Spring容器核心事件

2009-07-31 11:28:42

錯(cuò)誤處理機(jī)制ASP.NET

2009-08-06 09:45:53

AOS多核處理機(jī)制上網(wǎng)行為管理青蓮

2009-06-19 16:20:14

ASP.NET錯(cuò)誤處理
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)