QGLWidget Class Reference

#include <qgl.h>

Inheritance diagram for QGLWidget:

Inheritance graph
[legend]
Collaboration diagram for QGLWidget:

Collaboration graph
[legend]
List of all members.

Detailed Description

The QGLWidget class is a widget for rendering OpenGL graphics.

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, except that you have the choice between using QPainter and standard OpenGL rendering commands.

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 time resizeGL() or paintGL() is called.

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()
        {
            // 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).

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 \link QGLFormat format\endlink. (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: \list \i paintOverlayGL() \i resizeOverlayGL() \i initializeOverlayGL() \endlist 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. @section Painting Techniques As described above, subclass QGLWidget to render pure 3D content in the following way: \list \o Reimplement the QGLWidget::initializeGL() and QGLWidget::resizeGL() to set up the OpenGL state and provide a perspective transformation. \o Reimplement QGLWidget::paintGL() to paint the 3D scene, calling only OpenGL functions to draw on the widget. \endlist It is also possible to draw 2D graphics onto a QGLWidget subclass, it is necessary to reimplement QGLWidget::paintEvent() and do the following: \list \o Construct a QPainter object. \o Initialize it for use on the widget with the QPainter::begin() function. \o Draw primitives using QPainter's member functions. \o Call QPainter::end() to finish painting. \endlist Overpainting 2D content on top of 3D content takes a little more effort. One approach to doing this is shown in the \l{Overpainting Example}{Overpainting} example. \e{OpenGL is a trademark of Silicon Graphics, Inc. in the United States and other countries.} \sa QGLPixelBuffer, {Hello GL Example}, {2D Painting Example}, {Overpainting Example}, {Grabber Example} Definition at line 307 of file qgl.h.

Public Slots

virtual void updateGL ()
virtual void updateOverlayGL ()

Public Member Functions

 QGLWidget (QWidget *parent=0, const QGLWidget *shareWidget=0, Qt::WindowFlags f=0)
 QGLWidget (QGLContext *context, QWidget *parent=0, const QGLWidget *shareWidget=0, Qt::WindowFlags f=0)
 QGLWidget (const QGLFormat &format, QWidget *parent=0, const QGLWidget *shareWidget=0, Qt::WindowFlags f=0)
 ~QGLWidget ()
void qglColor (const QColor &c) const
void qglClearColor (const QColor &c) const
bool isValid () const
bool isSharing () const
void makeCurrent ()
void doneCurrent ()
bool doubleBuffer () const
void swapBuffers ()
QGLFormat format () const
void setFormat (const QGLFormat &format)
const QGLContextcontext () const
void setContext (QGLContext *context, const QGLContext *shareContext=0, bool deleteOldContext=true)
QPixmap renderPixmap (int w=0, int h=0, bool useContext=false)
QImage grabFrameBuffer (bool withAlpha=false)
void makeOverlayCurrent ()
const QGLContextoverlayContext () const
void setMouseTracking (bool enable)
const QGLColormapcolormap () const
void setColormap (const QGLColormap &map)
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)
QPaintEnginepaintEngine () const
GLuint bindTexture (const QImage &image, GLenum target=GL_TEXTURE_2D, GLint format=GL_RGBA)
GLuint bindTexture (const QPixmap &pixmap, GLenum target=GL_TEXTURE_2D, GLint format=GL_RGBA)
GLuint bindTexture (const QString &fileName)
void deleteTexture (GLuint tx_id)

Static Public Member Functions

static QImage convertToGLFormat (const QImage &img)

Protected Member Functions

bool event (QEvent *)
virtual void initializeGL ()
virtual void resizeGL (int w, int h)
virtual void paintGL ()
virtual void initializeOverlayGL ()
virtual void resizeOverlayGL (int w, int h)
virtual void paintOverlayGL ()
void setAutoBufferSwap (bool on)
bool autoBufferSwap () const
void paintEvent (QPaintEvent *)
void resizeEvent (QResizeEvent *)
virtual void glInit ()
virtual void glDraw ()
int fontDisplayListBase (const QFont &fnt, int listBase=2000)

Friends

class QGLDrawable
class QGLPixelBuffer
class QGLPixelBufferPrivate
class QGLContext
class QGLOverlayWidget
class QOpenGLPaintEngine


Constructor & Destructor Documentation

QGLWidget::QGLWidget ( QWidget parent = 0,
const QGLWidget shareWidget = 0,
Qt::WindowFlags  f = 0 
) [explicit]

Constructs an OpenGL widget with a parent widget.

The default format is used. The widget will be invalid if the system has no OpenGL support.

The parent 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(), {Textures Example}

Definition at line 2372 of file qgl.cpp.

References d, QGLFormat::defaultFormat(), QGLContext, QWidget::setAttribute(), QWidget::setAutoFillBackground(), Qt::WA_NoSystemBackground, and Qt::WA_PaintOnScreen.

02373     : QWidget(*(new QGLWidgetPrivate), parent, f | Qt::MSWindowsOwnDC)
02374 {
02375     Q_D(QGLWidget);
02376     setAttribute(Qt::WA_PaintOnScreen);
02377     setAttribute(Qt::WA_NoSystemBackground);
02378     setAutoFillBackground(true); // for compatibility
02379     d->init(new QGLContext(QGLFormat::defaultFormat(), this), shareWidget);
02380 }

Here is the call graph for this function:

QGLWidget::QGLWidget ( QGLContext context,
QWidget parent = 0,
const QGLWidget shareWidget = 0,
Qt::WindowFlags  f = 0 
) [explicit]

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 support.

The parent 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(), isValid()

Definition at line 2447 of file qgl.cpp.

References context(), d, QWidget::setAttribute(), QWidget::setAutoFillBackground(), Qt::WA_NoSystemBackground, and Qt::WA_PaintOnScreen.

02449     : QWidget(*(new QGLWidgetPrivate), parent, f | Qt::MSWindowsOwnDC)
02450 {
02451     Q_D(QGLWidget);
02452     setAttribute(Qt::WA_PaintOnScreen);
02453     setAttribute(Qt::WA_NoSystemBackground);
02454     setAutoFillBackground(true); // for compatibility
02455     d->init(context, shareWidget);
02456 }

Here is the call graph for this function:

QGLWidget::QGLWidget ( const QGLFormat format,
QWidget parent = 0,
const QGLWidget shareWidget = 0,
Qt::WindowFlags  f = 0 
) [explicit]

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 support.

The parent 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(), isValid()

Definition at line 2411 of file qgl.cpp.

References d, format(), QGLContext, QWidget::setAttribute(), QWidget::setAutoFillBackground(), Qt::WA_NoSystemBackground, and Qt::WA_PaintOnScreen.

02413     : QWidget(*(new QGLWidgetPrivate), parent, f | Qt::MSWindowsOwnDC)
02414 {
02415     Q_D(QGLWidget);
02416     setAttribute(Qt::WA_PaintOnScreen);
02417     setAttribute(Qt::WA_NoSystemBackground);
02418     setAutoFillBackground(true); // for compatibility
02419     d->init(new QGLContext(format, this), shareWidget);
02420 }

Here is the call graph for this function:

QGLWidget::~QGLWidget (  ) 

Destroys the widget.

Definition at line 2462 of file qgl.cpp.

References d, and QWidget::winId().

02463 {
02464     Q_D(QGLWidget);
02465 #if defined(GLX_MESA_release_buffers) && defined(QGL_USE_MESA_EXT)
02466     bool doRelease = (glcx && glcx->windowCreated());
02467 #endif
02468     delete d->glcx;
02469 #if defined(Q_WGL)
02470     delete d->olcx;
02471 #endif
02472 #ifdef Q_WS_MAC
02473     delete d->watcher;
02474     d->watcher = 0;
02475 #endif
02476 #if defined(GLX_MESA_release_buffers) && defined(QGL_USE_MESA_EXT)
02477     if (doRelease)
02478         glXReleaseBuffersMESA(x11Display(), winId());
02479 #endif
02480     d->cleanupColormaps();
02481 }

Here is the call graph for this function:


Member Function Documentation

void QGLWidget::qglColor ( const QColor c  )  const

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.

See also:
qglClearColor(), QGLContext::currentContext(), QColor

Definition at line 3064 of file qgl.cpp.

References c, QGLContext::colorIndex(), QGLContext::currentContext(), d, QGLContext::format(), i, and QGLFormat::rgba().

Referenced by GLWidget::extrude(), and GLWidget::quad().

03065 {
03066 #ifdef Q_WS_QWS
03067     glColor4f(c.red()/255.0, c.green()/255.0, c.blue()/255.0, c.alpha()/255.0);
03068 #else
03069     Q_D(const QGLWidget);
03070     const QGLContext *ctx = QGLContext::currentContext();
03071     if (ctx) {
03072         if (ctx->format().rgba())
03073             glColor4ub(c.red(), c.green(), c.blue(), c.alpha());
03074         else if (!d->cmap.isEmpty()) { // QGLColormap in use?
03075             int i = d->cmap.find(c.rgb());
03076             if (i < 0)
03077                 i = d->cmap.findNearest(c.rgb());
03078             glIndexi(i);
03079         } else
03080             glIndexi(ctx->colorIndex(c));
03081     }
03082 #endif
03083 }

Here is the call graph for this function:

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 this widgets GL context.

See also:
qglColor(), QGLContext::currentContext(), QColor

Definition at line 3093 of file qgl.cpp.

References c, QGLContext::colorIndex(), QGLContext::currentContext(), d, QGLContext::format(), i, and QGLFormat::rgba().

Referenced by QGLOverlayWidget::initializeGL(), and QView3DWidget::initializeGL().

03094 {
03095 #ifdef Q_WS_QWS
03096     glClearColor((GLfloat)c.red() / 255.0, (GLfloat)c.green() / 255.0,
03097                  (GLfloat)c.blue() / 255.0, (GLfloat) c.alpha() / 255.0);
03098 #else
03099     Q_D(const QGLWidget);
03100     const QGLContext *ctx = QGLContext::currentContext();
03101     if (ctx) {
03102         if (ctx->format().rgba())
03103             glClearColor((GLfloat)c.red() / 255.0, (GLfloat)c.green() / 255.0,
03104                           (GLfloat)c.blue() / 255.0, (GLfloat) c.alpha() / 255.0);
03105         else if (!d->cmap.isEmpty()) { // QGLColormap in use?
03106             int i = d->cmap.find(c.rgb());
03107             if (i < 0)
03108                 i = d->cmap.findNearest(c.rgb());
03109             glClearIndex(i);
03110         } else
03111             glClearIndex(ctx->colorIndex(c));
03112     }
03113 #endif
03114 }

Here is the call graph for this function:

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.

Definition at line 2530 of file qgl.cpp.

References d.

Referenced by glDraw(), glInit(), renderPixmap(), and resizeEvent().

02531 {
02532     Q_D(const QGLWidget);
02533     return d->glcx && d->glcx->isValid();
02534 }

bool QGLWidget::isSharing (  )  const

Returns true if this widget's GL context is shared with another GL context, otherwise false is returned. The GL system may fail to provide context sharing if the two QGLWidgets use different formats.

See also:
format()

Definition at line 2547 of file qgl.cpp.

References d.

02548 {
02549     Q_D(const QGLWidget);
02550     return d->glcx->isSharing();
02551 }

void QGLWidget::makeCurrent (  ) 

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

Definition at line 2561 of file qgl.cpp.

References d.

Referenced by QView3DWidget::addWidget(), QView3DWidget::beginAddingWidgets(), QView3DWidget::clear(), QView3DWidget::endAddingWidgets(), event(), glDraw(), glInit(), GLWidget::GLWidget(), grabFrameBuffer(), QGLExtensions::init(), GLWidget::initPbuffer(), QView3DWidget::keyReleaseEvent(), QGLDrawable::makeCurrent(), QView3DWidget::mouseMoveEvent(), renderText(), resizeEvent(), QView3DWidget::wheelEvent(), and QView3DWidget::widgetAt().

02562 {
02563     Q_D(QGLWidget);
02564     d->glcx->makeCurrent();
02565 }

void QGLWidget::doneCurrent (  ) 

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.

Definition at line 2575 of file qgl.cpp.

References d.

Referenced by event().

02576 {
02577     Q_D(QGLWidget);
02578     d->glcx->doneCurrent();
02579 }

bool QGLWidget::doubleBuffer (  )  const

Returns true if the contained GL rendering context has double buffering; otherwise returns false.

See also:
QGLFormat::doubleBuffer()

Definition at line 3420 of file qgl.cpp.

References d.

Referenced by glDraw().

03421 {
03422     Q_D(const QGLWidget);
03423     return d->glcx->format().doubleBuffer();
03424 }

void QGLWidget::swapBuffers (  ) 

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.

See also:
doubleBuffer(), setAutoBufferSwap(), QGLFormat::setDoubleBuffer()

Definition at line 2594 of file qgl.cpp.

References d.

Referenced by glDraw(), and QGLDrawable::swapBuffers().

02595 {
02596     Q_D(QGLWidget);
02597     d->glcx->swapBuffers();
02598 }

QGLFormat QGLWidget::format (  )  const

Returns the format of the contained GL rendering context.

Definition at line 3408 of file qgl.cpp.

References d.

Referenced by QGLDrawable::format(), grabFrameBuffer(), main(), QGLWidget(), and setFormat().

03409 {
03410     Q_D(const QGLWidget);
03411     return d->glcx->format();
03412 }

void QGLWidget::setFormat ( const QGLFormat format  ) 

Sets a new format for this widget.

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 rendering context format obtained.

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().

This method will try to keep any existing display list sharing with other QGLWidgets, but it may fail. Use isSharing() to test.

See also:
format(), isSharing(), isValid()

Definition at line 2645 of file qgl.cpp.

References format(), QGLContext, and setContext().

02646 {
02647     setContext(new QGLContext(format,this));
02648 }

Here is the call graph for this function:

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.

Definition at line 3414 of file qgl.cpp.

References d.

Referenced by QGLWidgetPrivate::initContext(), QGLOverlayWidget::initializeGL(), QGLWidget(), and setContext().

03415 {
03416     Q_D(const QGLWidget);
03417     return d->glcx;
03418 }

void QGLWidget::setContext ( QGLContext context,
const QGLContext shareContext = 0,
bool  deleteOldContext = true 
)

Definition at line 1110 of file qgl_x11.cpp.

References a, QWidget::backgroundRole(), Qt::black, choose_cmap(), context(), QWidget::create(), d, QGLContext::device(), QGLContext::deviceIsPixmap(), QX11Info::display(), QWidget::height(), QWidget::hide(), QColormap::instance(), QWidget::isVisible(), p, QWidget::palette(), QWidget::parentWidget(), QColormap::pixel(), qWarning(), QWidget::show(), QWidget::testAttribute(), w, Qt::WA_WState_Created, QWidget::width(), QWidget::window(), QGLContext::windowCreated(), QWidget::winId(), QWidget::x(), X11, and QWidget::y().

Referenced by setFormat().

01113 {
01114     Q_D(QGLWidget);
01115     if (context == 0) {
01116         qWarning("QGLWidget::setContext: Cannot set null context");
01117         return;
01118     }
01119     if (!context->deviceIsPixmap() && context->device() != this) {
01120         qWarning("QGLWidget::setContext: Context must refer to this widget");
01121         return;
01122     }
01123 
01124     if (d->glcx)
01125         d->glcx->doneCurrent();
01126     QGLContext* oldcx = d->glcx;
01127     d->glcx = context;
01128 
01129     bool createFailed = false;
01130     if (!d->glcx->isValid()) {
01131         if (!d->glcx->create(shareContext ? shareContext : oldcx))
01132             createFailed = true;
01133     }
01134     if (createFailed) {
01135         if (deleteOldContext)
01136             delete oldcx;
01137         return;
01138     }
01139 
01140     if (d->glcx->windowCreated() || d->glcx->deviceIsPixmap()) {
01141         if (deleteOldContext)
01142             delete oldcx;
01143         return;
01144     }
01145 
01146     bool visible = isVisible();
01147     if (visible)
01148         hide();
01149 
01150     XVisualInfo *vi = (XVisualInfo*)d->glcx->d_func()->vi;
01151     XSetWindowAttributes a;
01152 
01153     QColormap colmap = QColormap::instance(vi->screen);
01154     a.colormap = choose_cmap(QX11Info::display(), vi);        // find best colormap
01155     a.background_pixel = colmap.pixel(palette().color(backgroundRole()));
01156     a.border_pixel = colmap.pixel(Qt::black);
01157     Window p = RootWindow(X11->display, vi->screen);
01158     if (parentWidget())
01159         p = parentWidget()->winId();
01160 
01161     Window w = XCreateWindow(X11->display, p, x(), y(), width(), height(),
01162                               0, vi->depth, InputOutput, vi->visual,
01163                               CWBackPixel|CWBorderPixel|CWColormap, &a);
01164     Window *cmw;
01165     Window *cmwret;
01166     int count;
01167     if (XGetWMColormapWindows(X11->display, window()->winId(),
01168                                 &cmwret, &count)) {
01169         cmw = new Window[count+1];
01170         memcpy((char *)cmw, (char *)cmwret, sizeof(Window)*count);
01171         XFree((char *)cmwret);
01172         int i;
01173         for (i=0; i<count; i++) {
01174             if (cmw[i] == winId()) {                // replace old window
01175                 cmw[i] = w;
01176                 break;
01177             }
01178         }
01179         if (i >= count)                        // append new window
01180             cmw[count++] = w;
01181     } else {
01182         count = 1;
01183         cmw = new Window[count];
01184         cmw[0] = w;
01185     }
01186 
01187 #if defined(GLX_MESA_release_buffers) && defined(QGL_USE_MESA_EXT)
01188     if (oldcx && oldcx->windowCreated())
01189         glXReleaseBuffersMESA(X11->display, winId());
01190 #endif
01191     if (deleteOldContext)
01192         delete oldcx;
01193     oldcx = 0;
01194 
01195     if (testAttribute(Qt::WA_WState_Created))
01196         create(w);
01197     else
01198         d->createWinId(w);
01199     XSetWMColormapWindows(X11->display, window()->winId(), cmw, count);
01200     delete [] cmw;
01201 
01202     // calling QWidget::create() will always result in a new paint
01203     // engine being created - get rid of it and replace it with our
01204     // own
01205 
01206     if (visible)
01207         show();
01208     XFlush(X11->display);
01209     d->glcx->setWindowCreated(true);
01210 }

Here is the call graph for this function:

QPixmap QGLWidget::renderPixmap ( int  w = 0,
int  h = 0,
bool  useContext = false 
)

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. Note that under Windows a temporary context has to be created and usage of the useContext parameter is not supported.

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.

Definition at line 2886 of file qgl.cpp.

References QX11Info::appVisual(), d, QPixmap::depth(), QX11Info::depth(), QGLContext::doneCurrent(), QPixmap::fromImage(), image, isValid(), QGLContext::makeCurrent(), p, QGLContext, QWidget::QPixmap, qt_x11_preferred_pixmap_depth, QGLFormat::setDirectRendering(), QGLFormat::setDoubleBuffer(), QWidget::size(), QPixmap::toImage(), updateGL(), and QX11Info::visual().

Referenced by MainWindow::renderIntoPixmap().

02887 {
02888     Q_D(QGLWidget);
02889     QSize sz = size();
02890     if ((w > 0) && (h > 0))
02891         sz = QSize(w, h);
02892 
02893 #if defined(Q_WS_X11)
02894     extern int qt_x11_preferred_pixmap_depth;
02895     int old_depth = qt_x11_preferred_pixmap_depth;
02896     qt_x11_preferred_pixmap_depth = x11Info().depth();
02897     QPixmap pm(sz);
02898     qt_x11_preferred_pixmap_depth = old_depth;
02899     QX11Info xinfo = x11Info();
02900 
02901     // make sure we use a pixmap with the same depth/visual as the widget
02902     if (xinfo.visual() != QX11Info::appVisual()) {
02903         QX11InfoData* xd = pm.x11Info().getX11Data(true);
02904         xd->depth = xinfo.depth();
02905         xd->visual = static_cast<Visual *>(xinfo.visual());
02906         const_cast<QX11Info &>(pm.x11Info()).setX11Data(xd);
02907     }
02908 
02909 #else
02910     QPixmap pm(sz);
02911 #endif
02912 
02913     d->glcx->doneCurrent();
02914 
02915     bool success = true;
02916 
02917     if (useContext && isValid() && d->renderCxPm(&pm))
02918         return pm;
02919 
02920     QGLFormat fmt = d->glcx->requestedFormat();
02921     fmt.setDirectRendering(false);                // Direct is unlikely to work
02922     fmt.setDoubleBuffer(false);                // We don't need dbl buf
02923 
02924     QGLContext* ocx = d->glcx;
02925     ocx->doneCurrent();
02926     d->glcx = new QGLContext(fmt, &pm);
02927     d->glcx->create();
02928 
02929     if (d->glcx->isValid())
02930         updateGL();
02931     else
02932         success = false;
02933 
02934     delete d->glcx;
02935     d->glcx = ocx;
02936 
02937     ocx->makeCurrent();
02938 
02939     if (success) {
02940 #if defined(Q_WS_X11)
02941         if (xinfo.visual() != QX11Info::appVisual()) {
02942             QImage image = pm.toImage();
02943             QPixmap p = QPixmap::fromImage(image);
02944             return p;
02945         }
02946 #endif
02947         return pm;
02948     }
02949     return QPixmap();
02950 }

Here is the call graph for this function:

QImage QGLWidget::grabFrameBuffer ( bool  withAlpha = false  ) 

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.

Definition at line 2962 of file qgl.cpp.

References a, alpha, QImage::bits(), QColormap::colormap(), format(), QImage::Format_ARGB32, QImage::Format_Indexed8, QImage::Format_RGB32, h, QWidget::height(), i, QColormap::instance(), makeCurrent(), QImage::mirrored(), p, QImage::rgbSwapped(), QImage::setColor(), QImage::setNumColors(), w, and QWidget::width().

Referenced by MainWindow::grabFrameBuffer().

02963 {
02964     makeCurrent();
02965     QImage res;
02966     int w = width();
02967     int h = height();
02968     if (format().rgba()) {
02969         res = QImage(w, h, withAlpha ? QImage::Format_ARGB32 : QImage::Format_RGB32);
02970         glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, res.bits());
02971         if (QSysInfo::ByteOrder == QSysInfo::BigEndian) {
02972             // OpenGL gives RGBA; Qt wants ARGB
02973             uint *p = (uint*)res.bits();
02974             uint *end = p + w*h;
02975             if (withAlpha && format().alpha()) {
02976                 while (p < end) {
02977                     uint a = *p << 24;
02978                     *p = (*p >> 8) | a;
02979                     p++;
02980                 }
02981             } else {
02982                 while (p < end) {
02983                     *p = 0xFF000000 | (*p>>8);
02984                     ++p;
02985                 }
02986             }
02987         } else {
02988             // OpenGL gives ABGR (i.e. RGBA backwards); Qt wants ARGB
02989             res = res.rgbSwapped();
02990         }
02991     } else {
02992 #if defined (Q_WS_WIN)
02993         res = QImage(w, h, QImage::Format_Indexed8);
02994         glReadPixels(0, 0, w, h, GL_COLOR_INDEX, GL_UNSIGNED_BYTE, res.bits());
02995         const QVector<QColor> pal = QColormap::instance().colormap();
02996         if (pal.size()) {
02997             res.setNumColors(pal.size());
02998             for (int i = 0; i < pal.size(); i++)
02999                 res.setColor(i, pal.at(i).rgb());
03000         }
03001 #endif
03002     }
03003 
03004     return res.mirrored();
03005 }

