The QOpenGLWidget 类是用于渲染 OpenGL 图形的 Widget。 更多...
头: | #include <QOpenGLWidget> |
qmake: | QT += widgets |
Since: | Qt 5.4 |
继承: | QWidget |
enum | UpdateBehavior { NoPartialUpdate, PartialUpdate } |
QOpenGLWidget (QWidget * parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags()) | |
virtual | ~QOpenGLWidget () |
QOpenGLContext * | context () const |
GLuint | defaultFramebufferObject () const |
void | doneCurrent () |
QSurfaceFormat | format () const |
QImage | grabFramebuffer () |
bool | isValid () const |
void | makeCurrent () |
void | setFormat (const QSurfaceFormat & format ) |
void | setTextureFormat (GLenum texFormat ) |
void | setUpdateBehavior (QOpenGLWidget::UpdateBehavior updateBehavior ) |
GLenum | textureFormat () const |
QOpenGLWidget::UpdateBehavior | updateBehavior () const |
void | aboutToCompose () |
void | aboutToResize () |
void | frameSwapped () |
void | resized () |
const QMetaObject | staticMetaObject |
virtual void | initializeGL () |
virtual void | paintGL () |
virtual void | resizeGL (int w , int h ) |
virtual void | paintEvent (QPaintEvent * e ) override |
virtual void | resizeEvent (QResizeEvent * e ) override |
The QOpenGLWidget 类是用于渲染 OpenGL 图形的 Widget。
QOpenGLWidget 提供用于显示集成到 Qt 应用程序中的 OpenGL 图形功能。用法非常简单:使类继承它,并使用子类像任何其它 QWidget ,除了可以选择使用 QPainter 和标准 OpenGL 渲染命令。
QOpenGLWidget 提供了 3 个方便虚函数,可以在子类中重实现来履行典型 OpenGL 任务:
如果需要从其它地方触发重新绘制相比 paintGL () (典型范例是当使用 timers 来动画场景),应调用小部件的 update () 函数来调度更新。
Your widget's OpenGL rendering context is made current when paintGL (), resizeGL (),或 initializeGL () is called. If you need to call the standard OpenGL API functions from other places (e.g. in your widget's constructor or in your own paint functions), you must call makeCurrent () first.
All rendering happens into an OpenGL framebuffer object. makeCurrent () ensure that it is bound in the context. Keep this in mind when creating and binding additional framebuffer objects in the rendering code in paintGL (). Never re-bind the framebuffer with ID 0. Instead, call defaultFramebufferObject () to get the ID that should be bound.
QOpenGLWidget allows using different OpenGL versions and profiles when the platform supports it. Just set the requested format via setFormat (). Keep in mind however that having multiple QOpenGLWidget instances in the same window requires that they all use the same format, or at least formats that do not make the contexts non-sharable. To overcome this issue, prefer using QSurfaceFormat::setDefaultFormat () 而不是 setFormat ().
注意: 调用 QSurfaceFormat::setDefaultFormat () before constructing the QApplication instance is mandatory on some platforms (for example, macOS ) when an OpenGL core profile context is requested. This is to ensure that resource sharing between contexts stays functional as all internal contexts are created using the correct version and profile.
As described above, subclass QOpenGLWidget to render pure 3D content in the following way:
It is also possible to draw 2D graphics onto a QOpenGLWidget subclass using QPainter :
When performing drawing using QPainter only, it is also possible to perform the painting like it is done for ordinary widgets: by reimplementing paintEvent ().
When making OpenGL function calls, it is strongly recommended to avoid calling the functions directly. Instead, prefer using QOpenGLFunctions (when making portable applications) or the versioned variants (for example, QOpenGLFunctions_3_2_Core and similar, when targeting modern, desktop-only OpenGL). This way the application will work correctly in all Qt build configurations, including the ones that perform dynamic OpenGL implementation loading which means applications are not directly linking to an GL implementation and thus direct function calls are not feasible.
在 paintGL () the current context is always accessible by caling QOpenGLContext::currentContext (). From this context an already initialized, ready-to-be-used QOpenGLFunctions instance is retrievable by calling QOpenGLContext::functions (). An alternative to prefixing every GL call is to inherit from QOpenGLFunctions 和调用 QOpenGLFunctions::initializeOpenGLFunctions () 在 initializeGL ().
As for the OpenGL headers, note that in most cases there will be no need to directly include any headers like GL.h. The OpenGL-related Qt headers will include qopengl.h which will in turn include an appropriate header for the system. This might be an OpenGL ES 3.x or 2.0 header, the highest version that is available, or a system-provided gl.h. In addition, a copy of the extension headers (called glext.h on some systems) is provided as part of Qt both for OpenGL and OpenGL ES. These will get included automatically on platforms where feasible. This means that constants and function pointer typedefs from ARB, EXT, OES extensions are automatically available.
To get started, the simplest QOpenGLWidget subclass could like like the following:
class MyGLWidget : public QOpenGLWidget { public: MyGLWidget(QWidget *parent) : QOpenGLWidget(parent) { } protected: void initializeGL() override { // Set up the rendering context, load shaders and other resources, etc.: QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions(); f->glClearColor(1.0f, 1.0f, 1.0f, 1.0f); ... } void resizeGL(int w, int h) override { // Update projection matrix and other size related settings: m_projection.setToIdentity(); m_projection.perspective(45.0f, w / float(h), 0.01f, 100.0f); ... } void paintGL() override { // Draw the scene: QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions(); f->glClear(GL_COLOR_BUFFER_BIT); ... } };
Alternatively, the prefixing of each and every OpenGL call can be avoided by deriving from QOpenGLFunctions instead:
class MyGLWidget : public QOpenGLWidget, protected QOpenGLFunctions { ... void initializeGL() override { initializeOpenGLFunctions(); glClearColor(...); ... } ... };
To get a context compatible with a given OpenGL version or profile, or to request depth and stencil buffers, call setFormat ():
QOpenGLWidget *widget = new QOpenGLWidget(parent); QSurfaceFormat format; format.setDepthBufferSize(24); format.setStencilBufferSize(8); format.setVersion(3, 2); format.setProfile(QSurfaceFormat::CoreProfile); widget->setFormat(format); // must be called before the widget or its parent window gets shown
With OpenGL 3.0+ contexts, when portability is not important, the versioned QOpenGLFunctions variants give easy access to all the modern OpenGL functions available in a given version:
... void paintGL() override { QOpenGLFunctions_3_2_Core *f = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_2_Core>(); ... f->glDrawArraysInstanced(...); ... } ...
As described above, it is simpler and more robust to set the requested format globally so that it applies to all windows and contexts during the lifetime of the application. Below is an example of this:
int main(int argc, char **argv) { QApplication app(argc, argv); QSurfaceFormat format; format.setDepthBufferSize(24); format.setStencilBufferSize(8); format.setVersion(3, 2); format.setProfile(QSurfaceFormat::CoreProfile); QSurfaceFormat::setDefaultFormat(format); MyWidget widget; widget.show(); return app.exec(); }
The legacy QtOpenGL module (classes prefixed with QGL) provides a widget called QGLWidget. QOpenGLWidget is intended to be a modern replacement for it. Therefore, especially in new applications, the general recommendation is to use QOpenGLWidget .
While the API is very similar, there is an important difference between the two: QOpenGLWidget always renders offscreen, using framebuffer objects. QGLWidget on the other hand uses a native window and surface. The latter causes issues when using it in complex user interfaces since, depending on the platform, such native child widgets may have various limitations, regarding stacking orders for example. QOpenGLWidget avoids this by not creating a separate native window.
Due to being backed by a framebuffer object, the behavior of
QOpenGLWidget
非常类似
QOpenGLWindow
with the update behavior set to
PartialUpdateBlit
or
PartialUpdateBlend
. This means that the contents are preserved between
paintGL
() calls so that incremental rendering is possible. With QGLWidget (and naturally
QOpenGLWindow
with the default update behavior) this is usually not the case because swapping the buffers leaves the back buffer with undefined contents.
注意: Most applications do not need incremental rendering because they will render everything in the view on every paint call. In this case it is important to call glClear() as early as possible in paintGL (). This helps mobile GPUs that use a tile-based architecture to recognize that the tile buffer does not need to be reloaded with the framebuffer's previous contents. Omitting the clear call can lead to significant performance drops on such systems.
注意: Avoid calling winId () 在 QOpenGLWidget . This function triggers the creation of a native window, resulting in reduced performance and possibly rendering glitches.
Besides the main conceptual difference of being backed by a framebuffer object, there are a number of smaller, internal differences between QOpenGLWidget and the older QGLWidget:
true
for
autoFillBackground
. It then performed clearing to the palette's background color every time
QPainter::begin
() was used.
QOpenGLWidget
does not follow this:
autoFillBackground
defaults to false, like for any other widget. The only exception is when being used as a viewport for other widgets like
QGraphicsView
. In such a case
autoFillBackground
will be automatically set to true to ensure compatibility with QGLWidget-based viewports.
To enable multisampling, set the number of requested samples on the QSurfaceFormat that is passed to setFormat (). On systems that do not support it the request may get ignored.
Multisampling support requires support for multisampled renderbuffers and framebuffer blits. On OpenGL ES 2.0 implementations it is likely that these will not be present. This means that multisampling will not be available. With modern OpenGL versions and OpenGL ES 3.0 and up this is usually not a problem anymore.
Performing offscreen rendering on worker threads, for example to generate textures that are then used in the GUI/main thread in paintGL (), are supported by exposing the widget's QOpenGLContext so that additional contexts sharing with it can be created on each thread.
Drawing directly to the QOpenGLWidget 's framebuffer outside the GUI/main thread is possible by reimplementing paintEvent () to do nothing. The context's thread affinity has to be changed via QObject::moveToThread (). After that, makeCurrent () 和 doneCurrent () are usable on the worker thread. Be careful to move the context back to the GUI/main thread afterwards.
Unlike QGLWidget, triggering a buffer swap just for the QOpenGLWidget is not possible since there is no real, onscreen native surface for it. Instead, it is up to the widget stack to manage composition and buffer swaps on the gui thread. When a thread is done updating the framebuffer, call update () on the GUI/main thread to schedule composition.
Extra care has to be taken to avoid using the framebuffer when the GUI/main thread is performing compositing. The signals aboutToCompose () 和 frameSwapped () will be emitted when the composition is starting and ending. They are emitted on the GUI/main thread. This means that by using a direct connection aboutToCompose () can block the GUI/main thread until the worker thread has finished its rendering. After that, the worker thread must perform no further rendering until the frameSwapped () signal is emitted. If this is not acceptable, the worker thread has to implement a double buffering mechanism. This involves drawing using an alternative render target, that is fully controlled by the thread, e.g. an additional framebuffer object, and blitting to the QOpenGLWidget 's framebuffer at a suitable time.
When multiple QOpenGLWidgets are added as children to the same top-level widget, their contexts will share with each other. This does not apply for QOpenGLWidget instances that belong to different windows.
This means that all QOpenGLWidgets in the same window can access each other's sharable resources, like textures, and there is no need for an extra "global share" context, as was the case with QGLWidget.
To set up sharing between QOpenGLWidget instances belonging to different windows, set the Qt::AA_ShareOpenGLContexts application attribute before instantiating QApplication . This will trigger sharing between all QOpenGLWidget instances without any further steps.
Creating extra QOpenGLContext instances that share resources like textures with the QOpenGLWidget 's context is also possible. Simply pass the pointer returned from context () 到 QOpenGLContext::setShareContext () 先于调用 QOpenGLContext::create (). The resulting context can also be used on a different thread, allowing threaded generation of textures and asynchronous texture uploads.
注意, QOpenGLWidget expects a standard conformant implementation of resource sharing when it comes to the underlying graphics drivers. For example, some drivers, in particular for mobile and embedded hardware, have issues with setting up sharing between an existing context and others that are created later. Some other drivers may behave in unexpected ways when trying to utilize shared resources between different threads.
The QOpenGLWidget 's associated OpenGL context is guaranteed to be current whenever initializeGL () 和 paintGL () are invoked. Do not attempt to create OpenGL resources before initializeGL () is called. For example, attempting to compile shaders, initialize vertex buffer objects or upload texture data will fail when done in a subclass's constructor. These operations must be deferred to initializeGL (). Some of Qt's OpenGL helper classes, like QOpenGLBuffer or QOpenGLVertexArrayObject , have a matching deferred behavior: they can be instantiated without a context, but all initialization is deferred until a create (), or similar, call. This means that they can be used as normal (non-pointer) member variables in a QOpenGLWidget subclass, but the create () or similar function can only be called from initializeGL (). Be aware however that not all classes are designed like this. When in doubt, make the member variable a pointer and create and destroy the instance dynamically in initializeGL () and the destructor, respectively.
Releasing the resources also needs the context to be current. Therefore destructors that perform such cleanup are expected to call makeCurrent () before moving on to destroy any OpenGL resources or wrappers. Avoid deferred deletion via deleteLater() or the parenting mechanism of QObject . There is no guarantee the correct context will be current at the time the instance in question is really destroyed.
A typical subclass will therefore often look like the following when it comes to resource initialization and destruction:
class MyGLWidget : public QOpenGLWidget { ... private: QOpenGLVertexArrayObject m_vao; QOpenGLBuffer m_vbo; QOpenGLShaderProgram *m_program; QOpenGLShader *m_shader; QOpenGLTexture *m_texture; }; MyGLWidget::MyGLWidget() : m_program(0), m_shader(0), m_texture(0) { // No OpenGL resource initialization is done here. } MyGLWidget::~MyGLWidget() { // Make sure the context is current and then explicitly // destroy all underlying OpenGL resources. makeCurrent(); delete m_texture; delete m_shader; delete m_program; m_vbo.destroy(); m_vao.destroy(); doneCurrent(); } void MyGLWidget::initializeGL() { m_vao.create(); if (m_vao.isCreated()) m_vao.bind(); m_vbo.create(); m_vbo.bind(); m_vbo.allocate(...); m_texture = new QOpenGLTexture(QImage(...)); m_shader = new QOpenGLShader(...); m_program = new QOpenGLShaderProgram(...); ... }
This is naturally not the only possible solution. One alternative is to use the aboutToBeDestroyed() signal of QOpenGLContext . By connecting a slot, using direct connection, to this signal, it is possible to perform cleanup whenever the the underlying native context handle, or the entire QOpenGLContext instance, is going to be released. The following snippet is in principle equivalent to the previous one:
void MyGLWidget::initializeGL() { // context() and QOpenGLContext::currentContext() are equivalent when called from initializeGL or paintGL. connect(context(), &QOpenGLContext::aboutToBeDestroyed, this, &MyGLWidget::cleanup); } void MyGLWidget::cleanup() { makeCurrent(); delete m_texture; m_texture = 0; ... doneCurrent(); }
注意: For widgets that change their associated top-level window multiple times during their lifetime, a combined approach is essential. Whenever the widget or a parent of it gets reparented so that the top-level window becomes different, the widget's associated context is destroyed and a new one is created. This is then followed by a call to initializeGL () where all OpenGL resources must get reinitialized. Due to this the only option to perform proper cleanup is to connect to the context's aboutToBeDestroyed() signal. Note that the context in question may not be the current one when the signal gets emitted. Therefore it is good practice to call makeCurrent () in the connected slot. Additionally, the same cleanup steps must be performed from the derived class' destructor, since the slot connected to the signal will not get invoked when the widget is being destroyed.
注意: 当 Qt::AA_ShareOpenGLContexts is set, the widget's context never changes, not even when reparenting because the widget's associated texture is guaranteed to be accessible also from the new top-level's context.
Proper cleanup is especially important due to context sharing. Even though each QOpenGLWidget 's associated context is destroyed together with the QOpenGLWidget , the sharable resources in that context, like textures, will stay valid until the top-level window, in which the QOpenGLWidget lived, is destroyed. Additionally, settings like Qt::AA_ShareOpenGLContexts and some Qt modules may trigger an even wider scope for sharing contexts, potentially leading to keeping the resources in question alive for the entire lifetime of the application. Therefore the safest and most robust is always to perform explicit cleanup for all resources and resource wrappers used in the QOpenGLWidget .
Putting other widgets underneath and making the QOpenGLWidget transparent will not lead to the expected results: The widgets underneath will not be visible. This is because in practice the QOpenGLWidget is drawn before all other regular, non-OpenGL widgets, and so see-through type of solutions are not feasible. Other type of layouts, like having widgets on top of the QOpenGLWidget , will function as expected.
When absolutely necessary, this limitation can be overcome by setting the Qt::WA_AlwaysStackOnTop attribute on the QOpenGLWidget . Be aware however that this breaks stacking order, for example it will not be possible to have other widgets on top of the QOpenGLWidget , so it should only be used in situations where a semi-transparent QOpenGLWidget with other widgets visible underneath is required.
Note that this does not apply when there are no other widgets underneath and the intention is to have a semi-transparent window. In that case the traditional approach of setting
Qt::WA_TranslucentBackground
on the top-level window is sufficient. Note that if the transparent areas are only desired in the
QOpenGLWidget
,那么
Qt::WA_NoSystemBackground
will need to be turned back to
false
after enabling
Qt::WA_TranslucentBackground
. Additionally, requesting an alpha channel for the
QOpenGLWidget
's context via
setFormat
() may be necessary too, depending on the system.
QOpenGLWidget supports multiple update behaviors, just like QOpenGLWindow . In preserved mode the rendered content from the previous paintGL () call is available in the next one, allowing incremental rendering. In non-preserved mode the content is lost and paintGL () implementations are expected to redraw everything in the view.
Before Qt 5.5 the default behavior of
QOpenGLWidget
was to preserve the rendered contents between
paintGL
() calls. Since Qt 5.5 the default behavior is non-preserved because this provides better performance and the majority of applications have no need for the previous content. This also resembles the semantics of an OpenGL-based
QWindow
and matches the default behavior of
QOpenGLWindow
in that the color and ancillary buffers are invalidated for each frame. To restore the preserved behavior, call
setUpdateBehavior
() 采用
PartialUpdate
.
添加 QOpenGLWidget into a window turns on OpenGL-based compositing for the entire window. In some special cases this may not be ideal, and the old QGLWidget-style behavior with a separate, native child window is desired. Desktop applications that understand the limitations of this approach (for example when it comes to overlaps, transparency, scroll views and MDI areas), can use QOpenGLWindow with QWidget::createWindowContainer (). This is a modern alternative to QGLWidget and is faster than QOpenGLWidget due to the lack of the additional composition step. It is strongly recommended to limit the usage of this approach to cases where there is no other choice. Note that this option is not suitable for most embedded and mobile platforms, and it is known to have issues on certain desktop platforms (e.g. macOS ) too. The stable, cross-platform solution is always QOpenGLWidget .
OpenGL is a trademark of Silicon Graphics, Inc. in the United States and other countries.
另请参阅 QOpenGLFunctions , QOpenGLWindow , Qt::AA_ShareOpenGLContexts ,和 UpdateBehavior .
This enum describes the update semantics of QOpenGLWidget .
常量 | 值 | 描述 |
---|---|---|
QOpenGLWidget::NoPartialUpdate
|
0
|
QOpenGLWidget will discard the contents of the color buffer and the ancillary buffers after the QOpenGLWidget is rendered to screen. This is the same behavior that can be expected by calling QOpenGLContext::swapBuffers with a default opengl enabled QWindow as the argument. NoPartialUpdate can have some performance benefits on certain hardware architectures common in the mobile and embedded space when a framebuffer object is used as the rendering target. The framebuffer object is invalidated between frames with glDiscardFramebufferEXT if supported or a glClear. Please see the documentation of EXT_discard_framebuffer for more information: https://www.khronos.org/registry/gles/extensions/EXT/EXT_discard_framebuffer.txt |
QOpenGLWidget::PartialUpdate
|
1
|
The framebuffer objects color buffer and ancillary buffers are not invalidated between frames. |
该枚举在 Qt 5.5 引入或被修改。
另请参阅 updateBehavior () 和 setUpdateBehavior ().
构造子级 Widget 为 parent ,采用 Widget 设置标志 f .
[虚拟]
QOpenGLWidget::
~QOpenGLWidget
()
销毁 QOpenGLWidget 实例,释放其资源。
The QOpenGLWidget '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 widget.
警告: if you have objects wrapping OpenGL resources (such as QOpenGLBuffer , QOpenGLShaderProgram , etc.) as members of a OpenGLWidget 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.
另请参阅 makeCurrent .
[signal]
void
QOpenGLWidget::
aboutToCompose
()
This signal is emitted when the widget's top-level window is about to begin composing the textures of its QOpenGLWidget children and the other widgets.
[signal]
void
QOpenGLWidget::
aboutToResize
()
This signal is emitted when the widget's size is changed and therefore the framebuffer object is going to be recreated.
返回
QOpenGLContext
used by this widget or
0
若尚未初始化。
注意: The context and the framebuffer object used by the widget changes when reparenting the widget via setParent ().
另请参阅 QOpenGLContext::setShareContext () 和 defaultFramebufferObject ().
返回帧缓冲对象句柄或
0
若尚未初始化。
注意: The framebuffer object belongs to the context returned by context () and may not be accessible from other contexts.
注意: The context and the framebuffer object used by the widget changes when reparenting the widget via setParent (). In addition, the framebuffer object changes on each resize.
另请参阅 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 ().
Returns the context and surface format used by this widget and its toplevel window.
After the widget and its toplevel have both been created, resized and shown, this function will return the actual format of the context. This may differ from the requested format if the request could not be fulfilled by the platform. It is also possible to get larger color buffer sizes than requested.
When the widget's window and the related OpenGL resources are not yet initialized, the return value is the format that has been set via setFormat ().
另请参阅 setFormat () 和 context ().
[signal]
void
QOpenGLWidget::
frameSwapped
()
This signal is emitted after the widget's top-level window has finished composition and returned from its potentially blocking QOpenGLContext::swapBuffers () 调用。
渲染并返回帧缓冲的 32 位 RGB 图像。
注意: 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.
[virtual protected]
void
QOpenGLWidget::
initializeGL
()
This virtual function is called once before the first call to paintGL () 或 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 is not yet available at this stage, so avoid issuing draw calls from here. Defer such calls to paintGL () 代替。
另请参阅 paintGL () 和 resizeGL ().
返回 true if the widget and OpenGL resources, like the context, have been successfully initialized. Note that the return value is always false until the widget is shown.
Prepares for rendering OpenGL content for this widget by making the corresponding context current and binding the framebuffer object in that context.
It is not necessary to call this function in most cases, because it is called automatically before invoking paintGL ().
另请参阅 context (), paintGL (),和 doneCurrent ().
[override virtual protected]
void
QOpenGLWidget::
paintEvent
(
QPaintEvent
*
e
)
重实现自 QWidget::paintEvent ().
Handles paint events.
调用 QWidget::update () will lead to sending a paint event e , and thus invoking this function. (NB this is asynchronous and will happen at some point after returning from update ()). This function will then, after some preparation, call the virtual paintGL () to update the contents of the QOpenGLWidget 's framebuffer. The widget's top-level window will then composite the framebuffer's texture with the rest of the window.
[virtual protected]
void
QOpenGLWidget::
paintGL
()
This virtual function is called whenever the widget 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 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.
另请参阅 initializeGL () 和 resizeGL ().
[override virtual protected]
void
QOpenGLWidget::
resizeEvent
(
QResizeEvent
*
e
)
重实现自 QWidget::resizeEvent ().
Handles resize events that are passed in the e event parameter. Calls the virtual function resizeGL ().
注意: Avoid overriding this function in derived classes. If that is not feasible, make sure that QOpenGLWidget 's implementation is invoked too. Otherwise the underlying framebuffer object and related resources will not get resized properly and will lead to incorrect rendering.
[virtual protected]
void
QOpenGLWidget::
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 .
There is no need to call makeCurrent () because this has already been done when this function is called. Additionally, the framebuffer is also bound.
另请参阅 initializeGL () 和 paintGL ().
[signal]
void
QOpenGLWidget::
resized
()
This signal is emitted right after the framebuffer object has been recreated due to resizing the widget.
设置请求表面 format .
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 OpenGL widgets, individual calls to this function can be replaced by one single call to QSurfaceFormat::setDefaultFormat () before creating the first widget.
注意: Requesting an alpha buffer via this function will not lead to the desired results when the intention is to make other widgets beneath visible. Instead, use Qt::WA_AlwaysStackOnTop to enable semi-transparent QOpenGLWidget instances with other widgets visible underneath. Keep in mind however that this breaks the stacking order, so it will no longer be possible to have other widgets on top of the QOpenGLWidget .
另请参阅 format (), Qt::WA_AlwaysStackOnTop ,和 QSurfaceFormat::setDefaultFormat ().
Sets a custom internal texture format of texFormat .
When working with sRGB framebuffers, it will be necessary to specify a format like
GL_SRGB8_ALPHA8
. This can be achieved by calling this function.
注意: This function has no effect if called after the widget has already been shown and thus it performed initialization.
注意: This function will typically have to be used in combination with a QSurfaceFormat::setDefaultFormat () call that sets the color space to QSurfaceFormat::sRGBColorSpace .
该函数在 Qt 5.10 引入。
另请参阅 textureFormat ().
Sets this widget's update behavior to updateBehavior .
该函数在 Qt 5.5 引入。
另请参阅 updateBehavior ().
Returns the active internal texture format if the widget has already initialized, the requested format if one was set but the widget has not yet been made visible, or 0 if setTextureFormat () was not called and the widget has not yet been made visible.
该函数在 Qt 5.10 引入。
另请参阅 setTextureFormat ().
Returns the update behavior of the widget.
该函数在 Qt 5.5 引入。
另请参阅 setUpdateBehavior ().