CopperSpice API  1.8.2
QOpenGLContext Class Reference

Represents a native OpenGL context which is used to render OpenGL content on a QSurface. More...

Inheritance diagram for QOpenGLContext:
QObject

Public Methods

 QOpenGLContext (QObject *parent=nullptr)
 
 ~QOpenGLContext ()
 
bool create ()
 
GLuint defaultFramebufferObject () const
 
void doneCurrent ()
 
QSet< QByteArrayextensions () const
 
QOpenGLExtraFunctionsextraFunctions () const
 
QSurfaceFormat format () const
 
QOpenGLFunctionsfunctions () const
 
bool hasExtension (const QByteArray &extension) const
 
bool isOpenGLES () const
 
bool makeCurrent (QSurface *surface)
 
QScreenscreen () const
 
void setFormat (const QSurfaceFormat &format)
 
void setScreen (QScreen *screen)
 
void setShareContext (QOpenGLContext *shareContext)
 
QOpenGLContext * shareContext () const
 
QOpenGLContextGroupshareGroup () const
 
- Public Methods inherited from QObject
 QObject (QObject *parent=nullptr)
 
 ~QObject ()
 
bool blockSignals (bool block)
 
const QList< QObject * > & children () const
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool disconnect (const QObject *receiver, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod, const QString &location, const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod=QString (), const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
void dumpObjectInfo ()
 
void dumpObjectTree ()
 
QList< QStringdynamicPropertyNames () const
 
virtual bool event (QEvent *event)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
template<typename T >
findChild (const QString &childName=QString ()) const
 
template<class T >
QList< T > findChildren (const QRegularExpression &regExp, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
template<class T >
QList< T > findChildren (const QString &childName=QString (), Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
bool inherits (const QString &className) const
 
void installEventFilter (QObject *filterObj)
 
bool isWidgetType () const
 
bool isWindowType () const
 
void killTimer (int id)
 
const QMetaObjectmetaObject () const
 
void moveToThread (QThread *targetThread)
 
QString objectName () const
 
QObject * parent () const
 
template<class T = QVariant>
property (const QString &name) const
 
void removeEventFilter (QObject *obj)
 
void setObjectName (const QString &name)
 
void setParent (QObject *parent)
 
bool setProperty (const QString &name, const QVariant &value)
 
bool signalsBlocked () const
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 
QThreadthread () const
 

Static Public Methods

static QOpenGLContext * currentContext ()
 
static QOpenGLContext * globalShareContext ()
 
static OpenGLModuleType openGLModuleType ()
 
- Static Public Methods inherited from QObject
static bool connect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool connect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection, const QString &location=QString ())
 
static bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...), Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotLambda, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod)
 
static bool disconnect (const QObject *sender, std::nullptr_t, const QObject *receiver, std::nullptr_t)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...))
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, std::nullptr_t slotMethod=nullptr)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotMethod)
 
static QMetaObjectstaticMetaObject ()
 
static QString tr (const char *text, const char *comment=nullptr, std::optional< int > numArg=std::optional< int >())
 

Friends

class QGLContext
 
class QGLPixelBuffer
 
class QOpenGLFramebufferObject
 

Additional Inherited Members

- Public Signals inherited from QObject
void destroyed (QObject *obj=nullptr)
 
void objectNameChanged (const QString &objectName)
 
- Public Slots inherited from QObject
void deleteLater ()
 
- Protected Methods inherited from QObject
virtual void childEvent (QChildEvent *event)
 
virtual void connectNotify (const QMetaMethod &signalMethod) const
 
virtual void customEvent (QEvent *event)
 
virtual void disconnectNotify (const QMetaMethod &signalMethod) const
 
bool isSignalConnected (const QMetaMethod &signalMethod) const
 
int receivers (const QString &signal) const
 
QObject * sender () const
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *event)
 
- Properties inherited from QObject
 objectName
 

Detailed Description

The QOpenGLContext class represents a native OpenGL context which is used to render OpenGL content on a QSurface.

This class represents the OpenGL state of an underlying OpenGL context. To configure a context set the screen and format to match the target surface. A surface is a specific region of the screen where OpenGL content will be displayed. It may be necessary to share resources by calling setShareContext(). Once the context has been configured then call create(). Check the return value or call isValid() to determine if the context was successfully initialized.