Here is the call graph for this function:

void QGLWidget::makeOverlayCurrent (  ) 

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()

Definition at line 1088 of file qgl_x11.cpp.

References d.

01089 {
01090     Q_D(QGLWidget);
01091     if (d->olw)
01092         d->olw->makeCurrent();
01093 }

const QGLContext * QGLWidget::overlayContext (  )  const

Returns the overlay context of this widget, or 0 if this widget has no overlay.

See also:
context()

Definition at line 1078 of file qgl_x11.cpp.

References d.

01079 {
01080     Q_D(const QGLWidget);
01081     if (d->olw)
01082         return d->olw->context();
01083     else
01084         return 0;
01085 }

QImage QGLWidget::convertToGLFormat ( const QImage img  )  [static]

Converts the image img into the unnamed format expected by OpenGL functions such as glTexImage2D(). The returned image is not usable as a QImage, but QImage::width(), QImage::height() and QImage::bits() may be used with OpenGL.

###

opengl/texture example The following few lines are from the texture example. Most of the code is irrelevant, so we just quote the relevant bits:

opengl/texture/gltexobj.cpp tex1 tex1 gllogo.bmp

We create tex1 (and another variable) for OpenGL, and load a real image into buf.

convertToGLFormat convertToGLFormat

A few lines later, we convert buf into OpenGL format and store it in tex1.

