QGLWidget(3qt)
QGLWidget β¨
okay so this is the manual page for QGLWidget, from the 3qt section, and bestie I need you to know upfront: this is PART 1, there's more coming, so don't panic when it ends mid-thought. we're just getting started π
β¨ NAME β¨
QGLWidget β the widget that renders OpenGL graphics, and honestly? iconic. π
β¨ SYNOPSIS β¨
#include <qgl.h>
Inherits QWidget and QGL. yeah. both. she's versatile like that. π¦
πΈ Public Members πΈ
QGLWidget ( QWidget * parent = 0, const char * name = 0, const QGLWidget
* shareWidget = 0, WFlags f = 0 )
QGLWidget ( QGLContext * context, QWidget * parent, const char * name =
0, const QGLWidget * shareWidget = 0, WFlags f = 0 )
QGLWidget ( const QGLFormat & format, QWidget * parent = 0, const char *
name = 0, const QGLWidget * shareWidget = 0, WFlags f = 0 )
~QGLWidget ()
void qglColor ( const QColor & c ) const
void qglClearColor ( const QColor & c ) const
bool isValid () const
bool isSharing () const
virtual void makeCurrent ()
void doneCurrent ()
bool doubleBuffer () const
virtual void swapBuffers ()
QGLFormat format () const
const QGLContext * context () const
virtual QPixmap renderPixmap ( int w = 0, int h = 0, bool useContext =
FALSE )
virtual QImage grabFrameBuffer ( bool withAlpha = FALSE )
virtual void makeOverlayCurrent ()
const QGLContext * overlayContext () const
const QGLColormap & colormap () const
void setColormap ( const QGLColormap & cmap )
void renderText ( int x, int y, const QString & str, const QFont & fnt =
QFont ( ), int listBase = 2000 )
void renderText ( double x, double y, double z, const QString & str,
const QFont & fnt = QFont ( ), int listBase = 2000 )
π Public Slots π
virtual void updateGL ()
virtual void updateOverlayGL ()
π Static Public Members π
QImage convertToGLFormat ( const QImage & img )
π§ Protected Members π§
virtual void initializeGL ()
virtual void resizeGL ( int width, int height )
virtual void paintGL ()
virtual void initializeOverlayGL ()
virtual void resizeOverlayGL ( int width, int height )
virtual void paintOverlayGL ()
void setAutoBufferSwap ( bool on )
bool autoBufferSwap () const
virtual void paintEvent ( QPaintEvent * )
virtual void resizeEvent ( QResizeEvent * )
virtual void glInit ()
virtual void glDraw ()
β¨ DESCRIPTION β¨
The QGLWidget class is a widget for rendering OpenGL graphics. that's the whole vibe. π
QGLWidget gives you the functionality for displaying OpenGL<sup>*</sup> graphics all integrated into a Qt application. It is very simple to use β like, genuinely. You inherit from it and then 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. swap the brush for the GL, periodt. π
QGLWidget provides three convenient virtual functions that you can reimplement in your subclass to perform the typical OpenGL tasks:
paintGL()β Renders the OpenGL scene. Gets called whenever the widget needs to be updated. π¨resizeGL()β Sets up the OpenGL viewport, projection, etc. Gets called whenever the the widget has been resized (and also when it is shown for the first time because all newly created widgets get a resize event automatically).initializeGL()β Sets up the OpenGL rendering context, defines display lists, etc. Gets called once before the first timeresizeGL()orpaintGL()is called.
Here is a rough outline of how a QGLWidget subclass might look β she's giving main character energy fr fr:
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. no skipping this step, bestie, it will not work and you will be sad. π
QGLWidget provides functions for requesting a new display format and you can also create widgets with customized rendering contexts. fancy. π
You can also share OpenGL display lists between QGLWidgets (see the documentation of the QGLWidget constructors for details). sharing is caring π«Ά
π¦ Overlays π¦
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:
paintOverlayGL()resizeOverlayGL()initializeOverlayGL()
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. πΈ
β¨ MEMBER FUNCTION DOCUMENTATION β¨
QGLWidget::QGLWidget ( QWidget * parent = 0, const char * name = 0, const QGLWidget * shareWidget = 0, WFlags f = 0 )
Constructs an OpenGL widget with a parent widget and a name.
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. (yes, this is the thing everyone gets wrong. don't be that person. π)
See also QGLFormat::defaultFormat().
QGLWidget::QGLWidget ( QGLContext * context, QWidget * parent, const char * name = 0, const QGLWidget * shareWidget = 0, WFlags f = 0 )
Constructs an OpenGL widget with parent parent, called name.
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. bring your own context, we love a customization moment β¨
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().
QGLWidget::QGLWidget ( const QGLFormat & format, QWidget * parent = 0, const char * name = 0, const QGLWidget * shareWidget = 0, WFlags f = 0 )
Constructs an OpenGL widget with parent parent, called name.
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. so like, you ask for the whole look, and you might get the closest available version. still cute tho π
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().
QGLWidget::~QGLWidget ()
Destroys the widget. bye bestie ππ
bool QGLWidget::autoBufferSwap () const [protected]
Returns TRUE if the widget is doing automatic GL buffer swapping; otherwise returns FALSE.
See also setAutoBufferSwap().
const QGLColormap & QGLWidget::colormap () const
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. (the child is just borrowing mom's, basically π«Ά)
If no colormap has been set for this widget, the QColormap returned will be empty.
See also setColormap().
const QGLContext * QGLWidget::context () const
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. π
okay that's part 1, we are SO not done yet, keep scrolling bestie π¦π
π QImage QGLWidget::convertToGLFormat ( const QImage & img ) [static] π
Okay so this one takes the image img and converts it into the unnamed format that OpenGL functions like glTexImage2D() are absolutely gagging for. Bestie, hear me when I say: the returned image is not usable as a QImage, but you can still use QImage::width(), QImage::height() and QImage::bits() with OpenGL. Periodt.
The next few lines are straight from the texture example. Most of that code is irrelevant so we're just quoting the relevant bits:
QImage tex1, tex2, buf;
if ( !buf.load( "gllogo.bmp" ) ) { // Load first image from file
So we create tex1 (plus 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 stash 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. No cap, that's just the rules. π
Another function in the same example uses tex1 with OpenGL.
Example: opengl/texture/gltexobj.cpp.
β¨ void QGLWidget::doneCurrent () β¨
Makes no GL context the current context. Like, none of them. Normally you don't need to call this at all β QGLContext calls it as necessary. But it can be useful in multithreaded environments. π§΅
πΈ bool QGLWidget::doubleBuffer () const πΈ
Returns TRUE if the contained GL rendering context has double buffering; otherwise returns FALSE.
See also QGLFormat::doubleBuffer().
π¦ QGLFormat QGLWidget::format () const π¦
Returns the format of the contained GL rendering context. That's it, that's the whole vibe.
π
void QGLWidget::glDraw () [virtual protected] π
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.
π void QGLWidget::glInit () [virtual protected] π
Initializes OpenGL for this widget's context. Calls the virtual function initializeGL().
π₯ QImage QGLWidget::grabFrameBuffer ( bool withAlpha = FALSE ) [virtual] π₯
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.
β void QGLWidget::initializeGL () [virtual protected] β
This virtual function is called once before the first call to paintGL() or resizeGL(), and then once whenever the widget has been assigned a new QGLContext. Reimplement it in a subclass, bestie.
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. So don't do it, it's giving redundant. π
π void QGLWidget::initializeOverlayGL () [virtual protected] π
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. Which means initializeOverlayGL() is called once before the first call to paintOverlayGL() or 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.
π bool QGLWidget::isSharing () const π
Returns TRUE if display list sharing with another QGLWidget was requested in the constructor, and the GL system was able to provide it; otherwise returns FALSE. The GL system may fail to provide display list sharing if the two QGLWidgets use different formats. Rude of it, honestly.
See also format().
π bool QGLWidget::isValid () const π
Returns TRUE if the widget has a valid GL rendering context; otherwise returns FALSE. A widget will be invalid if the system has no OpenGL support. π
π void QGLWidget::makeCurrent () [virtual] π
Makes this widget the current widget for OpenGL operations, i.e. makes the widget's rendering context the current OpenGL rendering context. Main character energy, but for GL contexts.
β¨ void QGLWidget::makeOverlayCurrent () [virtual] β¨
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(), and paintOverlayGL().
Does nothing if this widget has no overlay. π€·
See also makeCurrent().
π¦ const QGLContext * QGLWidget::overlayContext () const π¦
Returns the overlay context of this widget, or 0 if this widget has no overlay.
See also context().
π void QGLWidget::paintEvent ( QPaintEvent * ) [virtual protected] π
Handles paint events. 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.
Reimplemented from QWidget.
π₯ void QGLWidget::paintGL () [virtual protected] π₯
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.
β void QGLWidget::paintOverlayGL () [virtual protected] β
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. Which means 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.
π void QGLWidget::qglClearColor ( const QColor & c ) const π
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 the current GL context.
See also qglColor(), QGLContext::currentContext(), and QColor.
π void QGLWidget::qglColor ( const QColor & c ) const π
Convenience function for specifying a drawing color to OpenGL. Calls glColor3 (in RGBA mode) or glIndex (in color-index mode) with the color c. Applies to the current GL context.
See also qglClearColor(), QGLContext::currentContext(), and QColor.
π QPixmap QGLWidget::renderPixmap ( int w = 0, int h = 0, bool useContext = FALSE ) [virtual] π
Renders the current scene on a pixmap and returns the pixmap. β¨
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. Bestie, fr fr, only if you understand them.
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 withininitializeGL(). If this is not done, the temporaryQGLContextwill not be initialized properly, and the rendered pixmap may be incomplete/corrupted.
π void QGLWidget::renderText ( int x, int y, const QString & str, const QFont & fnt = QFont ( ), int listBase = 2000 ) π
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 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 haveGL_LIGHTINGenabled, the string will not appear in the color you want. You should therefore switch lighting off before usingrenderText(). Not me forgetting this and being confused for 20 minutes. π
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.
β¨ void QGLWidget::renderText ( double x, double y, double z, const QString & str, const QFont & fnt = QFont ( ), int listBase = 2000 ) β¨
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
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. Labels that vibe with the model? Obsessed. π
π¦ void QGLWidget::resizeEvent ( QResizeEvent * ) [virtual protected] π¦
Handles resize events. Calls the virtual function resizeGL().
Reimplemented from QWidget.
π void QGLWidget::resizeGL ( int width, int height ) [virtual protected] π
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.
π₯ void QGLWidget::resizeOverlayGL ( int width, int height ) [virtual protected] π₯
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. Which means 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.
β void QGLWidget::setAutoBufferSwap ( bool on ) [protected] β
If on is TRUE, automatic GL buffer swapping is switched on; otherwise it is switched off.
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().
π void QGLWidget::setColormap ( const QGLColormap & cmap ) π
Set the colormap for this widget to cmap. Usually it is only top-level widgets that can have colormaps installed.
See also colormap().
void QGLWidget::swapBuffers () [virtual]β okay so this one literally swaps what's on your screen with the off-screen buffer, bestie. It only works if the widget's format specifies double buffer mode β like, no double buffer, no swap, periodt. π Normally you don't even gotta call this yourself because it happens automatically after every single widget repaint β i.e. each time afterpaintGL()has been executed. It's giving "the framework already handles it, you're just here for the vibes". π¦ See alsodoubleBuffer(),setAutoBufferSwap(), andQGLFormat::setDoubleBuffer().void QGLWidget::updateGL () [virtual slot]β updates the widget by callingglDraw(). That's it, that's the whole thing, and honestly? Slay. β¨void QGLWidget::updateOverlayGL () [virtual slot]β this one updates the widget's overlay (if any, so likeβ¦ if you have one π). It will cause the virtual functionpaintOverlayGL()to be executed. The widget's rendering context will become the current context andinitializeGL()will be called if it hasn't already been called. No cap, it just handles that for you. π
π SEE ALSO π
π COPYRIGHT π
Copyright 1992-2007 Trolltech ASA, http://www.trolltech.com. See the license file included in the distribution for a complete license statement. (read the license bestie, it's right there in the distribution π
)
π AUTHOR π
Generated automatically from the source code. Not me being auto-generated, main character energy regardless β¨
π¦ BUGS π¦
If you find a bug in Qt, please report it as described in http://doc.trolltech.com/bughowto.html. Good bug reports help us to help you. Thank you. fr fr, a good bug report is a love letter to every dev who comes after you π«Ά
The definitive Qt documentation is provided in HTML format; it is located at $QTDIR/doc/html and can be read using Qt Assistant or with a web browser. This man page is provided as a convenience for those users who prefer man pages, although this format is not officially supported by Trolltech.
If you find errors in this manual page, please report them to qt-bugs@trolltech.com. Please include the name of the manual page (qglwidget.3qt) and the Qt version (3.3.8). Include both, okay? The name AND the version. Don't make them guess π
β οΈ Real talk bestie: this whole page is served up courtesy of Trolltech AS, dated 2 February 2007, and it's the QGLWidget(3qt) page β part of the Qt 3.3.8 era. Old school, but iconic. π
That's a wrap on the whole thing, babe β every flag, every virtual, every overlay, all documented, all slayed. Go forth and render responsibly. πΈπβ¨
π¦ where this page comes from
- fedora 43: qt3-devel-docs 3.3.8b-102.fc43 Β· QPL-1.0 OR GPL-2.0-only OR GPL-3.0-only
- fedora 44: qt3-devel-docs 3.3.8b-103.fc44 Β· QPL-1.0 OR GPL-2.0-only OR GPL-3.0-only
- Copyright 1992-2007 Trolltech ASA. All rights reserved. See the
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms