- Qt 5.15
- Qt Platform Headers
- C++ Classes
- QWGLNativeContext
- Qt 5.15.15 Reference Documentation

# QWGLNativeContext Class

A class encapsulating a WGL context on Windows with desktop OpenGL (opengl32.dll). More...

Header: | #include <QWGLNativeContext>
Since: | Qt 5.4

This class was introduced in Qt 5.4.

- List of all members, including inherited members

## Public Functions

| QWGLNativeContext(HGLRC ctx, HWND wnd)
| QWGLNativeContext()
HGLRC | context() const
HWND | window() const

## Detailed Description

Note: There is no binary compatibility guarantee for this class, meaning that an application using it is only guaranteed to work with the Qt version it was developed against.

QWGLNativeContext is a value class that can be passed to QOpenGLContext::setNativeHandle(). When creating a QOpenGLContext with the native handle set, no new context will get created. Instead, the provided handles are used, without taking ownership. This allows wrapping a context created by an external framework or rendering engine. The typical usage will be similar to the following snippet:

     #include <QtPlatformSupport/QWGLNativeContext>
     ...create and retrieve the WGL context and the corresponding window...
     QOpenGLContext *context = new QOpenGLContext;
     QWGLNativeContext nativeContext(hglrc, hwnd);
     context->setNativeHandle(QVariant::fromValue(nativeContext));
     context->create();
     ...

The window is needed because the its pixel format will be queried. When the adoption is successful, QOpenGLContext::format() will return a QSurfaceFormat describing this pixel format.

It is recommended to restrict the usage of QOpenGLContexts created this way. Various platform-specific behavior and issues may prevent such contexts to be made current with windows (surfaces) created by Qt due to non-matching pixel formats for example. A potentially safer solution is to use the wrapped context only to set up sharing and perform Qt-based rendering offscreen, using a separate, dedicated QOpenGLContext. The resulting textures are then accessible in the foreign context too.

     ...like above...
     QOpenGLContext *qtcontext = new QOpenGLContext;
     qtcontext->setShareContext(context);
     qtcontext->setFormat(context->format());
     qtcontext->create();
     ...use qtcontext for rendering with Qt...

In addition to being used with QOpenGLContext::setNativeHandle(), this class is used also to retrieve the native context handle, that is, a HGLRC value, from a QOpenGLContext. Calling QOpenGLContext::nativeHandle() returns a QVariant which, on Windows with opengl32.dll at least, will contain a QWGLNativeContext:

     QVariant nativeHandle = context->nativeHandle();
     if (!nativeHandle.isNull() && nativeHandle.canConvert<QWGLNativeContext>()) {
         QWGLNativeContext nativeContext = nativeHandle.value<QWGLNativeContext>();
         HGLRC hglrc = nativeContext.context();
         ...
     }

See also QOpenGLContext::setNativeHandle() and QOpenGLContext::nativeHandle().

## Member Function Documentation

### QWGLNativeContext::QWGLNativeContext(HGLRC ctx, HWND wnd)

Constructs a new instance with the provided ctx context handle and wnd window handle.

Note: The window specified by wnd must have its pixel format set to a format compatible with the context's. If no SetPixelFormat() call was made on any device context belonging to the window, adopting the context will fail.

### QWGLNativeContext::QWGLNativeContext()

Construct a new instance with no handles.

### HGLRC QWGLNativeContext::context() const

Returns the WGL context.

### HWND QWGLNativeContext::window() const

Note: The window handle is not available when the QWGLNativeContext is queried from a regular, non-adopted QOpenGLContext using QOpenGLContext::nativeHandle(). This is because the windows platform plugin creates WGL contexts using a dummy window that is not available afterwards. Instead, the native window handle (HWND) is queriable from a QWindow via QPlatformNativeInterface::nativeResourceForWindow() using the "handle" resource key. Note however that the window will not have its pixel format set until it is first associated with a context via QOpenGLContext::makeCurrent().

Returns handle for the window for which the context was created.