diff --git a/src/plugins/platforms/xcb/qxcbconnection.cpp b/src/plugins/platforms/xcb/qxcbconnection.cpp
index 5510c3b1b4..77e4601485 100644
--- a/src/plugins/platforms/xcb/qxcbconnection.cpp
+++ b/src/plugins/platforms/xcb/qxcbconnection.cpp
@@ -818,6 +818,15 @@ void QXcbConnection::handleButtonRelease(xcb_generic_event_t *ev)
     qCDebug(lcQpaXInput, "xcb: released mouse button %d, button state %X", event->detail, static_cast<unsigned int>(m_buttons));
 }
 
+void QXcbConnection::handleMotionNotify(xcb_generic_event_t *ev)
+{
+    xcb_motion_notify_event_t *event = (xcb_motion_notify_event_t *)ev;
+
+    m_buttons = (m_buttons & ~0x7) | translateMouseButtons(event->state);
+    if (Q_UNLIKELY(lcQpaXInput().isDebugEnabled()))
+        qDebug("xcb: moved mouse to %4d, %4d; button state %X", event->event_x, event->event_y, static_cast<unsigned int>(m_buttons));
+}
+
 #ifndef QT_NO_XKB
 namespace {
     typedef union {
@@ -868,11 +877,8 @@ void QXcbConnection::handleXcbEvent(xcb_generic_event_t *event)
             handleButtonRelease(event);
             HANDLE_PLATFORM_WINDOW_EVENT(xcb_button_release_event_t, event, handleButtonReleaseEvent);
         case XCB_MOTION_NOTIFY:
-            if (Q_UNLIKELY(lcQpaXInput().isDebugEnabled())) {
-                xcb_motion_notify_event_t *mev = (xcb_motion_notify_event_t *)event;
-                qDebug("xcb: moved mouse to %4d, %4d; button state %X", mev->event_x, mev->event_y, static_cast<unsigned int>(m_buttons));
-            }
             m_keyboard->updateXKBStateFromCore(((xcb_motion_notify_event_t *)event)->state);
+            handleMotionNotify(event);
             HANDLE_PLATFORM_WINDOW_EVENT(xcb_motion_notify_event_t, event, handleMotionNotifyEvent);
         case XCB_CONFIGURE_NOTIFY:
             HANDLE_PLATFORM_WINDOW_EVENT(xcb_configure_notify_event_t, event, handleConfigureNotifyEvent);
diff --git a/src/plugins/platforms/xcb/qxcbconnection.h b/src/plugins/platforms/xcb/qxcbconnection.h
index 4d0596550c..9a73006cec 100644
--- a/src/plugins/platforms/xcb/qxcbconnection.h
+++ b/src/plugins/platforms/xcb/qxcbconnection.h
@@ -496,6 +496,7 @@ private:
     void updateScreens();
     void handleButtonPress(xcb_generic_event_t *event);
     void handleButtonRelease(xcb_generic_event_t *event);
+    void handleMotionNotify(xcb_generic_event_t *event);
 
     bool m_xi2Enabled;
     int m_xi2Minor;