summaryrefslogtreecommitdiffstats
path: root/examples/opengl/threadedqopenglwidget
diff options
context:
space:
mode:
authorLaszlo Agocs <laszlo.agocs@digia.com>2014-06-20 11:58:34 +0200
committerLaszlo Agocs <laszlo.agocs@digia.com>2014-08-01 17:13:59 +0200
commite453484bca5add5973602044ff0fbc224f819a07 (patch)
tree67f8b2afcb6622fee78f45959980324eeaf4accf /examples/opengl/threadedqopenglwidget
parent718f248a8921ad310cbb9e099b16f5ffa0c860c1 (diff)
Make QOpenGLWidget public
QOpenGLWidget is now public. In addition Qt::WA_AlwaysStackOnTop is introduced to support the special case of semi-transparent QOpenGLWidget or QQuickWidget on top of regular widgets. hellogl_es2 becomes the qopenglwidget example. This example performs painting both via QPainter and native GL commands and has the OpenGL widget combined with other, normal widgets. The widget stack receives some changes when it comes to renderToTexture widgets like QQuickWidget and QOpenGLWidget. Calling update() will now result in a paint event, which is essential for QOpenGLWidget since we want it to behave like a regular widget. The dirty region handling is extended specially for such widgets due to performance reasons. (an OpenGL content update must not result in any backingstore painting, and is thus handled as a different kind of dirtiness) [ChangeLog] Added QOpenGLWidget. This widget serves as a replacement for QGLWidget. Task-number: QTBUG-36899 Task-number: QTBUG-40086 Change-Id: Ibf7f82fea99b39edfffd2fc088e7e0eadbca25cf Reviewed-by: Paul Olav Tvete <paul.tvete@digia.com>
Diffstat (limited to 'examples/opengl/threadedqopenglwidget')
-rw-r--r--examples/opengl/threadedqopenglwidget/glwidget.cpp356
-rw-r--r--examples/opengl/threadedqopenglwidget/glwidget.h124
-rw-r--r--examples/opengl/threadedqopenglwidget/main.cpp69
-rw-r--r--examples/opengl/threadedqopenglwidget/mainwindow.cpp52
-rw-r--r--examples/opengl/threadedqopenglwidget/mainwindow.h54
-rw-r--r--examples/opengl/threadedqopenglwidget/threadedqopenglwidget.pro11
6 files changed, 666 insertions, 0 deletions
diff --git a/examples/opengl/threadedqopenglwidget/glwidget.cpp b/examples/opengl/threadedqopenglwidget/glwidget.cpp
new file mode 100644
index 0000000000..34d97bf1bf
--- /dev/null
+++ b/examples/opengl/threadedqopenglwidget/glwidget.cpp
@@ -0,0 +1,356 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
+** of its contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "glwidget.h"
+#include <math.h>
+#include <QGuiApplication>
+
+const int bubbleNum = 8;
+
+GLWidget::GLWidget(QWidget *parent)
+ : QOpenGLWidget(parent)
+{
+ QSurfaceFormat format;
+ format.setDepthBufferSize(16);
+ setFormat(format);
+
+ setMinimumSize(300, 250);
+
+ connect(this, &QOpenGLWidget::aboutToCompose, this, &GLWidget::onAboutToCompose);
+ connect(this, &QOpenGLWidget::frameSwapped, this, &GLWidget::onFrameSwapped);
+ connect(this, &QOpenGLWidget::aboutToResize, this, &GLWidget::onAboutToResize);
+ connect(this, &QOpenGLWidget::resized, this, &GLWidget::onResized);
+
+ m_thread = new QThread;
+ m_renderer = new Renderer(this);
+ m_renderer->moveToThread(m_thread);
+ connect(m_thread, &QThread::finished, m_renderer, &QObject::deleteLater);
+
+ connect(this, &GLWidget::renderRequested, m_renderer, &Renderer::render);
+ connect(m_renderer, &Renderer::contextWanted, this, &GLWidget::grabContext);
+
+ m_thread->start();
+}
+
+GLWidget::~GLWidget()
+{
+ m_renderer->prepareExit();
+ m_thread->quit();
+ m_thread->wait();
+ delete m_thread;
+}
+
+void GLWidget::onAboutToCompose()
+{
+ // We are on the gui thread here. Composition is about to
+ // begin. Wait until the render thread finishes.
+ m_renderer->lockRenderer();
+}
+
+void GLWidget::onFrameSwapped()
+{
+ m_renderer->unlockRenderer();
+ // Assuming a blocking swap, our animation is driven purely by the
+ // vsync in this example.
+ emit renderRequested();
+}
+
+void GLWidget::onAboutToResize()
+{
+ m_renderer->lockRenderer();
+}
+
+void GLWidget::onResized()
+{
+ m_renderer->unlockRenderer();
+}
+
+void GLWidget::grabContext()
+{
+ m_renderer->lockRenderer();
+ QMutexLocker lock(m_renderer->grabMutex());
+ context()->moveToThread(m_thread);
+ m_renderer->grabCond()->wakeAll();
+ m_renderer->unlockRenderer();
+}
+
+Renderer::Renderer(GLWidget *w)
+ : m_inited(false),
+ m_glwidget(w),
+ m_exiting(false)
+{
+}
+
+void Renderer::paintQtLogo()
+{
+ program.enableAttributeArray(normalAttr);
+ program.enableAttributeArray(vertexAttr);
+ program.setAttributeArray(vertexAttr, vertices.constData());
+ program.setAttributeArray(normalAttr, normals.constData());
+ glDrawArrays(GL_TRIANGLES, 0, vertices.size());
+ program.disableAttributeArray(normalAttr);
+ program.disableAttributeArray(vertexAttr);
+}
+
+// Some OpenGL implementations have serious issues with compiling and linking
+// shaders on multiple threads concurrently. Avoid this.
+Q_GLOBAL_STATIC(QMutex, initMutex)
+
+void Renderer::render()
+{
+ if (m_exiting)
+ return;
+
+ QOpenGLContext *ctx = m_glwidget->context();
+ if (!ctx) // QOpenGLWidget not yet initialized
+ return;
+
+ // Grab the context.
+ m_grabMutex.lock();
+ emit contextWanted();
+ m_grabCond.wait(&m_grabMutex);
+ QMutexLocker lock(&m_renderMutex);
+ m_grabMutex.unlock();
+
+ if (m_exiting)
+ return;
+
+ Q_ASSERT(ctx->thread() == QThread::currentThread());
+
+ // Make the context (and an offscreen surface) current for this thread. The
+ // QOpenGLWidget's fbo is bound in the context.
+ m_glwidget->makeCurrent();
+
+ if (!m_inited) {
+ m_inited = true;
+ initializeOpenGLFunctions();
+
+ QMutexLocker initLock(initMutex());
+ QOpenGLShader *vshader = new QOpenGLShader(QOpenGLShader::Vertex, this);
+ const char *vsrc =
+ "attribute highp vec4 vertex;\n"
+ "attribute mediump vec3 normal;\n"
+ "uniform mediump mat4 matrix;\n"
+ "varying mediump vec4 color;\n"
+ "void main(void)\n"
+ "{\n"
+ " vec3 toLight = normalize(vec3(0.0, 0.3, 1.0));\n"
+ " float angle = max(dot(normal, toLight), 0.0);\n"
+ " vec3 col = vec3(0.40, 1.0, 0.0);\n"
+ " color = vec4(col * 0.2 + col * 0.8 * angle, 1.0);\n"
+ " color = clamp(color, 0.0, 1.0);\n"
+ " gl_Position = matrix * vertex;\n"
+ "}\n";
+ vshader->compileSourceCode(vsrc);
+
+ QOpenGLShader *fshader = new QOpenGLShader(QOpenGLShader::Fragment, this);
+ const char *fsrc =
+ "varying mediump vec4 color;\n"
+ "void main(void)\n"
+ "{\n"
+ " gl_FragColor = color;\n"
+ "}\n";
+ fshader->compileSourceCode(fsrc);
+
+ program.addShader(vshader);
+ program.addShader(fshader);
+ program.link();
+
+ vertexAttr = program.attributeLocation("vertex");
+ normalAttr = program.attributeLocation("normal");
+ matrixUniform = program.uniformLocation("matrix");
+
+ m_fAngle = 0;
+ m_fScale = 1;
+ createGeometry();
+
+ m_elapsed.start();
+ }
+
+ //qDebug("%p elapsed %lld", QThread::currentThread(), m_elapsed.restart());
+
+ glClearColor(0.1f, 0.2f, 0.2f, 1.0f);
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+ glFrontFace(GL_CW);
+ glCullFace(GL_FRONT);
+ glEnable(GL_CULL_FACE);
+ glEnable(GL_DEPTH_TEST);
+
+ QMatrix4x4 modelview;
+ modelview.rotate(m_fAngle, 0.0f, 1.0f, 0.0f);
+ modelview.rotate(m_fAngle, 1.0f, 0.0f, 0.0f);
+ modelview.rotate(m_fAngle, 0.0f, 0.0f, 1.0f);
+ modelview.scale(m_fScale);
+ modelview.translate(0.0f, -0.2f, 0.0f);
+
+ program.bind();
+ program.setUniformValue(matrixUniform, modelview);
+ paintQtLogo();
+ program.release();
+
+ glDisable(GL_DEPTH_TEST);
+ glDisable(GL_CULL_FACE);
+
+ m_fAngle += 1.0f;
+
+ // Make no context current on this thread and move the QOpenGLWidget's
+ // context back to the gui thread.
+ m_glwidget->doneCurrent();
+ ctx->moveToThread(qGuiApp->thread());
+
+ // Schedule composition. Note that this will use QueuedConnection, meaning
+ // that update() will be invoked on the gui thread.
+ QMetaObject::invokeMethod(m_glwidget, "update");
+}
+
+void Renderer::createGeometry()
+{
+ vertices.clear();
+ normals.clear();
+
+ qreal x1 = +0.06f;
+ qreal y1 = -0.14f;
+ qreal x2 = +0.14f;
+ qreal y2 = -0.06f;
+ qreal x3 = +0.08f;
+ qreal y3 = +0.00f;
+ qreal x4 = +0.30f;
+ qreal y4 = +0.22f;
+
+ quad(x1, y1, x2, y2, y2, x2, y1, x1);
+ quad(x3, y3, x4, y4, y4, x4, y3, x3);
+
+ extrude(x1, y1, x2, y2);
+ extrude(x2, y2, y2, x2);
+ extrude(y2, x2, y1, x1);
+ extrude(y1, x1, x1, y1);
+ extrude(x3, y3, x4, y4);
+ extrude(x4, y4, y4, x4);
+ extrude(y4, x4, y3, x3);
+
+ const qreal Pi = 3.14159f;
+ const int NumSectors = 100;
+
+ for (int i = 0; i < NumSectors; ++i) {
+ qreal angle1 = (i * 2 * Pi) / NumSectors;
+ qreal x5 = 0.30 * sin(angle1);
+ qreal y5 = 0.30 * cos(angle1);
+ qreal x6 = 0.20 * sin(angle1);
+ qreal y6 = 0.20 * cos(angle1);
+
+ qreal angle2 = ((i + 1) * 2 * Pi) / NumSectors;
+ qreal x7 = 0.20 * sin(angle2);
+ qreal y7 = 0.20 * cos(angle2);
+ qreal x8 = 0.30 * sin(angle2);
+ qreal y8 = 0.30 * cos(angle2);
+
+ quad(x5, y5, x6, y6, x7, y7, x8, y8);
+
+ extrude(x6, y6, x7, y7);
+ extrude(x8, y8, x5, y5);
+ }
+
+ for (int i = 0;i < vertices.size();i++)
+ vertices[i] *= 2.0f;
+}
+
+void Renderer::quad(qreal x1, qreal y1, qreal x2, qreal y2, qreal x3, qreal y3, qreal x4, qreal y4)
+{
+ vertices << QVector3D(x1, y1, -0.05f);
+ vertices << QVector3D(x2, y2, -0.05f);
+ vertices << QVector3D(x4, y4, -0.05f);
+
+ vertices << QVector3D(x3, y3, -0.05f);
+ vertices << QVector3D(x4, y4, -0.05f);
+ vertices << QVector3D(x2, y2, -0.05f);
+
+ QVector3D n = QVector3D::normal
+ (QVector3D(x2 - x1, y2 - y1, 0.0f), QVector3D(x4 - x1, y4 - y1, 0.0f));
+
+ normals << n;
+ normals << n;
+ normals << n;
+
+ normals << n;
+ normals << n;
+ normals << n;
+
+ vertices << QVector3D(x4, y4, 0.05f);
+ vertices << QVector3D(x2, y2, 0.05f);
+ vertices << QVector3D(x1, y1, 0.05f);
+
+ vertices << QVector3D(x2, y2, 0.05f);
+ vertices << QVector3D(x4, y4, 0.05f);
+ vertices << QVector3D(x3, y3, 0.05f);
+
+ n = QVector3D::normal
+ (QVector3D(x2 - x4, y2 - y4, 0.0f), QVector3D(x1 - x4, y1 - y4, 0.0f));
+
+ normals << n;
+ normals << n;
+ normals << n;
+
+ normals << n;
+ normals << n;
+ normals << n;
+}
+
+void Renderer::extrude(qreal x1, qreal y1, qreal x2, qreal y2)
+{
+ vertices << QVector3D(x1, y1, +0.05f);
+ vertices << QVector3D(x2, y2, +0.05f);
+ vertices << QVector3D(x1, y1, -0.05f);
+
+ vertices << QVector3D(x2, y2, -0.05f);
+ vertices << QVector3D(x1, y1, -0.05f);
+ vertices << QVector3D(x2, y2, +0.05f);
+
+ QVector3D n = QVector3D::normal
+ (QVector3D(x2 - x1, y2 - y1, 0.0f), QVector3D(0.0f, 0.0f, -0.1f));
+
+ normals << n;
+ normals << n;
+ normals << n;
+
+ normals << n;
+ normals << n;
+ normals << n;
+}
diff --git a/examples/opengl/threadedqopenglwidget/glwidget.h b/examples/opengl/threadedqopenglwidget/glwidget.h
new file mode 100644
index 0000000000..9d746e681b
--- /dev/null
+++ b/examples/opengl/threadedqopenglwidget/glwidget.h
@@ -0,0 +1,124 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
+** of its contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef GLWIDGET_H
+#define GLWIDGET_H
+
+#include <QOpenGLWidget>
+#include <QOpenGLFunctions>
+#include <QOpenGLShaderProgram>
+#include <QVector3D>
+#include <QMatrix4x4>
+#include <QThread>
+#include <QMutex>
+#include <QWaitCondition>
+#include <QElapsedTimer>
+
+class GLWidget;
+
+class Renderer : public QObject, protected QOpenGLFunctions
+{
+ Q_OBJECT
+
+public:
+ Renderer(GLWidget *w);
+ void lockRenderer() { m_renderMutex.lock(); }
+ void unlockRenderer() { m_renderMutex.unlock(); }
+ QMutex *grabMutex() { return &m_grabMutex; }
+ QWaitCondition *grabCond() { return &m_grabCond; }
+ void prepareExit() { m_exiting = true; m_grabCond.wakeAll(); }
+
+signals:
+ void contextWanted();
+
+public slots:
+ void render();
+
+private:
+ void paintQtLogo();
+ void createGeometry();
+ void quad(qreal x1, qreal y1, qreal x2, qreal y2, qreal x3, qreal y3, qreal x4, qreal y4);
+ void extrude(qreal x1, qreal y1, qreal x2, qreal y2);
+
+ bool m_inited;
+ qreal m_fAngle;
+ qreal m_fScale;
+ QVector<QVector3D> vertices;
+ QVector<QVector3D> normals;
+ QOpenGLShaderProgram program;
+ int vertexAttr;
+ int normalAttr;
+ int matrixUniform;
+ GLWidget *m_glwidget;
+ QMutex m_renderMutex;
+ QElapsedTimer m_elapsed;
+ QMutex m_grabMutex;
+ QWaitCondition m_grabCond;
+ bool m_exiting;
+};
+
+class GLWidget : public QOpenGLWidget
+{
+ Q_OBJECT
+public:
+ GLWidget(QWidget *parent);
+ ~GLWidget();
+
+protected:
+ void paintEvent(QPaintEvent *) Q_DECL_OVERRIDE { }
+
+signals:
+ void renderRequested();
+
+public slots:
+ void grabContext();
+
+private slots:
+ void onAboutToCompose();
+ void onFrameSwapped();
+ void onAboutToResize();
+ void onResized();
+
+private:
+ QThread *m_thread;
+ Renderer *m_renderer;
+};
+
+#endif
diff --git a/examples/opengl/threadedqopenglwidget/main.cpp b/examples/opengl/threadedqopenglwidget/main.cpp
new file mode 100644
index 0000000000..046a24d576
--- /dev/null
+++ b/examples/opengl/threadedqopenglwidget/main.cpp
@@ -0,0 +1,69 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
+** of its contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QApplication>
+#include <QMainWindow>
+#include "mainwindow.h"
+#include "glwidget.h"
+
+int main( int argc, char ** argv )
+{
+ QApplication a( argc, argv );
+
+ // Two top-level windows with two QOpenGLWidget children in each.
+ // The rendering for the four QOpenGLWidgets happens on four separate threads.
+ MainWindow mw1;
+ mw1.setMinimumSize(800, 400);
+ mw1.show();
+
+ QScopedPointer<MainWindow> mw2;
+ if (!QApplication::arguments().contains(QStringLiteral("--single"))) {
+ mw2.reset(new MainWindow);
+ mw2->setMinimumSize(800, 400);
+ mw2->show();
+
+ // And a top-level.
+ GLWidget *bonus = new GLWidget(0);
+ bonus->resize(200, 200);
+ bonus->show();
+ }
+
+ return a.exec();
+}
diff --git a/examples/opengl/threadedqopenglwidget/mainwindow.cpp b/examples/opengl/threadedqopenglwidget/mainwindow.cpp
new file mode 100644
index 0000000000..29c59573cf
--- /dev/null
+++ b/examples/opengl/threadedqopenglwidget/mainwindow.cpp
@@ -0,0 +1,52 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
+** of its contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "mainwindow.h"
+#include "glwidget.h"
+
+MainWindow::MainWindow()
+{
+ GLWidget *glwidget1 = new GLWidget(this);
+ glwidget1->resize(400, 400);
+
+ GLWidget *glwidget2 = new GLWidget(this);
+ glwidget2->resize(400, 400);
+ glwidget2->move(400, 0);
+}
diff --git a/examples/opengl/threadedqopenglwidget/mainwindow.h b/examples/opengl/threadedqopenglwidget/mainwindow.h
new file mode 100644
index 0000000000..91f7580717
--- /dev/null
+++ b/examples/opengl/threadedqopenglwidget/mainwindow.h
@@ -0,0 +1,54 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
+** of its contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef MAINWINDOW_H
+#define MAINWINDOW_H
+
+#include <QWidget>
+
+class MainWindow : public QWidget
+{
+ Q_OBJECT
+
+public:
+ MainWindow();
+};
+
+#endif
diff --git a/examples/opengl/threadedqopenglwidget/threadedqopenglwidget.pro b/examples/opengl/threadedqopenglwidget/threadedqopenglwidget.pro
new file mode 100644
index 0000000000..4e941b28c4
--- /dev/null
+++ b/examples/opengl/threadedqopenglwidget/threadedqopenglwidget.pro
@@ -0,0 +1,11 @@
+QT += widgets
+
+SOURCES += main.cpp \
+ glwidget.cpp \
+ mainwindow.cpp
+
+HEADERS += glwidget.h \
+ mainwindow.h
+
+target.path = $$[QT_INSTALL_EXAMPLES]/opengl/threadedqopenglwidget
+INSTALLS += target