QOpenGLWindow Class

The QOpenGLWindow class is a convenience subclass of QWindow to perform OpenGL painting. 更多...

头文件: #include <QOpenGLWindow>
qmake: QT += gui
开始支持版本: Qt 5.4
基类: QPaintDeviceWindow

公有类型

enum UpdateBehavior { NoPartialUpdate, PartialUpdateBlit, PartialUpdateBlend }

公有函数

QOpenGLWindow(UpdateBehavior updateBehavior = NoPartialUpdate, QWindow *parent = Q_NULLPTR)
QOpenGLWindow(QOpenGLContext *shareContext, UpdateBehavior updateBehavior = NoPartialUpdate, QWindow *parent = Q_NULLPTR)
~QOpenGLWindow()
QOpenGLContext *context() const
GLuint defaultFramebufferObject() const
void doneCurrent()
QImage grabFramebuffer()
bool isValid() const
void makeCurrent()
QOpenGLContext *shareContext() const
UpdateBehavior updateBehavior() const

信号

void frameSwapped()

受保护的函数

virtual void initializeGL()
virtual void paintGL()
virtual void paintOverGL()
virtual void paintUnderGL()
virtual void resizeGL(int w, int h)

重新实现的受保护函数

virtual void paintEvent(QPaintEvent *event)
virtual void resizeEvent(QResizeEvent *event)

其他继承的成员

详细描述

The QOpenGLWindow class is a convenience subclass of QWindow to perform OpenGL painting.

QOpenGLWindow 是一个增强的 QWindow, 允许使用与 QOpenGLWidget 兼容且类似于传统 QGLWidget 的 API 轻松创建执行 OpenGL 渲染的窗口. 与 QOpenGLWidget 不同, QOpenGLWindow 不依赖于 widgets 模块, 并提供更好的性能.

典型的应用程序将继承 QOpenGLWindow , 并重新实现下列虚拟函数:

要需要重绘, 调用 update() 函数. 注意, 这不会立即导致调用paintGL(). 连续多次调用 update() 不会以任何方式改变行为.

这是一个槽函数, 因此可以连接到 QTimer::timeout() 信号来执行动画. 注意, 在现代 OpenGL 世界中, 依赖于显示器垂直刷新率的同步是更好的选择. 有关交换间隔的说明, 参见 setSwapInterval(). 当交换间隔为 1 时(大多数系统默认情况下都是这种情况), 每次重绘后由 QOpenGLWindow 在内部执行的 swapBuffers() 调用将阻塞并等待垂直同步. 这意味着每当交换完成时, 都可以通过调用 update() 再次安排更新, 而无需依赖计时器.

要请求上下文的特定配置, 请像任何其他 QWindow 一样使用 setFormat(). 除其他外, 这允许请求给定的 OpenGL 版本和配置文件, 或启用深度和模板缓冲区.

QWindow 不同, QOpenGLWindow 允许在其自身上打开一个painter, 并执行基于 QPainter 的绘图.

QOpenGLWindow支持多种更新行为. 默认情况下, NoPartialUpdate 相当于常规的, 基于 OpenGL 的 QWindow 或旧版 QGLWidget. 相比之下, PartialUpdateBlitPartialUpdateBlend 更符合 QOpenGLWidget 的工作方式, 其中始终存在额外的专用帧缓冲区对象. 这些模式允许通过牺牲一些性能, 在每次绘制上仅重新绘制较小的区域, 并保留前一帧的其余内容. 这对于使用 QPainter 增量渲染的应用程序很有用, 因为这样它们就不必在每次调用 paintGL() 时重绘整个窗口内容.

QOpenGLWidget 类似, QOpenGLWidget 支持 Qt::AA_ShareOpenGLContexts 属性. 启用后, 所有 QOpenGLWindow 实例的 OpenGL 上下文将相互共享. 这允许访问彼此的可共享 OpenGL 资源.

有关 Qt 中图形的更多信息,参见 Graphics.

成员类型

enum QOpenGLWindow::UpdateBehavior

This enum describes the update strategy of the QOpenGLWindow.

