Inherits QWidget and QGL.
QGLWidget provides functionality for displaying OpenGL<sup>*</sup> graphics integrated into a Qt application. It is very simple to use. You inherit from it and use the subclass like any other QWidget, except that instead of drawing the widget's contents using QPainter etc. you use the standard OpenGL rendering commands.
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, const char *name )
: QGLWidget(parent, name) {}
protected:
void initializeGL()
{
// 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 )
{
// setup viewport, projection etc.:
glViewport( 0, 0, (GLint)w, (GLint)h );
...
glFrustum( ... );
...
}
void paintGL()
{
// draw the scene:
...
glRotatef( ... );
glMaterialfv( ... );
glBegin( GL_QUADS );
glVertex3f( ... );
glVertex3f( ... );
...
glEnd();
...
}
};
If you need to trigger a repaint from places other than paintGL() (a typical example is when using timers to animate scenes), you should call the widget's updateGL() function.
Your widget's OpenGL rendering context is made current when paintGL(), resizeGL(), or 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 QGLWidgets (see the documentation of the QGLWidget constructors for details).
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. See the examples/opengl/overlay_x11 example program for details.
<sup>*</sup> OpenGL is a trademark of Silicon Graphics, Inc. in the United States and other countries.
See also Graphics Classes and Image Processing Classes.
The default format is used. The widget will be invalid if the system has no OpenGL support.
The parent, name and widget flag, f, arguments are passed to the QWidget constructor.
If the shareWidget parameter points to a valid QGLWidget, this widget will share OpenGL display lists with shareWidget. If this widget and shareWidget have different formats, display list sharing may fail. You can check whether display list sharing succeeded 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.
See also QGLFormat::defaultFormat().
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 support.
The parent, name and widget flag, f, arguments are passed to the QWidget constructor.
If the shareWidget parameter points to a valid QGLWidget, this widget will share OpenGL display lists with shareWidget. If this widget and shareWidget have different formats, display list sharing may fail. You can check whether display list sharing succeeded 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.
See also QGLFormat::defaultFormat() and isValid().
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 support.
The parent, name and widget flag, f, arguments are passed to the QWidget constructor.
If the shareWidget parameter points to a valid QGLWidget, this widget will share OpenGL display lists with shareWidget. If this widget and shareWidget have different formats, display list sharing may fail. You can check whether display list sharing succeeded 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.
See also QGLFormat::defaultFormat() and isValid().
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 QColormap returned will be empty.
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.
QImage tex1, tex2, buf;
if ( !buf.load( "gllogo.bmp" ) ) { // Load first image from file
We create tex1 (and another variable) for OpenGL, and load a real image into buf.
tex1 = QGLWidget::convertToGLFormat( buf ); // flipped 32bit RGBA
A few lines later, we convert buf into OpenGL format and store it in tex1.
glTexImage2D( GL_TEXTURE_2D, 0, 3, tex1.width(), tex1.height(), 0,
GL_RGBA, GL_UNSIGNED_BYTE, tex1.bits() );
Note the dimension restrictions for texture images as described in the glTexImage2D() documentation. The width must be 2^m + 2*border and the height 2^n + 2*border where m and n are integers and border is either 0 or 1.
Another function in the same example uses tex1 with OpenGL.
Example: opengl/texture/gltexobj.cpp.
See also QGLFormat::doubleBuffer().
The widget's rendering context will become the current context and initializeGL() will be called if it hasn't already been called.
Depending on your hardware, you can explicitly select which color buffer to grab with a glReadBuffer() call before calling this function.
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.
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.
Does nothing if this widget has no overlay.
The widget's rendering context will become the current context and initializeGL() will be called if it hasn't already been called.
There is no need to call makeCurrent() because this has already been done when this function is called.
There is no need to call makeOverlayCurrent() because this has already been done when this function is called.
See also qglColor(), QGLContext::currentContext(), and QColor.
See also qglClearColor(), QGLContext::currentContext(), and QColor.
You can use this method on both visible and invisible QGLWidgets.
This method will create a pixmap and a temporary QGLContext to render on the pixmap. It will then call initializeGL(), resizeGL(), and paintGL() on this context. Finally, the widget's original GL context is restored.
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.
If useContext is TRUE, this method will try to be more efficient by using the existing GL context to render the pixmap. The default is FALSE. Only use TRUE if you understand the risks.
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.
x and y are specified in window coordinates, with the origin in the upper left-hand corner of the window. If fnt 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. Note that if you have GL_LIGHTING enabled, the string will not appear in the color you want. You should therefore switch lighting off before using renderText().
listBase specifies the index of the first display list that is generated by this function. The default value is 2000. 256 display lists will be generated, one for each of the first 256 characters in the font that is used to render the string. If several fonts are used in the same widget, the display lists for these fonts will follow the last generated list. You would normally not have to change this value unless you are using lists in the same range. The lists are deleted when the widget is destroyed.
Note: This function only works reliably with ASCII strings.
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.
There is no need to call makeCurrent() because this has already been done when this function is called.
There is no need to call makeOverlayCurrent() because this has already been done when this function is called.
If 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() call.
The buffer auto-swapping is on by default.
See also autoBufferSwap(), doubleBuffer(), and swapBuffers().
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.
See also doubleBuffer(), setAutoBufferSwap(), and QGLFormat::setDoubleBuffer().
The widget's rendering context will become the current context and initializeGL() will be called if it hasn't already been called.