glTexImage2D glTexImage2D 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.

Definition at line 3157 of file qgl.cpp.

References QImage::convertToFormat(), QImage::Format_ARGB32, QImage::height(), i, QImage::mirrored(), p, QImage::rgbSwapped(), QImage::scanLine(), and QImage::width().

Referenced by QView3DWidget::addTexture().

03158 {
03159     QImage res = img.convertToFormat(QImage::Format_ARGB32);
03160     res = res.mirrored();
03161 
03162     if (QSysInfo::ByteOrder == QSysInfo::BigEndian) {
03163         // Qt has ARGB; OpenGL wants RGBA
03164         for (int i=0; i < res.height(); i++) {
03165             uint *p = (uint*)res.scanLine(i);
03166             uint *end = p + res.width();
03167             while (p < end) {
03168                 *p = (*p << 8) | ((*p >> 24) & 0xFF);
03169                 p++;
03170             }
03171         }
03172     }
03173     else {
03174         // Qt has ARGB; OpenGL wants ABGR (i.e. RGBA backwards)
03175         res = res.rgbSwapped();
03176     }
03177     return res;
03178 }

Here is the call graph for this function:

void QGLWidget::setMouseTracking ( bool  enable  ) 

If enable is true then mouse tracking is enabled; otherwise it is disabled.

