• <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>

            天下

            記錄修行的印記

            [轉(zhuǎn)]淺議Qt的事件處理機(jī)制二

            淺議Qt的事件處理機(jī)制二
            我們?cè)谏衔闹校榻B了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)用棧如下:
                
            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在視窗系統(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)

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

            bool QETWidget::translateMouseEvent(const MSG &msg)  
            {  
                
            //.. 這里很長(zhǎng)的代碼給以忽略   
                
                
                
            //比如主窗口中有一個(gè)QPushButton,當(dāng)在PushButton單擊時(shí),translateMouseEvent函數(shù)會(huì)計(jì)算出當(dāng)前單擊事件所在的的控件alienWidget,然后通過(guò)sendMouseEvent發(fā)送給當(dāng)前到alienWidget!
                 
                
            //單擊事件,計(jì)算出當(dāng)前單擊事件所在的的控件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);
                
                
                
            //計(jì)算是否符合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)  
            {  
                 
            //至此與平臺(tái)相關(guān)代碼處理完畢   
                 
            //MouseEvent默認(rèn)的發(fā)送方式是spontaneous, 所以將執(zhí)行sendSpontaneousEvent。 sendSpontaneousEvent() 與 sendEvent的代碼實(shí)現(xiàn)幾乎相同,除了將QEvent的屬性spontaneous標(biāo)記不同。 這里是解釋什么spontaneous事件:如果事件由應(yīng)用程序之外產(chǎn)生的,比如一個(gè)系統(tǒng)事件。 顯然MousePress事件是由視窗系統(tǒng)產(chǎn)生的一個(gè)的事件(詳見(jiàn)上文Section 1~ Section 7),因此它是spontaneous事件  
                if (spontaneous)  
                    result 
            = QApplication::sendSpontaneousEvent(receiver, event);  ==>Section 2-4  
                
            else  
                    result 
            = QApplication::sendEvent(receiver, event);  
            }  
            // (續(xù)上文Section 7) Section 2-1:
            QT_WIN_CALLBACK QtWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 
            {
                
                
            //如果我們通過(guò)EventFilter QCoreApplication::setEventFilter (EventFilter filter )設(shè)置了EventFilter
                
            //將會(huì)調(diào)用我們?cè)O(shè)置的EventFilter
                
            // send through app filter
                if (qApp->filterEvent(&msg, &res))
                    
            return res;    
               
               
            //檢查message是否屬于Qt可轉(zhuǎn)義的鼠標(biāo)事件
               if (qt_is_translatable_mouse_event(message)) {
                    
            if (QApplication::activePopupWidget() != 0) {            
                        POINT curPos 
            = msg.pt;
                        
            //取得鼠標(biāo)點(diǎn)擊坐標(biāo)所在的QWidget指針,它指向我們?cè)趍ain創(chuàng)建的widget實(shí)例
                        QWidget* w = QApplication::widgetAt(curPos.x, curPos.y);
                        
            if (w)
                            widget 
            = (QETWidget*)w;
                    }
                    
            if (!qt_tabletChokeMouse) {
                        
            //對(duì),就在這里。Windows的回調(diào)函數(shù)將鼠標(biāo)事件分發(fā)回給了Qt Widget 
                        
            // => Section 2-2
                        result = widget->translateMouseEvent(msg);       
                 
            }
            // Section 2-2  $QTDIR/src/gui/kernel/qapplication_win.cpp
            //該函數(shù)所在與Windows平臺(tái)相關(guān),主要職責(zé)就是把已windows格式打包的鼠標(biāo)事件解包、翻譯成QApplication可識(shí)別的QMouseEvent,QWidget.
            bool QETWidget::translateMouseEvent(const MSG &msg)
            {
                 
            //.. 這里很長(zhǎng)的代碼給以忽略  
                 
            // 讓我們看一下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)
            {
                 
            //至此與平臺(tái)相關(guān)代碼處理完畢
                 
            //MouseEvent默認(rèn)的發(fā)送方式是spontaneous, 所以將執(zhí)行sendSpontaneousEvent。 sendSpontaneousEvent() 與 sendEvent的代碼實(shí)現(xiàn)幾乎相同,除了將QEvent的屬性spontaneous標(biāo)記不同。 這里是解釋什么spontaneous事件:如果事件由應(yīng)用程序之外產(chǎn)生的,比如一個(gè)系統(tǒng)事件。 顯然MousePress事件是由視窗系統(tǒng)產(chǎn)生的一個(gè)的事件(詳見(jiàn)上文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標(biāo)記為自發(fā)事件   
                 
            //進(jìn)一步調(diào)用 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)  
            {  
                  
                
            // 幾行代碼對(duì)于Qt Jambi (QT Java綁定版本) 和QSA (QT Script for Application)的支持   
                   
                 
            // 以下代碼主要意圖為Qt強(qiáng)制事件只能夠發(fā)送給當(dāng)前線程里的對(duì)象,也就是說(shuō)receiver->d_func()->threadData應(yīng)該等于QThreadData::current()。 注意,跨線程的事件需要借助Event Loop來(lái)派發(fā)   
                 QObjectPrivate *= receiver->d_func();  
                QThreadData 
            *threadData = d->threadData;  
                
            ++threadData->loopLevel;  
                
            bool returnValue;  
                QT_TRY {  
                    
            //哇,終于來(lái)到大名鼎鼎的函數(shù)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和它的重載函數(shù)QApplication::notify在Qt的派發(fā)過(guò)程中起到核心的作用,Qt的官方文檔時(shí)這樣說(shuō)的:任何線程的任何對(duì)象的所有事件在發(fā)送時(shí)都會(huì)調(diào)用notify函數(shù)。   
            bool QApplication::notify(QObject *receiver, QEvent *e)  
            {  
               
            //代碼很長(zhǎng),最主要的是一個(gè)大大的Switch,Case   
               ..  
               
            switch ( e->type())  
               {  
                  
                
            case QEvent::MouseButtonPress:  
                
            case QEvent::MouseButtonRelease:  
                
            case QEvent::MouseButtonDblClick:  
                
            case QEvent::MouseMove:  
                   
                    
            //讓自己私有類(d是私有類的句柄)來(lái)進(jìn)一步處理 ==> 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)  
            {  
                  
                
            // 向事件過(guò)濾器發(fā)送該事件,這里介紹一下Event Filters. 事件過(guò)濾器是一個(gè)接受即將發(fā)送給目標(biāo)對(duì)象所有事件的對(duì)象。   
               
            //如代碼所示它開始處理事件在目標(biāo)對(duì)象行動(dòng)之前。過(guò)濾器的QObject::eventFilter()實(shí)現(xiàn)被調(diào)用,能接受或者丟棄過(guò)濾,允許或者拒絕事件的更進(jìn)一步的處理。如果所有的事件過(guò)濾器允許更進(jìn)一步的事件處理,事件將被發(fā)送到目標(biāo)對(duì)象本身。如果他們中的一個(gè)停止處理,目標(biāo)和任何后來(lái)的事件過(guò)濾器不能看到任何事件。   
                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通過(guò)notify及其私有類notify_helper,將事件最終派發(fā)給了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是虛函數(shù),QWidget的子類可以通過(guò)重載重新定義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 天下 閱讀(2540) 評(píng)論(0)  編輯 收藏 引用 所屬分類: QT

            <2012年9月>
            2627282930311
            2345678
            9101112131415
            16171819202122
            23242526272829
            30123456

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿(4)

            隨筆分類(378)

            隨筆檔案(329)

            鏈接

            最新隨筆

            搜索

            最新評(píng)論

            亚洲国产精品无码成人片久久| 久久精品无码一区二区无码| 国产精品美女久久久久| 一本色道久久综合狠狠躁| 色婷婷久久久SWAG精品| 久久免费香蕉视频| 国产精品久久久久久久久久免费| 97久久香蕉国产线看观看| 久久无码人妻一区二区三区| 一本色道久久88—综合亚洲精品| 一本一道久久a久久精品综合 | 国产午夜精品理论片久久| 久久精品成人免费网站| 91久久精一区二区三区大全| 99国产精品久久久久久久成人热| 久久婷婷激情综合色综合俺也去| 久久人人妻人人爽人人爽| 久久精品无码午夜福利理论片| 久久精品www人人爽人人| 久久精品毛片免费观看| 久久久精品午夜免费不卡| 色综合久久天天综合| 久久亚洲AV永久无码精品| 久久这里有精品| 久久久噜噜噜久久熟女AA片| 久久精品国产亚洲一区二区| 99久久www免费人成精品| 日本久久中文字幕| 亚洲AV乱码久久精品蜜桃| 久久久久久久综合日本亚洲| 久久精品嫩草影院| 日日狠狠久久偷偷色综合免费| 久久夜色精品国产亚洲| 国产精品久久久久天天影视| 久久99热这里只有精品国产 | 久久丫精品国产亚洲av| 日本精品久久久久中文字幕8| 久久噜噜久久久精品66| 三级三级久久三级久久| 久久久91精品国产一区二区三区| 少妇久久久久久被弄到高潮 |