diff --git a/src/plugins/platforms/cocoa/qnsview.h b/src/plugins/platforms/cocoa/qnsview.h
index 67b16b4b329b2ddef16fd92e3e0f6031c8c883ca..20e82940bd328d5f968cca682acbabeff1c1406e 100644
--- a/src/plugins/platforms/cocoa/qnsview.h
+++ b/src/plugins/platforms/cocoa/qnsview.h
@@ -69,6 +69,9 @@ QT_END_NAMESPACE
     bool m_sendUpAsRightButton;
     Qt::KeyboardModifiers currentWheelModifiers;
     bool m_subscribesForGlobalFrameNotifications;
+    QCocoaGLContext *m_glContext;
+    bool m_glContextDirty;
+    bool m_drawRectHasBeenCalled;
 }
 
 - (id)init;
diff --git a/src/plugins/platforms/cocoa/qnsview.mm b/src/plugins/platforms/cocoa/qnsview.mm
index a53d6c4e446b5bd1c511f6c321a435c82fe50669..7868d058aee8304af36a0d9f5785c208c2d887e3 100644
--- a/src/plugins/platforms/cocoa/qnsview.mm
+++ b/src/plugins/platforms/cocoa/qnsview.mm
@@ -85,6 +85,9 @@ static QTouchDevice *touchDevice = 0;
         m_buttons = Qt::NoButton;
         m_sendKeyEvent = false;
         m_subscribesForGlobalFrameNotifications = false;
+        m_glContext = 0;
+        m_glContextDirty = false;
+        m_drawRectHasBeenCalled = false;
         currentCustomDragTypes = 0;
         m_sendUpAsRightButton = false;
 
@@ -150,7 +153,12 @@ static QTouchDevice *touchDevice = 0;
 
 - (void) setQCocoaGLContext:(QCocoaGLContext *)context
 {
-    [context->nsOpenGLContext() setView:self];
+    m_glContext = context;
+    if (m_drawRectHasBeenCalled) {
+        [m_glContext->nsOpenGLContext() setView:self];
+    } else {
+        m_glContextDirty = true;
+    }
     if (!m_subscribesForGlobalFrameNotifications) {
         // NSOpenGLContext expects us to repaint (or update) the view when
         // it changes position on screen. Since this happens unnoticed for
@@ -344,6 +352,13 @@ static QTouchDevice *touchDevice = 0;
 
 - (void) drawRect:(NSRect)dirtyRect
 {
+    if (m_glContext && m_glContextDirty) {
+        [m_glContext->nsOpenGLContext() setView:self];
+        m_glContextDirty = false;
+    } else {
+        m_drawRectHasBeenCalled = true;
+    }
+
     if (!m_backingStore)
         return;