6.0.0-rc4Source

QOpenGLContext Class

The QOpenGLContext class represents a native OpenGL context, which renders with OpenGL on a QSurface. More…

Header#include <QOpenGLContext>
CMaketarget_link_libraries(app PRIVATE Nt::Gui)
ModuleNt GUI C++ Classes
Since6.0
InheritsQObject

List of all members, including inherited members

Public Types

Public Functions

QOpenGLContext(QObject *parent = nullptr)
~QOpenGLContext()
voidsetFormat(const QSurfaceFormat &format)
voidsetShareContext(QOpenGLContext *shareContext)
voidsetScreen(QScreen *screen)
boolcreate()
boolisValid() const
QSurfaceFormatformat() const
QOpenGLContext *shareContext() const
QScreen *screen() const
GLuintdefaultFramebufferObject() const
boolmakeCurrent(QSurface *surface)
voiddoneCurrent()
voidswapBuffers(QSurface *surface)
QFunctionPointergetProcAddress(const QByteArray &procName) const
QFunctionPointergetProcAddress(const char *procName) const
QSurface *surface() const
QOpenGLFunctions *functions() const
QSet<QByteArray>extensions() const
boolhasExtension(const QByteArray &extension) const
boolisOpenGLES() const

Signals

Static Public Members

QOpenGLContext *currentContext()
boolareSharing(QOpenGLContext *first, QOpenGLContext *second)
OpenGLModuleTypeopenGLModuleType()
boolsupportsThreadedOpenGL()

Detailed Description

Set the format of the context, create it, and make it current on a window whose surface type is QSurface::OpenGLSurface, or on a QOffscreenSurface. Then call OpenGL through functions(), and show the frame with swapBuffers():

QWindow window;
window.setSurfaceType(QSurface::OpenGLSurface);
window.show();

QOpenGLContext context;
context.setFormat(window.requestedFormat());
context.create();
context.makeCurrent(&window);
QOpenGLFunctions *f = context.functions();
f->glClearColor(0.2f, 0.3f, 0.4f, 1.0f);
f->glClear(GL_COLOR_BUFFER_BIT);
context.swapBuffers(&window);

QOpenGLWindow does all of this for a window that only renders with OpenGL, and QOpenGLPaintDevice lets a QPainter draw with OpenGL.

Nt reaches OpenGL through EGL, which it loads when the first context is created: a program that never uses OpenGL needs no OpenGL library. On the xcb platform contexts render into the window; on the offscreen platform, which has no window system, each window renders into a framebuffer object, which defaultFramebufferObject() returns while the context is current on the window.

See also QOpenGLFunctions, QSurfaceFormat, QOffscreenSurface and QOpenGLWindow.

Differences from Qt

Contexts are created through EGL only: there is no GLX, WGL, CGL or native handle, no QOpenGLContextGroup, no globalShareContext() and no extraFunctions() or versioned function classes. Unless the format asks for OpenGL ES, the context is desktop OpenGL when EGL offers it. The screen is only remembered.

Member Type Documentation

enum QOpenGLContext::OpenGLModuleType

This enum describes the kind of OpenGL library contexts use by default.

ConstantDescription
QOpenGLContext::LibGLDesktop OpenGL.
QOpenGLContext::LibGLESOpenGL ES.

Member Function Documentation

[explicit] QOpenGLContext::QOpenGLContext(QObject *parent = nullptr)

Constructs a context with the given parent. Call create() before using it.

[override] QOpenGLContext::~QOpenGLContext()

Destroys the context, after emitting aboutToBeDestroyed(). If the context is current in this thread, it is first released.

[signal] void QOpenGLContext::aboutToBeDestroyed()

This signal is emitted when the context is about to be destroyed. Make it current in a slot to delete the OpenGL objects it has made.

[static] bool QOpenGLContext::areSharing(QOpenGLContext *first, QOpenGLContext *second)

Returns true if first and second share their OpenGL objects, which is when they are the same context or one was created to share with the other, directly or through others.

bool QOpenGLContext::create()

Creates the native context with the format, share context and screen that were set, and returns true if that worked. If the context exists already, it is created anew.

See also isValid() and format().

[static] QOpenGLContext * QOpenGLContext::currentContext()

Returns the context that is current in this thread, or nullptr.

GLuint QOpenGLContext::defaultFramebufferObject() const

Returns the name of the framebuffer object that is the window's surface while the context is current on a window that has no native OpenGL surface, as on the offscreen platform; otherwise returns 0, the framebuffer of the window. Bind it again after drawing into a framebuffer object of your own.

See also makeCurrent().

void QOpenGLContext::doneCurrent()

Releases the context from this thread, if it is current here.

See also makeCurrent().

QSet<QByteArray> QOpenGLContext::extensions() const

Returns the names of the OpenGL extensions of the context. The context must be current.

See also hasExtension().

QSurfaceFormat QOpenGLContext::format() const

Returns the format of the context: the requested format before create(), and then the buffers of the native context. Once the context has been made current, the version, profile and renderable type are those of the context OpenGL gave.

See also setFormat().

QOpenGLFunctions * QOpenGLContext::functions() const

Returns the OpenGL ES 2.0 functions of the context. They can be called once the context has been made current.

QFunctionPointer QOpenGLContext::getProcAddress(const QByteArray &procName) const

This is an overloaded function.

Returns the address of the OpenGL function procName, or nullptr.

QFunctionPointer QOpenGLContext::getProcAddress(const char *procName) const

Returns the address of the OpenGL function procName, or nullptr if there is none.

bool QOpenGLContext::hasExtension(const QByteArray &extension) const

Returns true if the context has the OpenGL extension extension. The context must be current.

See also extensions().

bool QOpenGLContext::isOpenGLES() const

Returns true if the context is OpenGL ES rather than desktop OpenGL.

bool QOpenGLContext::isValid() const

Returns true if create() made the native context.

bool QOpenGLContext::makeCurrent(QSurface *surface)

Makes the context current in this thread on surface, and returns true if it succeeded. A window must have the surface type QSurface::OpenGLSurface; it is created if it has not been.

See also doneCurrent(), currentContext() and swapBuffers().

[static] OpenGLModuleType QOpenGLContext::openGLModuleType()

Returns the kind of OpenGL that contexts use when their format does not ask for one: desktop OpenGL if the EGL display offers it.

QScreen * QOpenGLContext::screen() const

Returns the screen of the context: the primary screen unless setScreen() set another.

void QOpenGLContext::setFormat(const QSurfaceFormat &format)

Sets the format the context is created with. Call it before create().

See also format() and create().

void QOpenGLContext::setScreen(QScreen *screen)

Sets the screen of the context.

See also screen().

void QOpenGLContext::setShareContext(QOpenGLContext *shareContext)

Makes the context share its textures, buffers and other objects with shareContext. Call it before create().

See also shareContext() and areSharing().

QOpenGLContext * QOpenGLContext::shareContext() const

Returns the context set with setShareContext(), or nullptr.

[static] bool QOpenGLContext::supportsThreadedOpenGL()

Returns true: contexts can be current in threads other than the main thread, one thread at a time.

QSurface * QOpenGLContext::surface() const

Returns the surface the context is current on, or nullptr.

void QOpenGLContext::swapBuffers(QSurface *surface)

Shows what was rendered into the window surface. The context must be current on it. With a swap interval of 1, the default, this waits for the display to show the frame; QSurfaceFormat::setSwapInterval() with 0 lets rendering run as fast as it can. For an offscreen surface, or a window that renders into a framebuffer object, it only flushes OpenGL.

See also makeCurrent() and QSurfaceFormat::setSwapInterval().