From 64f0d4fb353ae2e447483897839d4df8b5b32f54 Mon Sep 17 00:00:00 2001 From: Miikka Heikkinen Date: Tue, 29 Dec 2015 12:27:48 +0200 Subject: Add a method for checking if context was successfully created Now it is possible to check after graph construction if the graph is actually usable in environments where the OpenGL support might not be sufficient. Task-number: QTRD-3748 Change-Id: Ia68b4e51386859f6d0c7b09cb44e50a74730ca55 Reviewed-by: Titta Heikkala --- src/datavisualization/engine/qabstract3dgraph.cpp | 33 +++++++++++++++++++---- 1 file changed, 28 insertions(+), 5 deletions(-) (limited to 'src/datavisualization/engine/qabstract3dgraph.cpp') diff --git a/src/datavisualization/engine/qabstract3dgraph.cpp b/src/datavisualization/engine/qabstract3dgraph.cpp index 2f3caccd..bc40fbf9 100644 --- a/src/datavisualization/engine/qabstract3dgraph.cpp +++ b/src/datavisualization/engine/qabstract3dgraph.cpp @@ -189,7 +189,11 @@ QAbstract3DGraph::QAbstract3DGraph(QAbstract3DGraphPrivate *d, const QSurfaceFor d_ptr->m_context->setFormat(requestedFormat()); d_ptr->m_context->create(); - d_ptr->m_context->makeCurrent(this); + bool makeSuccess = d_ptr->m_context->makeCurrent(this); + + // If we fail to get context, just abort + if (!makeSuccess || !QOpenGLContext::currentContext()) + return; initializeOpenGLFunctions(); @@ -208,6 +212,8 @@ QAbstract3DGraph::QAbstract3DGraph(QAbstract3DGraphPrivate *d, const QSurfaceFor qFatal("GLSL version must be 1.20 or higher. Try installing latest display drivers."); } + d_ptr->m_initialized = true; + d_ptr->renderLater(); #if defined(Q_OS_OSX) @@ -871,6 +877,20 @@ qreal QAbstract3DGraph::margin() const return d_ptr->m_visualController->margin(); } +/*! + * \return \c{true} if the OpenGL context of the graph has been successfully initialized. + * Trying to use a graph when the context initialization has failed typically results in a crash. + * A common reason for a context initialization failure is lack of sufficient platform support + * for OpenGL. + */ +bool QAbstract3DGraph::hasContext() const +{ + if (d_ptr->m_initialized) + return true; + else + return false; +} + /*! * \internal */ @@ -898,9 +918,11 @@ void QAbstract3DGraph::resizeEvent(QResizeEvent *event) { Q_UNUSED(event); - Q3DScene *scene = d_ptr->m_visualController->scene(); - scene->d_ptr->setWindowSize(QSize(width(), height())); - scene->d_ptr->setViewport(QRect(0, 0, width(), height())); + if (d_ptr->m_visualController) { + Q3DScene *scene = d_ptr->m_visualController->scene(); + scene->d_ptr->setWindowSize(QSize(width(), height())); + scene->d_ptr->setViewport(QRect(0, 0, width(), height())); + } } /*! @@ -968,7 +990,8 @@ QAbstract3DGraphPrivate::QAbstract3DGraphPrivate(QAbstract3DGraph *q) m_updatePending(false), m_visualController(0), m_devicePixelRatio(1.f), - m_offscreenSurface(0) + m_offscreenSurface(0), + m_initialized(false) { } -- cgit v1.2.3