ConstantValueDescription
QOpenGLWindow::NoPartialUpdate0Indicates that the entire window surface will redrawn on each update and so no additional framebuffers are needed. This is the setting used in most cases and is equivalent to how drawing directly via QWindow would function.
QOpenGLWindow::PartialUpdateBlit1Indicates that the drawing performed in paintGL() does not cover the entire window. In this case an extra framebuffer object is created under the hood, and rendering performed in paintGL() will target this framebuffer. This framebuffer is then blitted onto the window surface's default framebuffer after each paint. This allows having QPainter-based drawing code in paintGL() which only repaints a smaller area at a time, because, unlike NoPartialUpdate, the previous content is preserved.
QOpenGLWindow::PartialUpdateBlend2Similar to PartialUpdateBlit, but instead of using framebuffer blits, the contents of the extra framebuffer is rendered by drawing a textured quad with blending enabled. This, unlike PartialUpdateBlit, allows alpha blended content and works even when the glBlitFramebuffer is not available. Performance-wise this setting is likely to be somewhat slower than PartialUpdateBlit.

成员函数

QOpenGLWindow::QOpenGLWindow(UpdateBehavior updateBehavior = NoPartialUpdate, QWindow *parent = Q_NULLPTR)

Constructs a new QOpenGLWindow with the given parent and updateBehavior.

参见 QOpenGLWindow::UpdateBehavior.

QOpenGLWindow::QOpenGLWindow(QOpenGLContext *shareContext, UpdateBehavior updateBehavior = NoPartialUpdate, QWindow *parent = Q_NULLPTR)

Constructs a new QOpenGLWindow with the given parent and updateBehavior. The QOpenGLWindow's context will share with shareContext.

参见 QOpenGLWindow::UpdateBehavior and shareContext.

QOpenGLWindow::~QOpenGLWindow()

Destroys the QOpenGLWindow instance, freeing its resources.

The OpenGLWindow's context is made current in the destructor, allowing for safe destruction of any child object that may need to release OpenGL resources belonging to the context provided by this window.

Warning: if you have objects wrapping OpenGL resources (such as QOpenGLBuffer, QOpenGLShaderProgram, etc.) as members of a QOpenGLWindow subclass, you may need to add a call to makeCurrent() in that subclass' destructor as well. Due to the rules of C++ object destruction, those objects will be destroyed before calling this function (but after that the destructor of the subclass has run), therefore making the OpenGL context current in this function happens too late for their safe disposal.

This function was introduced in Qt 5.5.

参见 makeCurrent.

QOpenGLContext *QOpenGLWindow::context() const

Returns The QOpenGLContext used by this window or 0 if not yet initialized.

GLuint QOpenGLWindow::defaultFramebufferObject() const

The framebuffer object handle used by this window.

When the update behavior is set to NoPartialUpdate, there is no separate framebuffer object. In this case the returned value is the ID of the default framebuffer.

Otherwise the value of the ID of the framebuffer object or 0 if not yet initialized.

void QOpenGLWindow::doneCurrent()

Releases the context.

It is not necessary to call this function in most cases, since the widget will make sure the context is bound and released properly when invoking paintGL().

参见 makeCurrent().

[signal] void QOpenGLWindow::frameSwapped()

This signal is emitted after the potentially blocking buffer swap has been done. Applications that wish to continuously repaint synchronized to the vertical refresh, should issue an update() upon this signal. This allows for a much smoother experience compared to the traditional usage of timers.

QImage QOpenGLWindow::grabFramebuffer()

Returns a 32-bit RGB image of the framebuffer.

Note: This is a potentially expensive operation because it relies on glReadPixels() to read back the pixels. This may be slow and can stall the GPU pipeline.

Note: When used together with update behavior NoPartialUpdate, the returned image may not contain the desired content when called after the front and back buffers have been swapped (unless preserved swap is enabled in the underlying windowing system interface). In this mode the function reads from the back buffer and the contents of that may not match the content on the screen (the front buffer). In this case the only place where this function can safely be used is paintGL() or paintOverGL().

[virtual protected] void QOpenGLWindow::initializeGL()

This virtual function is called once before the first call to paintGL() or resizeGL(). Reimplement it in a subclass.

This function should set up any required OpenGL resources and state.

There is no need to call makeCurrent() because this has already been done when this function is called. Note however that the framebuffer, in case partial update mode is used, is not yet available at this stage, so avoid issuing draw calls from here. Defer such calls to paintGL() instead.

参见 paintGL() and resizeGL().

bool QOpenGLWindow::isValid() const

