青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

天下

記錄修行的印記

[轉]淺議Qt的事件處理機制二

淺議Qt的事件處理機制二
我們在上文中,介紹了Qt框架的事件處理機制:事件的產生、分發、接受和處理,并以視窗系統鼠標點擊QWidget為例,對代碼進行了剖析,向大家分析了Qt框架如何通過Event Loop處理進入處理消息隊列循環,如何一步一步委派給平臺相關的函數獲取、打包用戶輸入事件交給視窗系統處理,函數調用棧如下:
    
QtCored4.dll
!QEventDispatcherWin32::processEvents()  qeventdispatcher_win.cpp Line 742    C++
QtGuid4.dll
!QGuiEventDispatcherWin32::processEvents()  Line 1202 + 0x15 bytes    C++
QtCored4.dll
!QEventLoop::processEvents()  Line 150    C++
QtCored4.dll
!QEventLoop::exec()  Line 204 + 0x2d bytes    C++
QtCored4.dll
!QCoreApplication::exec()  Line 1187 + 0x15 bytes    C++
QtGuid4.dll
!QApplication::exec()  Line 3813    C++
qt03.exe
!main()  Line 11 + 0x6 bytes    C++
qt03.exe
!WinMain()  Line 131 + 0x12 bytes    C++
qt03.exe
!__tmainCRTStartup()  Line 574 + 0x35 bytes    C
qt03.exe
!WinMainCRTStartup()  Line 399    C
kernel32.dll
!_BaseProcessStart@4()  + 0x23 bytes    
    
    

1.main(intchar **
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)

// (續上文Section 7) Section 2-1:   
QT_WIN_CALLBACK QtWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)   
{  
     
   
//檢查message是否屬于Qt可轉義的鼠標事件   
   if (qt_is_translatable_mouse_event(message)) {
        
if (QApplication::activePopupWidget() != 0) {              
            POINT curPos 
= msg.pt;  
            
//取得鼠標點擊坐標所在的QWidget指針,它指向我們在main創建的widget實例   
            QWidget* w = QApplication::widgetAt(curPos.x, curPos.y);  
            
if (w)  
                widget 
= (QETWidget*)w;  
        }  
        
if (!qt_tabletChokeMouse) {  
            
//對,就在這里。Windows的回調函數將鼠標事件分發回給了Qt Widget    
            
// => Section 2-2   
            result = widget->translateMouseEvent(msg);         
     
        }
    }
}  
// Section 2-2  $QTDIR/src/gui/kernel/qapplication_win.cpp   
//該函數所在與Windows平臺相關,主要職責就是把已windows格式打包的鼠標事件解包、翻譯成QApplication可識別的QMouseEvent,QWidget.
//打包翻譯成功return true,否則false

