path: root/src/widgets
diff options
authorLaszlo Agocs <>2014-08-07 16:45:05 +0200
committerLaszlo Agocs <>2014-08-08 07:29:29 +0200
commit22e439141384f49028770d2410fafb18ef8cad1f (patch)
treed30ada7a9da27083c464948654ad1d87b21d1f65 /src/widgets
parent41a7072195eef0f7daa94d0ce75a3f68e721d1cb (diff)
Add default format to QSurfaceFormat
Add defaultFormat() and setDefaultFormat() statics to QSurfaceFormat. These define the default values for the requestedFormat members of QOpenGLContext, QWindow and QOpenGLWidget (and implicitly QOpenGLWindow, QQuickWindow, etc.) This replaces QQuickWindow::setDefaultFormat() which can now be removed. The main inspiration here is not the convenience (avoiding setFormat() calls for all windows/widgets), but robustness: by setting the format once at the start of the application, all windows and contexts, including the internal share context used by QOpenGLWidget and QQuickWidget, will use the same format, eliminating the possibility of failing due to trying to share between incompatible contexts. Furthermore, since such a functionality is anyway mandatory for QQuickWindow (due to the possibility of creating windows from QML code), extending it to QSurfaceFormat and QOpenGLContext/QWindow is the next logical step. Change-Id: Ie94486adc489d17fecfcebb7050fecedffd2688b Reviewed-by: Gunnar Sletta <>
Diffstat (limited to 'src/widgets')
2 files changed, 35 insertions, 3 deletions
diff --git a/src/widgets/doc/snippets/code/doc_gui_widgets_qopenglwidget.cpp b/src/widgets/doc/snippets/code/doc_gui_widgets_qopenglwidget.cpp
index 0814b5f30f..921c3fa9f8 100644
--- a/src/widgets/doc/snippets/code/doc_gui_widgets_qopenglwidget.cpp
+++ b/src/widgets/doc/snippets/code/doc_gui_widgets_qopenglwidget.cpp
@@ -178,3 +178,22 @@ void MyGLWidget::cleanup()
//! [5]
+//! [6]
+int main(int argc, char **argv)
+ QApplication app(argc, argv);
+ QSurfaceFormat format;
+ format.setDepthBufferSize(24);
+ format.setStencilBufferSize(8);
+ format.setVersion(3, 2);
+ format.setProfile(QSurfaceFormat::CoreProfile);
+ QSurfaceFormat::setDefaultFormat(format);
+ MyWidget widget;
+ return app.exec();
+//! [6]
diff --git a/src/widgets/kernel/qopenglwidget.cpp b/src/widgets/kernel/qopenglwidget.cpp
index 33e036ee64..e59057a662 100644
--- a/src/widgets/kernel/qopenglwidget.cpp
+++ b/src/widgets/kernel/qopenglwidget.cpp
@@ -107,7 +107,8 @@ QT_BEGIN_NAMESPACE
setFormat(). Keep in mind however that having multiple QOpenGLWidget
instances in the same window requires that they all use the same
format, or at least formats that do not make the contexts
- non-sharable.
+ non-sharable. To overcome this issue, prefer using
+ QSurfaceFormat::setDefaultFormat() instead of setFormat().
\section1 Painting Techniques
@@ -190,7 +191,7 @@ QT_BEGIN_NAMESPACE
\snippet code/doc_gui_widgets_qopenglwidget.cpp 0
- Alternatively, the prefixing of each and every OpenGL call can be avoidided by deriving
+ Alternatively, the prefixing of each and every OpenGL call can be avoided by deriving
from QOpenGLFunctions instead:
\snippet code/doc_gui_widgets_qopenglwidget.cpp 1
@@ -206,6 +207,12 @@ QT_BEGIN_NAMESPACE
\snippet code/doc_gui_widgets_qopenglwidget.cpp 3
+ As described above, it is simpler and more robust to set the requested format
+ globally so that it applies to all windows and contexts during the lifetime of
+ the application. Below is an example of this:
+ \snippet code/doc_gui_widgets_qopenglwidget.cpp 6
\section1 Relation to QGLWidget
The legacy QtOpenGL module (classes prefixed with QGL) provides a widget
@@ -392,6 +399,7 @@ public:
+ requestedFormat = QSurfaceFormat::defaultFormat();
@@ -620,13 +628,18 @@ QOpenGLWidget::~QOpenGLWidget()
Sets the requested surface \a format.
+ When the format is not explicitly set via this function, the format returned by
+ QSurfaceFormat::defaultFormat() will be used. This means that when having multiple
+ OpenGL widgets, individual calls to this function can be replaced by one single call to
+ QSurfaceFormat::setDefaultFormat() before creating the first widget.
\note Requesting an alpha buffer via this function will not lead to the desired results
and should be avoided. Instead, use Qt::WA_AlwaysStackOnTop to enable semi-transparent
QOpenGLWidget instances with other widgets visible underneath. Keep in mind however that
this breaks the stacking order, so it will no longer be possible to have other widgets
on top of the QOpenGLWidget.
- \sa format(), Qt::WA_AlwaysStackOnTop
+ \sa format(), Qt::WA_AlwaysStackOnTop, QSurfaceFormat::setDefaultFormat()
void QOpenGLWidget::setFormat(const QSurfaceFormat &format)