A context can be activated for a given surface by calling makeCurrent(). When OpenGL rendering is complete call swapBuffers() to swap the front and back buffers of the surface. At this point the new content becomes visible to the user. QOpenGLContext requires calling makeCurrent() one more time before rendering a new frame.

If the context is temporarily unused, it can be useful to delete the context to free resources. Your application should connect to the aboutToBeDestroyed() signal to clean up any resources which have been allocated.

Once a QOpenGLContext is marked activated your application can render to the context using the CopperSpice OpenGL classes like QOpenGLFunctions, QOpenGLBuffer, QOpenGLShaderProgram, and QOpenGLFramebufferObject. Another option is to call functions in the C OpenGL API directly. The latter is necessary when using OpenGL 1.x or OpenGL ES 1.x.

Constructor & Destructor Documentation

QOpenGLContext::QOpenGLContext ( QObject parent = nullptr)
explicit

Creates a new OpenGL context with the given parent. Before the context can be used call setFormat() and then create().

See also
create(), makeCurrent()
QOpenGLContext::~QOpenGLContext ( )

Destroys the QOpenGLContext object. If this QOpenGLContext is active for the current thread then doneCurrent() will be called.

Method Documentation

bool QOpenGLContext::create ( )

Attempts to create the OpenGL context with the current configuration. The current configuration includes the format, the share context, and the screen. Returns true if the native context was successfully created and is ready to be used with makeCurrent(), swapBuffers(), etc.

If the OpenGL implementation on your system does not support the requested version of OpenGL context, then QOpenGLContext will try to create the closest matching version. The actual created context properties can be queried using the QSurfaceFormat returned by the format() function. For example, if you request a context that supports OpenGL 4.3 Core profile but the driver and/or hardware only supports version 3.2 Core profile contexts then you will get a 3.2 Core profile context.

If the context already exists, this method destroys the existing context first and then creates a new one.

See also
format(), makeCurrent()
QOpenGLContext * QOpenGLContext::currentContext ( )
static

Returns the last context which called makeCurrent() in the current thread or a nullptr if no context is active.

GLuint QOpenGLContext::defaultFramebufferObject ( ) const

This method returns the default framebuffer object for the current surface.

On some platforms the default framebuffer object depends on the surface being rendered to and might not have an index of 0. Instead of calling glBindFramebuffer(0) call glBindFramebuffer(ctx->defaultFramebufferObject()). If you use the glBindFramebuffer() in QOpenGLFunctions the context frame buffer object will be set correctly.

Note
Widgets which render via framebuffer objects like QOpenGLWidget will override the value returned from this method when painting is active. At that time the correct "default" framebuffer is the widget's associated backing framebuffer, not the platform specific one belonging to the top-level window's surface. This ensures the expected behavior for this method and other classes relying on it.
See also
QOpenGLFramebufferObject
void QOpenGLContext::doneCurrent ( )

Equivalent to calling makeCurrent() with a null surface. Calling this method results in no context being current in the current thread.

See also
currentContext()
QSet< QByteArray > QOpenGLContext::extensions ( ) const

Returns the set of OpenGL extensions supported by this context. The context or a sharing context must be current.

See also
hasExtension()
QOpenGLExtraFunctions * QOpenGLContext::extraFunctions ( ) const

Returns the QOpenGLExtraFunctions instance for this context. QOpenGLContext offers this as a convenient way to access QOpenGLExtraFunctions without having to manage it manually. The context or a sharing context must be current. The returned QOpenGLExtraFunctions instance is ready to be used and it does not need initializeOpenGLFunctions() to be called.

Note
QOpenGLExtraFunctions contains functionality that is not guaranteed to be available at runtime. Runtime availability depends on the platform, graphics driver, and the OpenGL version requested by the application.
See also
QOpenGLFunctions, QOpenGLExtraFunctions
QSurfaceFormat QOpenGLContext::format ( ) const

Returns the format of the underlying platform context, if create() has been called. Otherwise, returns the requested format.