bool QETWidget::translateMouseEvent(const MSG &msg)  
{  
    
//.. 這里很長的代碼給以忽略   
    
    
    
//比如主窗口中有一個QPushButton,當在PushButton單擊時,translateMouseEvent函數會計算出當前單擊事件所在的的控件alienWidget,然后通過sendMouseEvent發送給當前到alienWidget!
     
    
//單擊事件,計算出當前單擊事件所在的的控件alienWidget
    state  = translateButtonState(msg.wParam, type, button); // button state
    const QPoint widgetPos = mapFromGlobal(QPoint(msg.pt.x, msg.pt.y));
    QWidget 
*alienWidget = !internalWinId() ? this : childAt(widgetPos);
    
    
    
//計算是否符合MouseButtonPress條件
    gpos = msg.pt;
    pos 
= mapFromGlobal(QPoint(gpos.x, gpos.y));

    
// mouse button pressed
    if (!qt_button_down && (type == QEvent::MouseButtonPress || type == QEvent::MouseButtonDblClick)) {
        QWidget 
*tlw = window();
        
if (QWidget *child = tlw->childAt(mapTo(tlw, pos)))
            qt_button_down 
= child;
        
else
            qt_button_down 
= this;
    }    
    
    
if (alienWidget && alienWidget->internalWinId())
        alienWidget 
= 0;
    
    
const QPoint globalPos(gpos.x,gpos.y);
    QWidget 
*widget = QApplicationPrivate::pickMouseReceiver(this, globalPos, pos, type,
                                                             Qt::MouseButtons(bs),
                                                             qt_button_down, alienWidget);
    
if (!widget)
        
return false// don't send event

    QMouseEvent e(type, pos, globalPos, Qt::MouseButton(button),
                  Qt::MouseButtons(state 
& Qt::MouseButtonMask),
                  Qt::KeyboardModifiers(state 
& Qt::KeyboardModifierMask));
            
       
      
// 讓我們看一下sendMouseEvent的聲明   
     
// widget是事件的接受者; e是封裝好的QMouseEvent   
     
// ==> Section 2-3   
     res = QApplicationPrivate::sendMouseEvent(widget, &e, alienWidget, this&qt_button_down, qt_last_mouse_receiver);  
}  
// Section 2-3 $QTDIR/src/gui/kernel/qapplication.cpp   
bool QApplicationPrivate::sendMouseEvent(QWidget *receiver, QMouseEvent *event,  
                                         QWidget 
*alienWidget, QWidget *nativeWidget,  
                                         QWidget 
**buttonDown, QPointer<QWidget> &lastMouseReceiver,  
                                         
bool spontaneous)  
{  
     
//至此與平臺相關代碼處理完畢   
     
//MouseEvent默認的發送方式是spontaneous, 所以將執行sendSpontaneousEvent。 sendSpontaneousEvent() 與 sendEvent的代碼實現幾乎相同,除了將QEvent的屬性spontaneous標記不同。 這里是解釋什么spontaneous事件:如果事件由應用程序之外產生的,比如一個系統事件。 顯然MousePress事件是由視窗系統產生的一個的事件(詳見上文Section 1~ Section 7),因此它是spontaneous事件  
    if (spontaneous)  
        result 
= QApplication::sendSpontaneousEvent(receiver, event);  ==>Section 2-4  
    
else  
        result 
= QApplication::sendEvent(receiver, event);  
}  
// (續上文Section 7) Section 2-1:
QT_WIN_CALLBACK QtWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 
{
    
    
//如果我們通過EventFilter QCoreApplication::setEventFilter (EventFilter filter )設置了EventFilter
    
//將會調用我們設置的EventFilter
    
// send through app filter
    if (qApp->filterEvent(&msg, &res))
        
return res;    
   
   
//檢查message是否屬于Qt可轉義的鼠標事件
   if (qt_is_translatable_mouse_event(message)) {
        
if (QApplication::activePopupWidget() != 0) {            
            POINT curPos 
= msg.pt;
            
//取得鼠標點擊坐標所在的QWidget指針,它指向我們在main創建的widget實例
            QWidget* w = QApplication::widgetAt(curPos.x, curPos.y);
            
if (w)
                widget 
= (QETWidget*)w;
        }
        
if (!qt_tabletChokeMouse) {
            
//對,就在這里。Windows的回調函數將鼠標事件分發回給了Qt Widget 
            
// => Section 2-2
            result = widget->translateMouseEvent(msg);       
     
}
// Section 2-2  $QTDIR/src/gui/kernel/qapplication_win.cpp
//該函數所在與Windows平臺相關,主要職責就是把已windows格式打包的鼠標事件解包、翻譯成QApplication可識別的QMouseEvent,QWidget.
bool QETWidget::translateMouseEvent(const MSG &msg)
{
     
//.. 這里很長的代碼給以忽略  
     
// 讓我們看一下sendMouseEvent的聲明
     
// widget是事件的接受者; e是封裝好的QMouseEvent
     
// ==> Section 2-3
     res = QApplicationPrivate::sendMouseEvent(widget, &e, alienWidget, this&qt_button_down, qt_last_mouse_receiver);
}
// Section 2-3 $QTDIR/src/gui/kernel/qapplication.cpp
bool QApplicationPrivate::sendMouseEvent(QWidget *receiver, QMouseEvent *event,
                                         QWidget 
*alienWidget, QWidget *nativeWidget,
                                         QWidget 
**buttonDown, QPointer<QWidget> &lastMouseReceiver,
                                         
bool spontaneous)
{
     
//至此與平臺相關代碼處理完畢
     
//MouseEvent默認的發送方式是spontaneous, 所以將執行sendSpontaneousEvent。 sendSpontaneousEvent() 與 sendEvent的代碼實現幾乎相同,除了將QEvent的屬性spontaneous標記不同。 這里是解釋什么spontaneous事件:如果事件由應用程序之外產生的,比如一個系統事件。 顯然MousePress事件是由視窗系統產生的一個的事件(詳見上文Section 1~ Section 7),因此它是spontaneous事件
    if (spontaneous)
        result 
= QApplication::sendSpontaneousEvent(receiver, event);  ==〉Section 2-4
    
else
        result 
= QApplication::sendEvent(receiver, event);
}
 
// Section 2-4 C:/Qt/4.7.1-Vs/src/corelib/kernel/qcoreapplication.h   
inline bool QCoreApplication::sendSpontaneousEvent(QObject *receiver, QEvent *event)  
{   
    
//將event標記為自發事件   
     
//進一步調用 2-5 QCoreApplication::notifyInternal   
    if (eventevent->spont = truereturn self ? self->notifyInternal(receiver, event) : false;   
}  
// Section 2-5:  $QTDIR/gui/kernel/qapplication.cpp   
bool QCoreApplication::notifyInternal(QObject *receiver, QEvent *event)  
{  
      
    
// 幾行代碼對于Qt Jambi (QT Java綁定版本) 和QSA (QT Script for Application)的支持   
       
     
// 以下代碼主要意圖為Qt強制事件只能夠發送給當前線程里的對象,也就是說receiver->d_func()->threadData應該等于QThreadData::current()。 注意,跨線程的事件需要借助Event Loop來派發   
     QObjectPrivate *= receiver->d_func();  
    QThreadData 
*threadData = d->threadData;  
    
++threadData->loopLevel;  
    
bool returnValue;  
    QT_TRY {  
        
//哇,終于來到大名鼎鼎的函數QCoreApplication::nofity()了 ==> Section 2-6   
        returnValue = notify(receiver, event);  
    } QT_CATCH () {  
        
--threadData->loopLevel;  
        QT_RETHROW;  
    }  
}  
// Section 2-6:  $QTDIR/gui/kernel/qapplication.cpp   
// QCoreApplication::notify和它的重載函數QApplication::notify在Qt的派發過程中起到核心的作用,Qt的官方文檔時這樣說的:任何線程的任何對象的所有事件在發送時都會調用notify函數。   
bool QApplication::notify(QObject *receiver, QEvent *e)  
{  
   
//代碼很長,最主要的是一個大大的Switch,Case   
   ..  
   
switch ( e->type())  
   {  
      
    
case QEvent::MouseButtonPress:  
    
case QEvent::MouseButtonRelease:  
    
case QEvent::MouseButtonDblClick:  
    
case QEvent::MouseMove:  
       
        
//讓自己私有類(d是私有類的句柄)來進一步處理 ==> Section 2-7   
        res = d->notify_helper(w, w == receiver ? mouse : &me);  
        e
->spont = false;  
        
break;  
    }  
      
}  
// Section 2-7:  $QTDIR/gui/kernel/qapplication.cpp   
bool QApplicationPrivate::notify_helper(QObject *receiver, QEvent * e)  
{  
      
    
// 向事件過濾器發送該事件,這里介紹一下Event Filters. 事件過濾器是一個接受即將發送給目標對象所有事件的對象。   
   
//如代碼所示它開始處理事件在目標對象行動之前。過濾器的QObject::eventFilter()實現被調用,能接受或者丟棄過濾,允許或者拒絕事件的更進一步的處理。如果所有的事件過濾器允許更進一步的事件處理,事件將被發送到目標對象本身。如果他們中的一個停止處理,目標和任何后來的事件過濾器不能看到任何事件。   
    if (sendThroughObjectEventFilters(receiver, e))  
        
return true;  
     
// 遞交事件給receiver  => Section 2-8   
    bool consumed = receiver->event(e);  
    e
->spont = false;  
}  
// Section 2-8  $QTDIR/gui/kernel/qwidget.cpp   
// QApplication通過notify及其私有類notify_helper,將事件最終派發給了QObject的子類- QWidget.   
bool QWidget::event(QEvent *event)  
{  
      
    
switch(event->type()) {  
    
case QEvent::MouseButtonPress:  
        
// Don't reset input context here. Whether reset or not is   
        
// a responsibility of input method. reset() will be   
        
// called by mouseHandler() of input method if necessary   
        
// via mousePressEvent() of text widgets.   
        
//mousePressEvent是虛函數,QWidget的子類可以通過重載重新定義mousePress事件的行為   
        mousePressEvent((QMouseEvent*)event);  
        
break;     



/*!
    Sends \a message through the event filter that was set by
    setEventFilter(). If no event filter has been set, this function
    returns false; otherwise, this function returns the result of the
    event filter function in the \a result parameter.
    \sa setEventFilter()
*/
bool QCoreApplication::filterEvent(void *message, long *result)
{
    Q_D(QCoreApplication);
    
if (result)
        
*result = 0;
    
if (d->eventFilter)
        
return d->eventFilter(message, result);
#ifdef Q_OS_WIN
    
return winEventFilter(reinterpret_cast<MSG *>(message), result);
#else
    
return false;
#endif
}

posted on 2013-07-04 12:00 天下 閱讀(2571) 評論(0)  編輯 收藏 引用 所屬分類: QT

<2012年8月>
2930311234
567891011
12131415161718
19202122232425
2627282930311
2345678

導航

統計

常用鏈接

留言簿(4)

隨筆分類(378)

隨筆檔案(329)

鏈接

最新隨筆

搜索

最新評論

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            一本色道久久88精品综合| 久久精品人人| 亚洲精品日韩久久| 欧美日韩成人一区| 午夜精品福利视频| 午夜一区二区三区不卡视频| 国产亚洲综合精品| 亚洲福利视频免费观看| 欧美激情一级片一区二区| 亚洲调教视频在线观看| 亚洲欧美中文另类| 亚洲国产精品成人久久综合一区| 亚洲国产99| 国产精品电影观看| 久久综合九色99| 欧美人与禽猛交乱配视频| 午夜精品久久久久久久白皮肤 | 亚洲国产精品va在线看黑人| 欧美日韩国产首页在线观看| 久久av资源网| 欧美chengren| 久久国产精品一区二区三区| 欧美成人精品在线| 性欧美xxxx大乳国产app| 蜜乳av另类精品一区二区| 亚洲一区二区三区精品在线| 久久久欧美一区二区| 亚洲伊人第一页| 久久综合中文色婷婷| 亚洲免费影视| 欧美人妖另类| 欧美国产在线观看| 国产亚洲精品bt天堂精选| 亚洲精品中文字| 极品少妇一区二区三区| 这里是久久伊人| 亚洲精选大片| 久久男人资源视频| 欧美影视一区| 欧美日韩日本国产亚洲在线| 欧美黄色成人网| 国内成人在线| 亚洲欧美视频在线| 亚洲一区视频在线观看视频| 免费成人在线视频网站| 久久久久久网站| 国产女主播视频一区二区| 在线亚洲激情| 一区二区三区蜜桃网| 欧美激情麻豆| 亚洲大胆视频| 亚洲国产精品久久人人爱蜜臀| 亚洲欧美日韩国产中文 | 亚洲第一在线综合网站| 欧美一级视频精品观看| 欧美在线观看视频在线| 国产精品久久久久久久9999| 日韩午夜在线电影| 亚洲午夜高清视频| 国产精品99一区二区| 99国产精品| 亚洲欧美日韩国产精品| 国产精品久久久久久久久久ktv| 亚洲三级免费观看| 中文av字幕一区| 欧美日韩综合视频| 亚洲视频网在线直播| 亚洲一区精品在线| 国产精品午夜在线| 午夜在线精品| 久久亚洲欧洲| 亚洲福利在线观看| 你懂的网址国产 欧美| 亚洲日本成人| 亚洲午夜激情网页| 国产欧美综合一区二区三区| 午夜精品久久久久久久久久久久| 久久久99爱| 亚洲国产一区在线| 欧美激情精品久久久久久免费印度| 亚洲精品乱码久久久久| 西瓜成人精品人成网站| 国产一区香蕉久久| 欧美国产精品日韩| 一区二区三区视频免费在线观看| 欧美在线一区二区| 伊人男人综合视频网| 欧美激情一区二区三区四区| 在线视频你懂得一区二区三区| 久久国产一二区| 亚洲欧洲精品一区二区三区波多野1战4 | 欧美va亚洲va日韩∨a综合色| 亚洲人www| 国产精品日韩欧美一区二区三区 | 欧美日本一区| 欧美在线观看视频在线| 亚洲电影欧美电影有声小说| 亚洲综合另类| 亚洲电影免费观看高清完整版在线| 欧美人与性动交α欧美精品济南到| 一区二区成人精品| 免费成人av在线| 亚洲欧美日韩在线综合| 在线日韩av片| 国产精品亚洲成人| 欧美国产精品| 久久九九99| 亚洲午夜激情| 亚洲精品国产视频| 免费观看成人鲁鲁鲁鲁鲁视频| 中文精品一区二区三区| 亚洲成色最大综合在线| 国产精品入口日韩视频大尺度| 久久综合久久88| 亚欧成人在线| 亚洲视频免费| 日韩午夜剧场| 亚洲国产另类精品专区| 麻豆91精品91久久久的内涵| 欧美一区二区精美| 一区二区久久久久| 亚洲精品一区久久久久久| 国产午夜久久久久| 国产精品红桃| 欧美日本一区二区高清播放视频| 美女视频黄免费的久久| 久久久五月婷婷| 欧美亚洲综合在线| 午夜精品影院在线观看| 亚洲一区二区三| 一区二区三区视频在线| 99视频精品在线| 亚洲精品之草原avav久久| 亚洲国产老妈| 亚洲黑丝在线| 亚洲欧洲精品天堂一级| 欧美激情导航| 亚洲第一福利社区| 亚洲国产高清视频| 亚洲国产影院| 亚洲理伦电影| 99国产精品国产精品久久| 亚洲精品免费一区二区三区| 亚洲人成亚洲人成在线观看| 亚洲精品国产精品国自产在线| 亚洲经典在线看| 亚洲精品一区二| 亚洲看片一区| 日韩亚洲欧美综合| 夜夜躁日日躁狠狠久久88av| 亚洲视频碰碰| 欧美一区二区性| 久久性色av| 欧美精品亚洲二区| 国产精品久久久久久久app| 国产精品专区第二| 国产一区二区三区丝袜| 91久久精品www人人做人人爽| 99国产精品99久久久久久| 亚洲午夜视频| 久久精品国产久精国产爱| 麻豆精品一区二区综合av| 亚洲第一在线综合在线| 亚洲日本在线观看| 亚洲欧美精品中文字幕在线| 欧美在线欧美在线| 欧美不卡视频一区发布| 欧美亚洲第一区| 国产一区二区三区奇米久涩| 亚洲国产另类久久精品| 亚洲一区二区三区在线视频| 午夜欧美精品| 欧美成人自拍| 亚洲主播在线| 欧美激情影院| 国产日韩欧美中文| 亚洲精品免费一二三区| 欧美在线一二三四区| 亚洲国产清纯| 篠田优中文在线播放第一区| 嫩模写真一区二区三区三州| 国产精品美女久久久久av超清 | 国产精品美女久久久久久免费 | 国产日韩久久| 一本色道久久88精品综合| 久久全国免费视频| 亚洲精品视频在线观看免费| 欧美一区免费| 国产精品qvod| 亚洲精品社区| 免费在线观看精品| 午夜在线播放视频欧美| 欧美日韩黄视频| 亚洲国产成人午夜在线一区| 欧美一区二区视频在线观看2020| 亚洲国产精品免费| 久久久人成影片一区二区三区观看| 国产精品久久久久久久9999| 一区二区动漫| 亚洲第一中文字幕在线观看|