The QGLWidget class is a widget for rendering OpenGL graphics. 更多...
头: | #include <QGLWidget> |
qmake: | QT += opengl |
继承: | QWidget |
该类已过时。 提供它是为使旧源代码能继续工作。强烈建议不要在新代码中使用它。
QGLWidget (const QGLFormat & format , QWidget * parent = nullptr, const QGLWidget * shareWidget = nullptr, Qt::WindowFlags f = Qt::WindowFlags()) | |
QGLWidget (QGLContext * context , QWidget * parent = nullptr, const QGLWidget * shareWidget = nullptr, Qt::WindowFlags f = Qt::WindowFlags()) | |
QGLWidget (QWidget * parent = nullptr, const QGLWidget * shareWidget = nullptr, Qt::WindowFlags f = Qt::WindowFlags()) | |
virtual | ~QGLWidget () |
GLuint | bindTexture (const QImage & image , GLenum target = GL_TEXTURE_2D, GLint format = GL_RGBA) |
GLuint | bindTexture (const QImage & image , GLenum target , GLint format , QGLContext::BindOptions options ) |
GLuint | bindTexture (const QPixmap & pixmap , GLenum target , GLint format , QGLContext::BindOptions options ) |
GLuint | bindTexture (const QPixmap & pixmap , GLenum target = GL_TEXTURE_2D, GLint format = GL_RGBA) |
GLuint | bindTexture (const QString & fileName ) |
const QGLColormap & | colormap () const |
QGLContext * | context () const |
void | deleteTexture (GLuint id ) |
void | doneCurrent () |
bool | doubleBuffer () const |
void | drawTexture (const QRectF & target , GLuint textureId , GLenum textureTarget = GL_TEXTURE_2D) |
void | drawTexture (const QPointF & point , GLuint textureId , GLenum textureTarget = GL_TEXTURE_2D) |
QGLFormat | format () const |
QImage | grabFrameBuffer (bool withAlpha = false) |
bool | isSharing () const |
bool | isValid () const |
void | makeCurrent () |
void | makeOverlayCurrent () |
const QGLContext * | overlayContext () const |
void | qglClearColor (const QColor & c ) const |
void | qglColor (const QColor & c ) const |
QPixmap | renderPixmap (int w = 0, int h = 0, bool useContext = false) |
void | renderText (int x , int y , const QString & str , const QFont & font = QFont()) |
void | renderText (double x , double y , double z , const QString & str , const QFont & font = QFont()) |
void | setColormap (const QGLColormap & cmap ) |
void | swapBuffers () |
virtual void | updateGL () |
virtual void | updateOverlayGL () |
QImage | convertToGLFormat (const QImage & img ) |
bool | autoBufferSwap () const |
virtual void | glDraw () |
virtual void | glInit () |
virtual void | initializeGL () |
virtual void | initializeOverlayGL () |
virtual void | paintGL () |
virtual void | paintOverlayGL () |
virtual void | resizeGL (int width , int height ) |
virtual void | resizeOverlayGL (int width , int height ) |
void | setAutoBufferSwap (bool on ) |
virtual bool | event (QEvent * e ) override |
virtual void | paintEvent (QPaintEvent * event ) override |
virtual void | resizeEvent (QResizeEvent * event ) override |
QGLWidget provides functionality for displaying OpenGL graphics integrated into a Qt application. It is very simple to use. You inherit from it and use the subclass like any other QWidget ,除了可以选择使用 QPainter 和标准 OpenGL 渲染命令。
注意:
This class is part of the legacy
Qt OpenGL
module and, like the other
QGL
classes, should be avoided in the new applications. Instead, starting from Qt 5.4, prefer using
QOpenGLWidget
和
QOpenGL
类。
QGLWidget provides three convenient virtual functions that you can reimplement in your subclass to perform the typical OpenGL tasks:
Here is a rough outline of how a QGLWidget subclass might look:
class MyGLDrawer : public QGLWidget { Q_OBJECT // must include this if you use Qt signals/slots public: MyGLDrawer(QWidget *parent) : QGLWidget(parent) {} protected: void initializeGL() override { // Set up the rendering context, define display lists etc.: ... glClearColor(0.0, 0.0, 0.0, 0.0); glEnable(GL_DEPTH_TEST); ... } void resizeGL(int w, int h) override { // setup viewport, projection etc.: glViewport(0, 0, (GLint)w, (GLint)h); ... glFrustum(...); ... } void paintGL() override { // draw the scene: ... glRotatef(...); glMaterialfv(...); glBegin(GL_QUADS); glVertex3f(...); glVertex3f(...); ... glEnd(); ... } };
如果需要从其它地方触发重新绘制相比 paintGL () (典型范例是当使用 timers 来动画场景),应调用小部件的 updateGL () 函数。
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.
QGLWidget provides functions for requesting a new display format and you can also create widgets with customized rendering contexts .
You can also share OpenGL display lists between QGLWidget objects (see the documentation of the QGLWidget constructors for details).
Note that under Windows, the QGLContext belonging to a QGLWidget has to be recreated when the QGLWidget is reparented. This is necessary due to limitations on the Windows platform. This will most likely cause problems for users that have subclassed and installed their own QGLContext on a QGLWidget. It is possible to work around this issue by putting the QGLWidget inside a dummy widget and then reparenting the dummy widget, instead of the QGLWidget. This will side-step the issue altogether, and is what we recommend for users that need this kind of functionality.
On macOS, when Qt is built with Cocoa support, a QGLWidget can't have any sibling widgets placed ontop of itself. This is due to limitations in the Cocoa API and is not supported by Apple.
The QGLWidget creates a GL overlay context in addition to the normal context if overlays are supported by the underlying system.
If you want to use overlays, you specify it in the format . (Note: Overlay must be requested in the format passed to the QGLWidget constructor.) Your GL widget should also implement some or all of these virtual methods:
These methods work in the same way as the normal paintGL () etc. functions, except that they will be called when the overlay context is made current. You can explicitly make the overlay context current by using makeOverlayCurrent (), and you can access the overlay context directly (e.g. to ask for its transparent color) by calling overlayContext ().
On X servers in which the default visual is in an overlay plane, non-GL Qt windows can also be used for overlays.
As described above, subclass QGLWidget to render pure 3D content in the following way:
It is also possible to draw 2D graphics onto a QGLWidget subclass, it is necessary to reimplement QGLWidget::paintEvent () and do the following:
As of Qt version 4.8, support for doing threaded GL rendering has been improved. There are three scenarios that we currently support:
1. Buffer swapping in a thread.
Swapping buffers in a double buffered context may be a synchronous, locking call that may be a costly operation in some GL implementations. Especially so on embedded devices. It's not optimal to have the CPU idling while the GPU is doing a buffer swap. In those cases it is possible to do the rendering in the main thread and do the actual buffer swap in a separate thread. This can be done with the following steps:
1. Call doneCurrent () in the main thread when the rendering is finished.
2. Call QGLContext::moveToThread (swapThread) to transfer ownership of the context to the swapping thread.
3. Notify the swapping thread that it can grab the context.
4. Make the rendering context current in the swapping thread with makeCurrent () and then call swapBuffers ().
5. Call doneCurrent () in the swapping thread.
6. Call QGLContext::moveToThread ( qApp -> thread ()) and notify the main thread that swapping is done.
Doing this will free up the main thread so that it can continue with, for example, handling UI events or network requests. Even if there is a context swap involved, it may be preferable compared to having the main thread wait while the GPU finishes the swap operation. Note that this is highly implementation dependent.
2. Texture uploading in a thread.
Doing texture uploads in a thread may be very useful for applications handling large amounts of images that needs to be displayed, like for instance a photo gallery application. This is supported in Qt through the existing bindTexture () API. A simple way of doing this is to create two sharing QGLWidgets. One is made current in the main GUI thread, while the other is made current in the texture upload thread. The widget in the uploading thread is never shown, it is only used for sharing textures with the main thread. For each texture that is bound via bindTexture (), notify the main thread so that it can start using the texture.
In Qt 4.8, it is possible to draw into a QGLWidget using a QPainter in a separate thread. Note that this is also possible for QGLPixelBuffers and QGLFramebufferObjects. Since this is only supported in the GL 2 paint engine, OpenGL 2.0 or OpenGL ES 2.0 is required.
QGLWidgets can only be created in the main GUI thread. This means a call to doneCurrent () is necessary to release the GL context from the main thread, before the widget can be drawn into by another thread. You then need to call QGLContext::moveToThread () to transfer ownership of the context to the thread in which you want to make it current. Also, the main GUI thread will dispatch resize and paint events to a QGLWidget when the widget is resized, or parts of it becomes exposed or needs redrawing. It is therefore necessary to handle those events because the default implementations inside QGLWidget will try to make the QGLWidget's context current, which again will interfere with any threads rendering into the widget. Reimplement QGLWidget::paintEvent () 和 QGLWidget::resizeEvent () to notify the rendering thread that a resize or update is necessary, and be careful not to call the base class implementation. If you are rendering an animation, it might not be necessary to handle the paint event at all since the rendering thread is doing regular updates. Then it would be enough to reimplement QGLWidget::paintEvent () to do nothing.
As a general rule when doing threaded rendering: be aware that binding and releasing contexts in different threads have to be synchronized by the user. A GL rendering context can only be current in one thread at any time. If you try to open a QPainter on a QGLWidget and the widget's rendering context is current in another thread, it will fail.
In addition to this, rendering using raw GL calls in a separate thread is supported.
OpenGL is a trademark of Silicon Graphics, Inc. in the United States and other countries.
另请参阅 QOpenGLWidget and QGLPixelBuffer .
Constructs an OpenGL widget with parent parent .
The format argument specifies the desired rendering options . If the underlying OpenGL/Window system cannot satisfy all the features requested in format , the nearest subset of features will be used. After creation, the format () method will return the actual format obtained.
The widget will be invalid if the system has no OpenGL 支持 .
The parent 和 Widget 标志 f ,自变量被传递给 QWidget 构造函数。
若 shareWidget is a valid QGLWidget, this widget will share OpenGL display lists and texture objects with shareWidget . But if shareWidget and this widget have different formats , sharing might not be possible. You can check whether sharing is in effect by calling isSharing ().
The initialization of OpenGL rendering state, etc. should be done by overriding the initializeGL () function, rather than in the constructor of your QGLWidget subclass.
另请参阅 QGLFormat::defaultFormat () 和 isValid ().
Constructs an OpenGL widget with parent parent .
The context argument is a pointer to the QGLContext that you wish to be bound to this widget. This allows you to pass in your own QGLContext sub-classes.
The widget will be invalid if the system has no OpenGL 支持 .
The parent 和 Widget 标志 f ,自变量被传递给 QWidget 构造函数。
若 shareWidget is a valid QGLWidget, this widget will share OpenGL display lists and texture objects with shareWidget . But if shareWidget and this widget have different formats , sharing might not be possible. You can check whether sharing is in effect by calling isSharing ().
The initialization of OpenGL rendering state, etc. should be done by overriding the initializeGL () function, rather than in the constructor of your QGLWidget subclass.
另请参阅 QGLFormat::defaultFormat () 和 isValid ().
构造 OpenGL 小部件采用 parent 小部件。
The 默认格式 is used. The widget will be invalid if the system has no OpenGL 支持 .
The parent 和 Widget 标志 f ,自变量被传递给 QWidget 构造函数。
若 shareWidget is a valid QGLWidget, this widget will share OpenGL display lists and texture objects with shareWidget . But if shareWidget and this widget have different formats , sharing might not be possible. You can check whether sharing is in effect by calling isSharing ().
The initialization of OpenGL rendering state, etc. should be done by overriding the initializeGL () function, rather than in the constructor of your QGLWidget subclass.
另请参阅 QGLFormat::defaultFormat () 和 纹理范例 .
[virtual slot]
void
QGLWidget::
updateGL
()
Updates the widget by calling glDraw ().
[virtual slot]
void
QGLWidget::
updateOverlayGL
()
Updates the widget's overlay (if any). Will cause the virtual function paintOverlayGL () to be executed.
The widget's rendering context will become the current context and initializeGL () will be called if it hasn't already been called.
[虚拟]
QGLWidget::
~QGLWidget
()
销毁 Widget。
[protected]
bool
QGLWidget::
autoBufferSwap
() const
返回
true
if the widget is doing automatic GL buffer swapping; otherwise returns
false
.
另请参阅 setAutoBufferSwap ().
Calls QGLContext:::bindTexture( image , target , format ) on the currently set context.
另请参阅 deleteTexture ().
这是重载函数。
The binding options are a set of options used to decide how to bind the texture to the context.
该函数在 Qt 4.6 引入。
这是重载函数。
Generates and binds a 2D GL texture to the current context, based on pixmap . The generated texture id is returned and can be used in
The binding options are a set of options used to decide how to bind the texture to the context.
该函数在 Qt 4.6 引入。
Calls QGLContext:::bindTexture( pixmap , target , format ) on the currently set context.
另请参阅 deleteTexture ().
这是重载函数。
调用 QGLContext::bindTexture ( fileName ) on the currently set context.
另请参阅 deleteTexture ().
Returns the colormap for this widget.
Usually it is only top-level widgets that can have different colormaps installed. Asking for the colormap of a child widget will return the colormap for the child's top-level widget.
If no colormap has been set for this widget, the QGLColormap returned will be empty.
另请参阅 setColormap () 和 QGLColormap::isEmpty ().
Returns the context of this widget.
It is possible that the context is not valid (see isValid ()), for example, if the underlying hardware does not support the format attributes that were requested.
另请参阅 setContext ().
[static]
QImage
QGLWidget::
convertToGLFormat
(const
QImage
&
img
)
Converts the image
img
into the unnamed format expected by OpenGL functions such as glTexImage2D(). The returned image is not usable as a
QImage
,但
QImage::width
(),
QImage::height
() 和
QImage::bits
() may be used with OpenGL. The GL format used is
GL_RGBA
.
调用 QGLContext::deleteTexture ( id ) on the currently set context.
另请参阅 bindTexture ().
Makes no GL context the current context. Normally, you do not need to call this function; QGLContext calls it as necessary. However, it may be useful in multithreaded environments.
返回
true
if the contained GL rendering context has double buffering; otherwise returns
false
.
另请参阅 QGLFormat::doubleBuffer ().
Calls the corresponding QGLContext::drawTexture () 采用 target , textureId ,和 textureTarget for this widget's context.
该函数在 Qt 4.4 引入。
Calls the corresponding QGLContext::drawTexture () 采用 point , textureId ,和 textureTarget for this widget's context.
该函数在 Qt 4.4 引入。
[override virtual protected]
bool
QGLWidget::
event
(
QEvent
*
e
)
重实现: QWidget::event (QEvent *event).
Returns the format of the contained GL rendering context.
另请参阅 setFormat ().
[virtual protected]
void
QGLWidget::
glDraw
()
Executes the virtual function paintGL ().
The widget's rendering context will become the current context and initializeGL () will be called if it hasn't already been called.
[virtual protected]
void
QGLWidget::
glInit
()
Initializes OpenGL for this widget's context. Calls the virtual function initializeGL ().
Returns an image of the frame buffer. If withAlpha is true the alpha channel is included.
Depending on your hardware, you can explicitly select which color buffer to grab with a glReadBuffer() call before calling this function.
On QNX the back buffer is not preserved when swapBuffers () is called. The back buffer where this function reads from, might thus not contain the same content as the front buffer. In order to retrieve what is currently visible on the screen, swapBuffers () has to be executed prior to this function call.
[virtual protected]
void
QGLWidget::
initializeGL
()
This virtual function is called once before the first call to paintGL () 或 resizeGL (), and then once whenever the widget has been assigned a new QGLContext. Reimplement it in a subclass.
This function should set up any required OpenGL context rendering flags, defining display lists, etc.
There is no need to call makeCurrent () because this has already been done when this function is called.
[virtual protected]
void
QGLWidget::
initializeOverlayGL
()
This virtual function is used in the same manner as initializeGL () except that it operates on the widget's overlay context instead of the widget's main context. This means that initializeOverlayGL() is called once before the first call to paintOverlayGL () 或 resizeOverlayGL (). Reimplement it in a subclass.
This function should set up any required OpenGL context rendering flags, defining display lists, etc. for the overlay context.
There is no need to call makeOverlayCurrent () because this has already been done when this function is called.
返回
true
if this widget's GL context is shared with another GL context, otherwise false is returned. Context sharing might not be possible if the widgets use different formats.
另请参阅 format ().
返回
true
if the widget has a valid GL rendering context; otherwise returns
false
. A widget will be invalid if the system has no
OpenGL 支持
.
Makes this widget the current widget for OpenGL operations, i.e. makes the widget's rendering context the current OpenGL rendering context.
Makes the overlay context of this widget current. Use this if you need to issue OpenGL commands to the overlay context outside of initializeOverlayGL (), resizeOverlayGL (),和 paintOverlayGL ().
Does nothing if this widget has no overlay.
另请参阅 makeCurrent ().
Returns the overlay context of this widget, or
nullptr
if this widget has no overlay.
另请参阅 context ().
[override virtual protected]
void
QGLWidget::
paintEvent
(
QPaintEvent
*
event
)
重实现: QWidget::paintEvent (QPaintEvent *event).
处理描绘事件传入的 event parameter. Will cause the virtual paintGL () function to be called.
The widget's rendering context will become the current context and initializeGL () will be called if it hasn't already been called.
[virtual protected]
void
QGLWidget::
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.
[virtual protected]
void
QGLWidget::
paintOverlayGL
()
This virtual function is used in the same manner as paintGL () except that it operates on the widget's overlay context instead of the widget's main context. This means that paintOverlayGL() is called whenever the widget's overlay needs to be painted. Reimplement it in a subclass.
There is no need to call makeOverlayCurrent () because this has already been done when this function is called.
Convenience function for specifying the clearing color to OpenGL. Calls glClearColor (in RGBA mode) or glClearIndex (in color-index mode) with the color c . Applies to this widgets GL context.
另请参阅 qglColor (), QGLContext::currentContext (),和 QColor .
Convenience function for specifying a drawing color to OpenGL. Calls glColor4 (in RGBA mode) or glIndex (in color-index mode) with the color c . Applies to this widgets GL context.
注意: This function is not supported on OpenGL/ES 2.0 systems.
另请参阅 qglClearColor (), QGLContext::currentContext (),和 QColor .
Renders the current scene on a pixmap and returns the pixmap.
You can use this method on both visible and invisible QGLWidget 对象。
Internally the function renders into a framebuffer object and performs pixel readback. This has a performance penalty, meaning that this function is not suitable to be called at a high frequency.
After creating and binding the framebuffer object, the function will call initializeGL (), resizeGL (),和 paintGL (). On the next normal update initializeGL () 和 resizeGL () will be triggered again since the size of the destination pixmap and the QGLWidget 's size may differ.
The size of the pixmap will be w pixels wide and h pixels high unless one of these parameters is 0 (the default), in which case the pixmap will have the same size as the widget.
Care must be taken when using framebuffer objects in paintGL () in combination with this function. To switch back to the default framebuffer, use QGLFramebufferObject::bindDefault (). Binding FBO 0 is wrong since renderPixmap() uses a custom framebuffer instead of the one provided by the windowing system.
useContext is ignored. Historically this parameter enabled the usage of the existing GL context. This is not supported anymore since additional contexts are never created.
Overlays are not rendered onto the pixmap.
If the GL rendering context and the desktop have different bit depths, the result will most likely look surprising.
Note that the creation of display lists, modifications of the view frustum etc. should be done from within initializeGL (). If this is not done, the temporary QGLContext will not be initialized properly, and the rendered pixmap may be incomplete/corrupted.
Renders the string str into the GL context of this widget.
x and y are specified in window coordinates, with the origin in the upper left-hand corner of the window. If font is not specified, the currently set application font will be used to render the string. To change the color of the rendered text you can use the glColor() call (or the qglColor () convenience function), just before the renderText() call.
注意: This function clears the stencil buffer.
注意: This function is not supported on OpenGL/ES systems.
注意: This function temporarily disables depth-testing when the text is drawn.
注意: This function can only be used inside a QPainter::beginNativePainting ()/ QPainter::endNativePainting () block if a painter is active on the QGLWidget .
这是重载函数。
x , y and z are specified in scene or object coordinates relative to the currently set projection and model matrices. This can be useful if you want to annotate models with text labels and have the labels move with the model as it is rotated etc.
注意: This function is not supported on OpenGL/ES systems.
注意:
If depth testing is enabled before this function is called, then the drawn text will be depth-tested against the models that have already been drawn in the scene. Use
glDisable(GL_DEPTH_TEST)
before calling this function to annotate the models without depth-testing the text.
注意: This function can only be used inside a QPainter::beginNativePainting ()/ QPainter::endNativePainting () block if a painter is active on the QGLWidget .
[override virtual protected]
void
QGLWidget::
resizeEvent
(
QResizeEvent
*
event
)
重实现: QWidget::resizeEvent (QResizeEvent *event).
Handles resize events that are passed in the event parameter. Calls the virtual function resizeGL ().
[virtual protected]
void
QGLWidget::
resizeGL
(
int
width
,
int
height
)
This virtual function is called whenever the widget has been resized. The new size is passed in width and height . Reimplement it in a subclass.
There is no need to call makeCurrent () because this has already been done when this function is called.
[virtual protected]
void
QGLWidget::
resizeOverlayGL
(
int
width
,
int
height
)
This virtual function is used in the same manner as paintGL () except that it operates on the widget's overlay context instead of the widget's main context. This means that resizeOverlayGL() is called whenever the widget has been resized. The new size is passed in width and height . Reimplement it in a subclass.
There is no need to call makeOverlayCurrent () because this has already been done when this function is called.
[protected]
void
QGLWidget::
setAutoBufferSwap
(
bool
on
)
若 on is true automatic GL buffer swapping is switched on; otherwise it is switched off.
若 on is true and the widget is using a double-buffered format, the background and foreground GL buffers will automatically be swapped after each paintGL () 调用。
The buffer auto-swapping is on by default.
另请参阅 autoBufferSwap (), doubleBuffer (),和 swapBuffers ().
Set the colormap for this widget to cmap . Usually it is only top-level widgets that can have colormaps installed.
另请参阅 colormap ().
Swaps the screen contents with an off-screen buffer. This only works if the widget's format specifies double buffer mode.
Normally, there is no need to explicitly call this function because it is done automatically after each widget repaint, i.e. each time after paintGL () has been executed.
另请参阅 doubleBuffer (), setAutoBufferSwap (),和 QGLFormat::setDoubleBuffer ().