diff --git a/src/quick/items/qquickflickable.cpp b/src/quick/items/qquickflickable.cpp
index d1bad78d413bf19fba9910de8f2d86e0b62a32af..d12a7554bb0b4f45dec99762280f9c32219d24b6 100644
--- a/src/quick/items/qquickflickable.cpp
+++ b/src/quick/items/qquickflickable.cpp
@@ -1374,15 +1374,15 @@ void QQuickFlickablePrivate::replayDelayedPress()
         delayedPressTimer.stop();
 
         // If we have the grab, release before delivering the event
-        QQuickWindow *w = q->window();
-        if (w && (w->mouseGrabberItem() == q)) {
-            q->ungrabMouse();
+        if (QQuickWindow *w = q->window()) {
+            if (w->mouseGrabberItem() == q)
+                q->ungrabMouse();
+
+            // Use the event handler that will take care of finding the proper item to propagate the event
+            replayingPressEvent = true;
+            QQuickWindowPrivate::get(w)->deliverMouseEvent(mouseEvent.data());
+            replayingPressEvent = false;
         }
-
-        // Use the event handler that will take care of finding the proper item to propagate the event
-        replayingPressEvent = true;
-        QQuickWindowPrivate::get(w)->deliverMouseEvent(mouseEvent.data());
-        replayingPressEvent = false;
     }
 }