diff --git a/tools/qmlscene/main.cpp b/tools/qmlscene/main.cpp
index bc339ae3e5e14bea503f44fb73696ad52d566e7b..2a86b724f490015fd16c13f8a429c871b45bac5b 100644
--- a/tools/qmlscene/main.cpp
+++ b/tools/qmlscene/main.cpp
@@ -405,6 +405,9 @@ int main(int argc, char ** argv)
         }
     }
 
+    // QtWebEngine needs a shared context in order for the GPU thread to
+    // upload textures.
+    QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts, options.contextSharing);
 #ifdef QT_WIDGETS_LIB
     QApplication app(argc, argv);
 #else
@@ -443,15 +446,6 @@ int main(int argc, char ** argv)
         displayFileDialog(&options);
 #endif
 
-    // QWebEngine needs a shared context in order for the GPU thread to
-    // upload textures.
-    QScopedPointer<QOpenGLContext> shareContext;
-    if (options.contextSharing) {
-        shareContext.reset(new QOpenGLContext);
-        shareContext->create();
-        qt_gl_set_global_share_context(shareContext.data());
-    }
-
     int exitCode = 0;
 
     if (!options.file.isEmpty()) {