Reimplemented from QWidget.

Definition at line 1055 of file qgl_x11.cpp.

References d, and QWidget::setMouseTracking().

01056 {
01057     Q_D(QGLWidget);
01058     if (d->olw)
01059         d->olw->setMouseTracking(enable);
01060     QWidget::setMouseTracking(enable);
01061 }

Here is the call graph for this function:

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.

If no colormap has been set for this widget, the QColormap returned will be empty.

See also:
setColormap()

Definition at line 1212 of file qgl_x11.cpp.

References d.

01213 {
01214     Q_D(const QGLWidget);
01215     return d->cmap;
01216 }

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()

Definition at line 1280 of file qgl_x11.cpp.

References c, d, qCanAllocColors(), qStoreColors(), qWarning(), QWidget::window(), QWidget::winId(), and X11.

01281 {
01282     Q_D(QGLWidget);
01283     QWidget * tlw = window(); // must return a valid widget
01284 
01285     d->cmap = c;
01286     if (!d->cmap.handle())
01287         return;
01288 
01289     if (!qCanAllocColors(this)) {
01290         qWarning("QGLWidget::setColormap: Cannot create a read/write "
01291                   "colormap for this visual");
01292         return;
01293     }
01294 
01295     // If the child GL widget is not of the same visual class as the
01296     // toplevel widget we will get in trouble..
01297     Window wid = tlw->winId();
01298     Visual * vis = (Visual *) tlw->x11Info().visual();;
01299     VisualID cvId = XVisualIDFromVisual((Visual *) x11Info().visual());
01300     VisualID tvId = XVisualIDFromVisual((Visual *) tlw->x11Info().visual());
01301     if (cvId != tvId) {
01302         wid = winId();
01303         vis = (Visual *) x11Info().visual();
01304     }
01305 
01306     if (!d->cmap.handle()) // allocate a cmap if necessary
01307         d->cmap.setHandle(XCreateColormap(X11->display, wid, vis, AllocAll));
01308 
01309     qStoreColors(this, (Colormap) d->cmap.handle(), c);
01310     XSetWindowColormap(X11->display, wid, (Colormap) d->cmap.handle());
01311 
01312     // tell the wm that this window has a special colormap
01313     Window * cmw;
01314     Window * cmwret;
01315     int count;
01316     if (XGetWMColormapWindows(X11->display, tlw->winId(), &cmwret, &count))
01317     {
01318         cmw = new Window[count+1];
01319         memcpy((char *) cmw, (char *) cmwret, sizeof(Window) * count);
01320         XFree((char *) cmwret);
01321         int i;
01322         for (i = 0; i < count; i++) {
01323             if (cmw[i] == winId()) {
01324                 break;
01325             }
01326         }
01327         if (i >= count)   // append new window only if not in the list
01328             cmw[count++] = winId();
01329     } else {
01330         count = 1;
01331         cmw = new Window[count];
01332         cmw[0] = winId();
01333     }
01334     XSetWMColormapWindows(X11->display, tlw->winId(), cmw, count);
01335     delete [] cmw;
01336 }

