Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
6{
7public:
9
10protected:
11 void initializeGL() override
12 {
13 // Set up the rendering context, load shaders and other resources, etc.:
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
27 void paintGL() override
28 {
29 // Draw the scene:
31 f->glClear(GL_COLOR_BUFFER_BIT);
32 ...
33 }
34
35};
37
39class MyGLWidget : public QOpenGLWidget, protected QOpenGLFunctions
40{
41 ...
42 void initializeGL() override
43 {
45 glClearColor(...);
46 ...
47 }
48 ...
49};
51
56format.setStencilBufferSize(8);
57format.setVersion(3, 2);
59widget->setFormat(format); // must be called before the widget or its parent window gets shown
61
63 ...
64 void paintGL() override
65 {
67 ...
69 ...
70 }
71 ...
73
75class MyGLWidget : public QOpenGLWidget
76{
77 ...
78
79private:
81 QOpenGLBuffer m_vbo;
82 QOpenGLShaderProgram *m_program;
83 QOpenGLShader *m_shader;
84 QOpenGLTexture *m_texture;
85};
86
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.
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}
127
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}
150
152int main(int argc, char **argv)
153{
154 QApplication app(argc, argv);
155
158 format.setStencilBufferSize(8);
159 format.setVersion(3, 2);
162
164 widget.show();
165
166 return app.exec();
167}
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.
The QApplication class manages the GUI application's control flow and main settings.
static int exec()
Enters the main event loop and waits until exit() is called, then returns the value that was set to e...
\inmodule QtGui
Definition qimage.h:37
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
The QOpenGLBuffer class provides functions for creating and managing OpenGL buffer objects.
bool bind()
Binds the buffer associated with this object to the current OpenGL context.
bool create()
Creates the buffer object in the OpenGL server.
void allocate(const void *data, int count)
Allocates count bytes of space to the buffer, initialized to the contents of data.
void destroy()
Destroys this buffer object, including the storage being used in the OpenGL server.
void aboutToBeDestroyed()
This signal is emitted before the underlying native OpenGL context is destroyed, such that users may ...
static QOpenGLContext * currentContext()
Returns the last context which called makeCurrent in the current thread, or \nullptr,...
void glDrawArraysInstanced(GLenum mode, GLint first, GLsizei count, GLsizei instancecount)
The QOpenGLFunctions class provides cross-platform access to the OpenGL ES 2.0 API.
void glClearColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
Convenience function that calls glClearColor(red, green, blue, alpha).
void initializeOpenGLFunctions()
Initializes OpenGL function resolution for the current context.
The QOpenGLShaderProgram class allows OpenGL shader programs to be linked and used.
The QOpenGLShader class allows OpenGL shaders to be compiled.
\inmodule QtGui
The QOpenGLVertexArrayObject class wraps an OpenGL Vertex Array Object.
void destroy()
Destroys the underlying OpenGL vertex array object.
bool create()
Creates the underlying OpenGL vertex array object.
bool isCreated() const
Returns true is the underlying OpenGL vertex array object has been created.
void bind()
Binds this vertex array object to the OpenGL binding point.
\inmodule QtOpenGLWidgets
QOpenGLContext * context() const
void setFormat(const QSurfaceFormat &format)
Sets the requested surface format.
void doneCurrent()
Releases the context.
void makeCurrent()
Prepares for rendering OpenGL content for this widget by making the corresponding context current and...
The QSurfaceFormat class represents the format of a QSurface. \inmodule QtGui.
void setDepthBufferSize(int size)
Set the minimum depth buffer size to size.
static void setDefaultFormat(const QSurfaceFormat &format)
Sets the global default surface format.
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
void show()
Shows the widget and its child widgets.
Definition qwidget.cpp:7875
void paintGL() override
[2]
QSurfaceFormat format
QOpenGLWidget * widget
[1]
int main()
[0]
GLfloat GLfloat GLfloat w
[0]
GLfloat GLfloat f
GLint GLsizei GLsizei GLenum format
GLfloat GLfloat GLfloat GLfloat h
myObject disconnect()
[26]
QApplication app(argc, argv)
[0]