Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
doc_gui_widgets_qopenglwidget.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4//! [0]
6{
7public:
8 MyGLWidget(QWidget *parent) : QOpenGLWidget(parent) { }
9
10protected:
12 {
13 // Set up the rendering context, load shaders and other resources, etc.:
14 QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
15 f->glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
16 ...
17 }
18
19 void resizeGL(int w, int h) override
20 {
21 // Update projection matrix and other size related settings:
22 m_projection.setToIdentity();
23 m_projection.perspective(45.0f, w / float(h), 0.01f, 100.0f);
24 ...
25 }
26
28 {
29 // Draw the scene:
30 QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
31 f->glClear(GL_COLOR_BUFFER_BIT);
32 ...
33 }
34
35};
36//! [0]
37
38//! [1]
39class MyGLWidget : public QOpenGLWidget, protected QOpenGLFunctions
40{
41 ...
42 void initializeGL() override
43 {
44 initializeOpenGLFunctions();
45 glClearColor(...);
46 ...
47 }
48 ...
49};
50//! [1]
51
52//! [2]
53QOpenGLWidget *widget = new QOpenGLWidget(parent);
55format.setDepthBufferSize(24);
56format.setStencilBufferSize(8);
57format.setVersion(3, 2);
58format.setProfile(QSurfaceFormat::CoreProfile);
59widget->setFormat(format); // must be called before the widget or its parent window gets shown
60//! [2]
61
62//! [3]
63 ...
64 void paintGL() override
65 {
66 QOpenGLFunctions_3_2_Core *f = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_2_Core>();
67 ...
68 f->glDrawArraysInstanced(...);
69 ...
70 }
71 ...
72//! [3]
73
74//! [4]
75class MyGLWidget : public QOpenGLWidget
76{
77 ...
78
79private:
80 QOpenGLVertexArrayObject m_vao;
81 QOpenGLBuffer m_vbo;
82 QOpenGLShaderProgram *m_program;
83 QOpenGLShader *m_shader;
84 QOpenGLTexture *m_texture;
85};
86
87MyGLWidget::MyGLWidget()
88 : m_program(0), m_shader(0), m_texture(0)
89{
90 // No OpenGL resource initialization is done here.
91}
92
93MyGLWidget::~MyGLWidget()
94{
95 // Make sure the context is current and then explicitly
96 // destroy all underlying OpenGL resources.
97 makeCurrent();
98
99 delete m_texture;
100 delete m_shader;
101 delete m_program;
102
103 m_vbo.destroy();
104 m_vao.destroy();
105
106 doneCurrent();
107}
108
110{
111 m_vao.create();
112 if (m_vao.isCreated())
113 m_vao.bind();
114
115 m_vbo.create();
116 m_vbo.bind();
117 m_vbo.allocate(...);
118
119 m_texture = new QOpenGLTexture(QImage(...));
120
121 m_shader = new QOpenGLShader(...);
122 m_program = new QOpenGLShaderProgram(...);
123
124 ...
125}
126//! [4]
127
128//! [5]
129MyGLWidget::~MyGLWidget()
130{
131 cleanup();
132}
133
135{
136 ...
137 connect(context(), &QOpenGLContext::aboutToBeDestroyed, this, &MyGLWidget::cleanup);
138}
139
140void MyGLWidget::cleanup()
141{
142 makeCurrent();
143 delete m_texture;
144 m_texture = 0;
145 ...
146 doneCurrent();
147 disconnect(context(), &QOpenGLContext::aboutToBeDestroyed, this, &MyGLWidget::cleanup);
148}
149//! [5]
150
151//! [6]
152int main(int argc, char **argv)
153{
154 QApplication app(argc, argv);
155
156 QSurfaceFormat format;
157 format.setDepthBufferSize(24);
158 format.setStencilBufferSize(8);
159 format.setVersion(3, 2);
160 format.setProfile(QSurfaceFormat::CoreProfile);
161 QSurfaceFormat::setDefaultFormat(format);
162
163 MyWidget widget;
164 widget.show();
165
166 return app.exec();
167}
168//! [6]
void paintGL() override
This virtual function is called whenever the widget needs to be painted.
void initializeGL() override
This virtual function is called once before the first call to paintGL() or resizeGL().
MyGLWidget(QWidget *parent)
void resizeGL(int w, int h) override
This virtual function is called whenever the widget has been resized.
QOpenGLWidget * widget
[1]
int main(int argc, char *argv[])
[ctor_close]
QImage::Format format
Definition qimage_p.h:52