Here is the call graph for this function:

void QGLWidget::renderText ( int  x,
int  y,
const QString str,
const QFont font = 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 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. 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.

Warning:
This function only works reliably with ASCII strings.
To draw strings with unicode characters, or draw text to a QGLPixelBuffer, open a QPainter and call QPainter::drawText() instead.

Definition at line 3308 of file qgl.cpp.

References QWidget::font(), fontDisplayListBase(), QWidget::height(), makeCurrent(), NULL, QFont::overline(), qt_drawFontLining(), QFont::strikeOut(), QString::toLatin1(), QFont::underline(), and QWidget::width().

Referenced by QView3DWidget::paintGL().

03309 {
03310 #ifndef Q_WS_QWS
03311     makeCurrent();
03312     glPushAttrib(GL_ALL_ATTRIB_BITS);
03313     glDisable(GL_TEXTURE_1D);
03314     glDisable(GL_TEXTURE_2D);
03315     glDisable(GL_DEPTH_TEST);
03316     glDisable(GL_CULL_FACE);
03317 
03318     glMatrixMode(GL_PROJECTION);
03319     glPushMatrix();
03320     glLoadIdentity();
03321     glOrtho(0, width(), height(), 0, -1, 1);
03322     glMatrixMode(GL_MODELVIEW);
03323     glPushMatrix();
03324     glLoadIdentity();
03325 
03326     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
03327     glEnable(GL_BLEND);
03328     glAlphaFunc(GL_GREATER, 0.0);
03329     glEnable(GL_ALPHA_TEST);
03330     glRasterPos2i(0, 0);
03331     glBitmap(0, 0, 0, 0, x, -y, NULL);
03332     glListBase(fontDisplayListBase(font, listBase));
03333     QByteArray cstr(str.toLatin1());
03334     glCallLists(cstr.size(), GL_UNSIGNED_BYTE, cstr.constData());
03335 
03336     if (font.underline() || font.strikeOut() || font.overline())
03337         qt_drawFontLining(x, y, str, font);
03338 
03339     glPopMatrix();
03340     glMatrixMode(GL_PROJECTION);
03341     glPopMatrix();
03342     glPopAttrib();
03343 #endif
03344 }

Here is the call graph for this function:

void QGLWidget::renderText ( double  x,
double  y,
double  z,
const QString str,
const QFont font = QFont(),
int  listBase = 2000 
)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. 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.

To draw strings with unicode characters, or draw text to a QGLPixelBuffer, open a QPainter and call QPainter::drawText() instead. Note that you will have to do the screen-coordinate to object-coordinate transform yourself.

Definition at line 3358 of file qgl.cpp.

References QWidget::font(), fontDisplayListBase(), GLint, QWidget::height(), makeCurrent(), QFont::overline(), qgluProject(), qt_drawFontLining(), QFont::strikeOut(), QString::toLatin1(), QFont::underline(), and QWidget::width().

03360 {
03361 #ifndef Q_WS_QWS
03362     makeCurrent();
03363     glPushAttrib(GL_ALL_ATTRIB_BITS);
03364 
03365     glDisable(GL_TEXTURE_1D);
03366     glDisable(GL_TEXTURE_2D);
03367     glDisable(GL_CULL_FACE);
03368 
03369     glRasterPos3d(x, y, z);
03370     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
03371     glEnable(GL_BLEND);
03372     glAlphaFunc(GL_GREATER, 0.0);
03373     glEnable(GL_ALPHA_TEST);
03374     glListBase(fontDisplayListBase(font, listBase));
03375     QByteArray cstr(str.toLatin1());
03376     glCallLists(cstr.size(), GL_UNSIGNED_BYTE, cstr.constData());
03377 
03378     if (font.underline() || font.strikeOut() || font.overline()) {
03379         GLdouble model[4][4], proj[4][4];
03380         GLint view[4];
03381         glGetDoublev(GL_MODELVIEW_MATRIX, &model[0][0]);
03382         glGetDoublev(GL_PROJECTION_MATRIX, &proj[0][0]);
03383         glGetIntegerv(GL_VIEWPORT, &view[0]);
03384 
03385         GLdouble win_x = 0, win_y = 0, win_z = 0;
03386         qgluProject(x, y, z, &model[0][0], &proj[0][0], &view[0],
03387                     &win_x, &win_y, &win_z);
03388         win_y = height() - win_y; // y is inverted
03389 
03390         glMatrixMode(GL_PROJECTION);
03391         glPushMatrix();
03392         glLoadIdentity();
03393         glOrtho(0, width(), height(), 0, -1, 1);
03394         glMatrixMode(GL_MODELVIEW);
03395         glPushMatrix();
03396         glLoadIdentity();
03397 
03398         qt_drawFontLining(win_x, win_y, str, font);
03399 
03400         glPopMatrix();
03401         glMatrixMode(GL_PROJECTION);
03402         glPopMatrix();
03403     }
03404     glPopAttrib();
03405 #endif
03406 }

Here is the call graph for this function:

QPaintEngine * QGLWidget::paintEngine (  )  const [virtual]

Returns a pointer to the paint engine used for drawing on the device.

Reimplemented from QWidget.

Definition at line 3494 of file qgl.cpp.

Referenced by QWidgetPrivate::drawWidget().

03495 {
03496     return qt_gl_engine();
03497 }

GLuint QGLWidget::bindTexture ( const QImage image,
GLenum  target = GL_TEXTURE_2D,
GLint  format = GL_RGBA 
)

Calls QGLContext:bindTexture(image, target, format) on the currently set context.

See also:
deleteTexture()

Definition at line 3444 of file qgl.cpp.

References d, and image.

Referenced by QGLDrawable::bindTexture(), and GLWidget::initializeGL().

03445 {
03446     Q_D(QGLWidget);
03447     return d->glcx->bindTexture(image, target, format);
03448 }

GLuint QGLWidget::bindTexture ( const QPixmap pixmap,
GLenum  target = GL_TEXTURE_2D,
GLint  format = GL_RGBA 
)

Calls QGLContext:bindTexture(pixmap, target, format) on the currently set context.

See also:
deleteTexture()

Definition at line 3456 of file qgl.cpp.

References d.

03457 {
03458     Q_D(QGLWidget);
03459     return d->glcx->bindTexture(pixmap, target, format);
03460 }

GLuint QGLWidget::bindTexture ( const QString fileName  ) 

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Calls QGLContext::bindTexture(fileName) on the currently set context.

See also:
deleteTexture()

Definition at line 3468 of file qgl.cpp.

References d.

03469 {
03470     Q_D(QGLWidget);
03471     return d->glcx->bindTexture(fileName);
03472 }

void QGLWidget::deleteTexture ( GLuint  id  ) 

Calls QGLContext::deleteTexture(id) on the currently set context.

See also:
bindTexture()

Definition at line 3480 of file qgl.cpp.

References d.

03481 {
03482     Q_D(QGLWidget);
03483     d->glcx->deleteTexture(id);
03484 }

void QGLWidget::updateGL (  )  [virtual, slot]

Updates the widget by calling glDraw().

Definition at line 2700 of file qgl.cpp.

References glDraw(), and QWidget::updatesEnabled().

Referenced by GLWidget::advanceGears(), QView3DWidget::keyReleaseEvent(), QView3DWidget::mouseMoveEvent(), renderPixmap(), GLWidget::rotateBy(), GLWidget::setClearColor(), GLWidget::setXRotation(), GLWidget::setYRotation(), GLWidget::setZRotation(), and QView3DWidget::wheelEvent().

02701 {
02702     if (updatesEnabled())
02703         glDraw();
02704 }

void QGLWidget::updateOverlayGL (  )  [virtual, slot]

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.

Definition at line 1096 of file qgl_x11.cpp.

References d.

Referenced by paintEvent().

01097 {
01098     Q_D(QGLWidget);
01099     if (d->olw)
01100         d->olw->updateGL();
01101 }

bool QGLWidget::event ( QEvent e  )  [protected, virtual]

Reimplemented from QWidget.

Definition at line 1041 of file qgl_x11.cpp.

References doneCurrent(), QWidget::event(), QEvent::Hide, makeCurrent(), and QEvent::type().

Referenced by GLWidget::paintEvent(), and GLWidget::showEvent().

01042 {
01043     // prevents X errors on some systems, where we get a flush to a
01044     // hidden widget
01045     if (e->type() == QEvent::Hide) {
01046         makeCurrent();
01047         glFinish();
01048         doneCurrent();
01049     }
01050 
01051     return QWidget::event(e);
01052 }

Here is the call graph for this function:

void QGLWidget::initializeGL (  )  [protected, virtual]

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.

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.

Definition at line 2730 of file qgl.cpp.

Referenced by glInit().

02731 {
02732 }

void QGLWidget::resizeGL ( int  width,
int  height 
) [protected, virtual]

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.

Definition at line 2759 of file qgl.cpp.

Referenced by glDraw(), and resizeEvent().

02760 {
02761 }

void QGLWidget::paintGL (  )  [protected, virtual]

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.

Definition at line 2743 of file qgl.cpp.

Referenced by glDraw().

02744 {
02745 }

void QGLWidget::initializeOverlayGL (  )  [protected, virtual]

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() 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.

Definition at line 2779 of file qgl.cpp.

Referenced by QGLOverlayWidget::initializeGL().

02780 {
02781 }

void QGLWidget::resizeOverlayGL ( int  width,
int  height 
) [protected, virtual]

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.

Definition at line 2813 of file qgl.cpp.

Referenced by QGLOverlayWidget::resizeGL().

02814 {
02815 }

void QGLWidget::paintOverlayGL (  )  [protected, virtual]

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.

Definition at line 2795 of file qgl.cpp.

Referenced by QGLOverlayWidget::paintGL().

02796 {
02797 }

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(), swapBuffers()

Definition at line 3426 of file qgl.cpp.

References d.

03427 {
03428     Q_D(QGLWidget);
03429     d->autoSwap = on;
03430 }

bool QGLWidget::autoBufferSwap (  )  const [protected]

Returns true if the widget is doing automatic GL buffer swapping; otherwise returns false.

See also:
setAutoBufferSwap()

Definition at line 3432 of file qgl.cpp.

References d.

Referenced by QGLDrawable::swapBuffers().

03433 {
03434     Q_D(const QGLWidget);
03435     return d->autoSwap;
03436 }

void QGLWidget::paintEvent ( QPaintEvent event  )  [protected, virtual]

Handles paint events passed in the 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.

Reimplemented from QWidget.

Definition at line 2830 of file qgl.cpp.

References glDraw(), updateOverlayGL(), and QWidget::updatesEnabled().

02831 {
02832     if (updatesEnabled()) {
02833         glDraw();
02834         updateOverlayGL();
02835     }
02836 }

Here is the call graph for this function:

void QGLWidget::resizeEvent ( QResizeEvent event  )  [protected, virtual]

Handles resize events that are passed in the event parameter. Calls the virtual function resizeGL().

Reimplemented from QWidget.

Definition at line 1064 of file qgl_x11.cpp.

References d, glInit(), QWidget::height(), isValid(), makeCurrent(), QWidget::rect(), resizeGL(), and QWidget::width().

01065 {
01066     Q_D(QGLWidget);
01067     if (!isValid())
01068         return;
01069     makeCurrent();
01070     if (!d->glcx->initialized())
01071         glInit();
01072     glXWaitX();
01073     resizeGL(width(), height());
01074     if (d->olw)
01075         d->olw->setGeometry(rect());
01076 }

Here is the call graph for this function:

void QGLWidget::glInit (  )  [protected, virtual]

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

Definition at line 3014 of file qgl.cpp.

References d, initializeGL(), isValid(), and makeCurrent().

Referenced by glDraw(), and resizeEvent().

03015 {
03016     Q_D(QGLWidget);
03017     if (!isValid())
03018         return;
03019     makeCurrent();
03020     initializeGL();
03021     d->glcx->setInitialized(true);
03022 }

Here is the call graph for this function:

void QGLWidget::glDraw (  )  [protected, virtual]

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.

Definition at line 3032 of file qgl.cpp.

References d, doubleBuffer(), glInit(), isValid(), makeCurrent(), paintGL(), resizeGL(), and swapBuffers().

Referenced by paintEvent(), and updateGL().

03033 {
03034     Q_D(QGLWidget);
03035     if (!isValid())
03036         return;
03037     makeCurrent();
03038 #ifndef Q_WS_QWS
03039     if (d->glcx->deviceIsPixmap())
03040         glDrawBuffer(GL_FRONT);
03041 #endif
03042     if (!d->glcx->initialized()) {
03043         glInit();
03044         resizeGL(d->glcx->device()->width(), d->glcx->device()->height()); // New context needs this "resize"
03045     }
03046     paintGL();
03047     if (doubleBuffer()) {
03048         if (d->autoSwap)
03049             swapBuffers();
03050     } else {
03051         glFlush();
03052     }
03053 }

Here is the call graph for this function:

int QGLWidget::fontDisplayListBase ( const QFont font,
int  listBase = 2000 
) [protected]

Returns the value of the first display list that is generated for the characters in the given font. listBase indicates the base value used when generating the display lists for the font. The default value is 2000.

Definition at line 3212 of file qgl.cpp.

References base, d, QWidget::font(), QFont::key(), key, QFont::NoAntialias, QString::number(), QString::sprintf(), QFont::styleStrategy(), and QMap< Key, T >::value().

Referenced by renderText().

03213 {
03214     Q_D(QGLWidget);
03215     int base;
03216 
03217     if (!d->glcx) { // this can't happen unless we run out of mem
03218         return 0;
03219     }
03220 
03221     // always regenerate font disp. lists for pixmaps - hw accelerated
03222     // contexts can't handle this otherwise
03223     bool regenerate = d->glcx->deviceIsPixmap();
03224 #ifndef QT_NO_FONTCONFIG
03225     // font color needs to be part of the font cache key when using
03226     // antialiased fonts since one set of glyphs needs to be generated
03227     // for each font color
03228     QString color_key;
03229     if (font.styleStrategy() != QFont::NoAntialias) {
03230         GLfloat color[4];
03231         glGetFloatv(GL_CURRENT_COLOR, color);
03232         color_key.sprintf("%f_%f_%f",color[0], color[1], color[2]);
03233     }
03234     QString key = font.key() + color_key + QString::number((int) regenerate);
03235 #else
03236     QString key = font.key() + QString::number((int) regenerate);
03237 #endif
03238     if (!regenerate && (d->displayListCache.find(key) != d->displayListCache.end())) {
03239         base = d->displayListCache[key];
03240     } else {
03241         int maxBase = listBase - 256;
03242         QMap<QString,int>::ConstIterator it;
03243         for (it = d->displayListCache.constBegin(); it != d->displayListCache.constEnd(); ++it) {
03244             if (maxBase < it.value()) {
03245                 maxBase = it.value();
03246             }
03247         }
03248         maxBase += 256;
03249         d->glcx->generateFontDisplayLists(font, maxBase);
03250         d->displayListCache[key] = maxBase;
03251         base = maxBase;
03252     }
03253     return base;
03254 }

Here is the call graph for this function:


Friends And Related Function Documentation

friend class QGLDrawable [friend]

Definition at line 404 of file qgl.h.

friend class QGLPixelBuffer [friend]

Definition at line 405 of file qgl.h.

friend class QGLPixelBufferPrivate [friend]

Definition at line 406 of file qgl.h.

friend class QGLContext [friend]

Reimplemented from QWidget.

Definition at line 407 of file qgl.h.

Referenced by QGLWidget(), renderPixmap(), and setFormat().

friend class QGLOverlayWidget [friend]

Definition at line 408 of file qgl.h.

friend class QOpenGLPaintEngine [friend]

Definition at line 409 of file qgl.h.


The documentation for this class was generated from the following files:
Generated on Thu Mar 15 17:46:32 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1