The requested and the actual format may differ. Requesting a given OpenGL version does not mean the resulting context will target exactly the requested version. It is only guaranteed that the version/profile/options combination for the created context is compatible with the request, as long as the driver is able to provide such a context.

For example, requesting an OpenGL version 3.x core profile context may result in an OpenGL 4.x core profile context. Similarly, a request for OpenGL 2.1 may result in an OpenGL 3.0 context with deprecated functions enabled. Finally, depending on the driver, unsupported versions may result in either a context creation failure or in a context for the highest supported version.

Similar differences are possible in the buffer sizes, for example, the resulting context may have a larger depth buffer than requested.

See also
setFormat()
QOpenGLFunctions * QOpenGLContext::functions ( ) const

Get the QOpenGLFunctions instance for this context. QOpenGLContext offers this as a convenient way to access QOpenGLFunctions without having to manage it manually. The context or a sharing context must be current. The returned QOpenGLFunctions instance is ready to be used and it does not need initializeOpenGLFunctions() to be called.

QOpenGLContext * QOpenGLContext::globalShareContext ( )
static

Returns the shared OpenGL context for the application, if one is available. Otherwise, returns a nullptr. This is useful if you need to upload OpenGL objects (buffers, textures, etc.) before creating or showing a QOpenGLWidget.

You must set the Qt::AA_ShareOpenGLContexts flag on QApplication before creating the QApplication object, otherwise CopperSpice may not create a global shared context.

Warning
Do not attempt to make the context returned by this method current on any surface. Instead, create a new context which shares with the global one and then make the new context current.
See also
setShareContext(), makeCurrent()
bool QOpenGLContext::hasExtension ( const QByteArray extension) const

Returns true if this OpenGL context supports the specified OpenGL extension, otherwise returns false. The context or a sharing context must be current.

See also
extensions()
bool QOpenGLContext::isOpenGLES ( ) const

Returns true if the context is an OpenGL ES context. If the context has not yet been created, the result is based on the requested format set via setFormat().

See also
create(), format(), setFormat()
bool QOpenGLContext::makeCurrent ( QSurface surface)

Sets the current context in the current thread to render to the given surface. Returns true if successful otherwise returns false. The latter may happen if the surface is not exposed or the graphics hardware is not available due to the application being suspended.

If surface is a nullptr then calling this method is equivalent to calling doneCurrent().

Avoid calling this method from a different thread than the one the QOpenGLContext instance lives in. If you wish to use QOpenGLContext from a different thread you should first make sure it's not current in the current thread, by calling doneCurrent() if necessary. Then call moveToThread(otherThread) before using it in the other thread.

See also
functions(), doneCurrent()
QOpenGLContext::OpenGLModuleType QOpenGLContext::openGLModuleType ( )
static

Returns the underlying OpenGL implementation type. On platforms where the OpenGL implementation is not dynamically loaded, the return value is determined during compile time and never changes.

A desktop OpenGL implementation may be capable of creating ES-compatible contexts too. Therefore in most cases it is more appropriate to check QSurfaceFormat::renderableType() or use the method isOpenGLES().

This method can only be called after a QApplication has been created.

QScreen * QOpenGLContext::screen ( ) const

Returns the screen this context is displayed on.

See also
setScreen()
void QOpenGLContext::setFormat ( const QSurfaceFormat format)

Sets the format the OpenGL context should be compatible with. Call create() so the new format will take effect.

When the format is not explicitly set via this function, the format returned by QSurfaceFormat::defaultFormat() will be used. This means that when having multiple contexts, individual calls to this function can be replaced by one single call to QSurfaceFormat::setDefaultFormat() before creating the first context.

See also
format()
void QOpenGLContext::setScreen ( QScreen screen)

Sets the screen this context will be displayed on.

See also
screen()
void QOpenGLContext::setShareContext ( QOpenGLContext *  shareContext)

Configures this context to share textures, shaders, and other OpenGL resources with the given shareContext.

See also
shareContext()
QOpenGLContext * QOpenGLContext::shareContext ( ) const

Returns the share context this context was created with. If the underlying platform was not able to support the requested sharing this method will return a nullptr.

See also
setShareContext()
QOpenGLContextGroup * QOpenGLContext::shareGroup ( ) const

Returns the share group this context belongs to.