Qt logo

QGLWidget Class Reference


The QGLWidget class is a widget for rendering OpenGL graphics. This class is part of the Qt OpenGL Extension. More...

#include <qgl.h>

Inherits QWidget.

List of all member functions.

Public Members

Public Slots

Protected Members


Detailed Description

This class is defined in the Qt OpenGL Extension, which can be found in the qt/extensions directory. It is not included in the main Qt API.

The QGLWidget class is a widget for rendering OpenGL graphics.

QGLWidget provides functionality for displaying OpenGL graphics integrated in a Qt application. It is very simple to use: you inherit from it and use the subclass like any other QWidget, only that instead of drawing the widget's contents using QPainter & al., 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 your QGLWidget subclass may 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 other places than paintGL() (a typical example is when using timers to animate scenes), you should call the widget's updateGL() function.

When paintGL(), resizeGL() or initializeGL() is called, your widget's OpenGL rendering context has been made current. If you need to call the standard OpenGL API functions from other places (e.g. in your widget's constructor), you must call makeCurrent() first.

QGLWidget provides advanced functions for requesting a new display format, and you can even set a new rendering context.

You can achieve sharing of OpenGL display lists between QGLWidgets, see the documentation of the QGLWidget constructors for details. Portability note: Some window systems require that the sharing widgets/contexts have the same format


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 parent, name and 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.

Note: Initialization of OpenGL rendering state etc. should be done by overriding the initializeGL() function, not in the constructor of your QGLWidget subclass.

See also: QGLFormat::defaultFormat().

QGLWidget::QGLWidget ( const QGLFormat & format, 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 format argument specifies the rendering capabilities. The widget becomes invalid if the driver/hardware cannot satisfy the requested format.

The parent, name and 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.

Note: Initialization of OpenGL rendering state etc. should be done by overriding the initializeGL() function, not in the constructor of your QGLWidget subclass.

See also: QGLFormat::defaultFormat() and isValid().

QGLWidget::~QGLWidget ()

Destroys the widget.

const QGLContext * QGLWidget::context () const

Returns the current context.

See also: setContext().

bool QGLWidget::doubleBuffer () const

Returns TRUE if double buffering is set in the format for this widget.

See also: QGLFormat::doubleBuffer().

const QGLFormat & QGLWidget::format () const

Returns the widget's format.

See also: setFormat().

void QGLWidget::gl_init () [protected]

Initializes OpenGL for this widget's context. Calls the virtual function initializeGL().

void QGLWidget::initializeGL () [virtual protected]

This virtual function is called one time before the first call to paintGL() or resizeGL(), and then one time whenever the widget has been assigned a new QGLContext. Reimplement it in a subclass.

This function should take care of setting 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.

bool QGLWidget::isValid () const

Returns TRUE if the widget was able to satisfy the specified constraints.

void QGLWidget::makeCurrent () [virtual]

Makes this widget the current widget for OpenGL operations. I.e. makes this widget's rendering context the current OpenGL rendering context.

void QGLWidget::paintEvent ( QPaintEvent * ) [virtual protected]

Handles paint events. Calls the virtual function paintGL().

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::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. 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::setContext ( QGLContext * context, const QGLContext * shareContext = 0 ) [virtual]

Sets a new context for this widget. The context must be created using new. QGLWidget will delete the context when another context is set or when the widget is destroyed.

The initializeGL() function will be executed for the new context before the first resizeGL() or paintGL().

Calling setContext() will not alter any display list sharing this widget currently has, unless you explicitly request sharing with a different context by giving a shareContext parameter that points to a valid context.

See also: context() and setFormat().

void QGLWidget::setFormat ( const QGLFormat & format ) [virtual]

Sets a new format for this widget. The widget becomes invalid if the requested format cannot be satisfied.

The widget will be assigned a new QGLContext, and the initializeGL() function will be executed for this new context before the first resizeGL() or paintGL().

Calling setFormat() will not alter any display list sharing this widget currently has.

See also: format(), setContext() and isValid().

void QGLWidget::swapBuffers () [virtual]

Swaps the screen contents with an off-screen buffer. Works only if the widget's format specifies double buffer mode.

See also: doubleBuffer() and QGLFormat::setDoubleBuffer().

void QGLWidget::updateGL () [virtual slot]

Updates the widget by calling paintGL().


Search the documentation, FAQ, qt-interest archive and more (uses www.troll.no):


This file is part of the Qt toolkit, copyright © 1995-98 Troll Tech, all rights reserved.

It was generated from the following files:


Copyright © 1998 Troll TechTrademarks
Qt version 1.42