summaryrefslogtreecommitdiffstats
path: root/src/widgets/doc/snippets/code
diff options
context:
space:
mode:
authorLaszlo Agocs <laszlo.agocs@digia.com>2014-08-03 15:22:43 +0200
committerLaszlo Agocs <laszlo.agocs@digia.com>2014-08-05 16:47:59 +0200
commit68c9a2f82d873068c3adbde520c76d81d0a2dd62 (patch)
tree2e66ef94c44ead250ed431138f111eaa82ed1af1 /src/widgets/doc/snippets/code
parent34fbc61f22f185bc4ef542132111d045956f5011 (diff)
Enhance QOpenGLWidget docs about resource management
Change-Id: Idd1181a34055237b13643dbc58e855db411d0a7c Reviewed-by: Gunnar Sletta <gunnar.sletta@jollamobile.com>
Diffstat (limited to 'src/widgets/doc/snippets/code')
-rw-r--r--src/widgets/doc/snippets/code/doc_gui_widgets_qopenglwidget.cpp71
1 files changed, 71 insertions, 0 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 bc279e4406..0814b5f30f 100644
--- a/src/widgets/doc/snippets/code/doc_gui_widgets_qopenglwidget.cpp
+++ b/src/widgets/doc/snippets/code/doc_gui_widgets_qopenglwidget.cpp
@@ -107,3 +107,74 @@ widget->setFormat(format); // must be called before the widget or its parent win
}
...
//! [3]
+
+//! [4]
+class MyGLWidget : public QOpenGLWidget
+{
+ ...
+
+private:
+ QOpenGLVertexArrayObject m_vao;
+ QOpenGLBuffer m_vbo;
+ QOpenGLShaderProgram *m_program;
+ QOpenGLShader *m_shader;
+ QOpenGLTexture *m_texture;
+};
+
+MyGLWidget::MyGLWidget()
+ : m_program(0), m_shader(0), m_texture(0)
+{
+ // No OpenGL resource initialization is done here.
+}
+
+MyGLWidget::~MyGLWidget()
+{
+ // Make sure the context is current and then explicitly
+ // destroy all underlying OpenGL resources.
+ makeCurrent();
+
+ delete m_texture;
+ delete m_shader;
+ delete m_program;
+
+ m_vbo.destroy();
+ m_vao.destroy();
+
+ doneCurrent();
+}
+
+void MyGLWidget::initializeGL()
+{
+ m_vao.create();
+ if (m_vao.isCreated())
+ m_vao.bind();
+
+ m_vbo.create();
+ m_vbo.bind();
+ m_vbo.allocate(...);
+
+ m_texture = new QOpenGLTexture(QImage(...));
+
+ m_shader = new QOpenGLShader(...);
+ m_program = new QOpenGLShaderProgram(...);
+
+ ...
+}
+//! [4]
+
+//! [5]
+void MyGLWidget::initializeGL()
+{
+ // context() and QOpenGLContext::currentContext() are equivalent when called from initializeGL or paintGL.
+ connect(context(), &QOpenGLContext::aboutToBeDestroyed, this, &MyGLWidget::cleanup);
+}
+
+void MyGLWidget::cleanup()
+{
+ makeCurrent();
+ delete m_texture;
+ m_texture = 0;
+ ...
+ doneCurrent();
+}
+//! [5]