Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

What does this qt_GetMessageHook(Qt5.6.3) do?



  • Read processEvents() source code in qeventdispatcher_win.cpp, I don't understand the meaning of qt_GetMessageHook(); For example Create a timer (sendPostedEventsWindowsTimerId), I think even without this timer, the peekmessage() in processEvents() in the next cycle can send WM_QT_SENDPOSTEDEVENTS

    LRESULT QT_WIN_CALLBACK qt_GetMessageHook(int code, WPARAM wp, LPARAM lp)
    {
        QEventDispatcherWin32 *q = qobject_cast<QEventDispatcherWin32 *>(QAbstractEventDispatcher::instance());
        Q_ASSERT(q != 0);
    
        if (wp == PM_REMOVE) {
            if (q) {
                MSG *msg = (MSG *) lp;
                QEventDispatcherWin32Private *d = q->d_func();
                const int localSerialNumber = d->serialNumber.load();
                static const UINT mask = inputTimerMask();
                if (HIWORD(GetQueueStatus(mask)) == 0) {
                    // no more input or timer events in the message queue, we can allow posted events to be sent normally now
                    if (d->sendPostedEventsWindowsTimerId != 0) {
                        // stop the timer to send posted events, since we now allow the WM_QT_SENDPOSTEDEVENTS message
                        KillTimer(d->internalHwnd, d->sendPostedEventsWindowsTimerId);
                        d->sendPostedEventsWindowsTimerId = 0;
                    }
                    (void) d->wakeUps.fetchAndStoreRelease(0);
                    if (localSerialNumber != d->lastSerialNumber
                        // if this message IS the one that triggers sendPostedEvents(), no need to post it again
                        && (msg->hwnd != d->internalHwnd
                            || msg->message != WM_QT_SENDPOSTEDEVENTS)) {
                        PostMessage(d->internalHwnd, WM_QT_SENDPOSTEDEVENTS, 0, 0);
                    }
                } else if (d->sendPostedEventsWindowsTimerId == 0
                           && localSerialNumber != d->lastSerialNumber) {
                    // start a special timer to continue delivering posted events while
                    // there are still input and timer messages in the message queue
                    d->sendPostedEventsWindowsTimerId = SetTimer(d->internalHwnd,
                                                                 SendPostedEventsWindowsTimerId,
                                                                 0, // we specify zero, but Windows uses USER_TIMER_MINIMUM
                                                                 NULL);
                    // we don't check the return value of SetTimer()... if creating the timer failed, there's little
                    // we can do. we just have to accept that posted events will be starved
                }
            }
        }
    #ifdef Q_OS_WINCE
        return 0;
    #else
        return q->d_func()->getMessageHook ? CallNextHookEx(0, code, wp, lp) : 0;
    #endif
    }
    
    

Log in to reply