Returns true if the window's OpenGL resources, like the context, have been successfully initialized. Note that the return value is always false until the window becomes exposed (shown).

void QOpenGLWindow::makeCurrent()

Prepares for rendering OpenGL content for this window by making the corresponding context current and binding the framebuffer object, if there is one, in that context context.

It is not necessary to call this function in most cases, because it is called automatically before invoking paintGL(). It is provided nonetheless to support advanced, multi-threaded scenarios where a thread different than the GUI or main thread may want to update the surface or framebuffer contents. See QOpenGLContext for more information on threading related issues.

This function is suitable for calling also when the underlying platform window is already destroyed. This means that it is safe to call this function from a QOpenGLWindow subclass' destructor. If there is no native window anymore, an offscreen surface is used instead. This ensures that OpenGL resource cleanup operations in the destructor will always work, as long as this function is called first.

参见 QOpenGLContext, context(), paintGL(), and doneCurrent().

[virtual protected] void QOpenGLWindow::paintEvent(QPaintEvent *event)

Reimplemented from QPaintDeviceWindow::paintEvent().

Paint event handler. Calls paintGL().

参见 paintGL().

[virtual protected] void QOpenGLWindow::paintGL()

This virtual function is called whenever the window contents needs to be painted. Reimplement it in a subclass.

There is no need to call makeCurrent() because this has already been done when this function is called.

Before invoking this function, the context and the framebuffer, if there is one, are bound, and the viewport is set up by a call to glViewport(). No other state is set and no clearing or drawing is performed by the framework.

Note: When using a partial update behavior, like PartialUpdateBlend, the output of the previous paintGL() call is preserved and, after the additional drawing perfomed in the current invocation of the function, the content is blitted or blended over the content drawn directly to the window in paintUnderGL().

参见 initializeGL(), resizeGL(), paintUnderGL(), paintOverGL(), and UpdateBehavior.

[virtual protected] void QOpenGLWindow::paintOverGL()

This virtual function is called after each invocation of paintGL().

When the update mode is set to NoPartialUpdate, there is no difference between this function and paintGL(), performing rendering in either of them leads to the same result.

Like paintUnderGL(), rendering in this function targets the default framebuffer of the window, regardless of the update behavior. It gets called after paintGL() has returned and the blit (PartialUpdateBlit) or quad drawing (PartialUpdateBlend) has been done.

参见 paintGL(), paintUnderGL(), and UpdateBehavior.

[virtual protected] void QOpenGLWindow::paintUnderGL()

The virtual function is called before each invocation of paintGL().

When the update mode is set to NoPartialUpdate, there is no difference between this function and paintGL(), performing rendering in either of them leads to the same result.

The difference becomes significant when using PartialUpdateBlend, where an extra framebuffer object is used. There, paintGL() targets this additional framebuffer object, which preserves its contents, while paintUnderGL() and paintOverGL() target the default framebuffer, i.e. directly the window surface, the contents of which is lost after each displayed frame.

Note: Avoid relying on this function when the update behavior is PartialUpdateBlit. This mode involves blitting the extra framebuffer used by paintGL() onto the default framebuffer after each invocation of paintGL(), thus overwriting all drawing generated in this function.

参见 paintGL(), paintOverGL(), and UpdateBehavior.

[virtual protected] void QOpenGLWindow::resizeEvent(QResizeEvent *event)

Reimplemented from QWindow::resizeEvent().

Resize event handler. Calls resizeGL().

参见 resizeGL().

[virtual protected] void QOpenGLWindow::resizeGL(int w, int h)

This virtual function is called whenever the widget has been resized. Reimplement it in a subclass. The new size is passed in w and h.

Note: This is merely a convenience function in order to provide an API that is compatible with QOpenGLWidget. Unlike with QOpenGLWidget, derived classes are free to choose to override resizeEvent() instead of this function.

Note: Avoid issuing OpenGL commands from this function as there may not be a context current when it is invoked. If it cannot be avoided, call makeCurrent().

Note: Scheduling updates from here is not necessary. The windowing systems will send expose events that trigger an update automatically.

参见 initializeGL() and paintGL().

QOpenGLContext *QOpenGLWindow::shareContext() const

Returns The QOpenGLContext requested to be shared with this window's QOpenGLContext.

UpdateBehavior QOpenGLWindow::updateBehavior() const

Returns the update behavior for this QOpenGLWindow.