返回介绍

QGLWidget Class

发布于 2019-10-04 15:00:33 字数 19826 浏览 1140 评论 0 收藏 0

The QGLWidget class is a widget for rendering OpenGL graphics. More...

#include <qgl.h>

Inherits QWidget and QGL.

List of all member functions.

Public Members

  • QGLWidget ( QWidget*parent = 0, constchar*name = 0, constQGLWidget*shareWidget = 0, WFlagsf = 0 )
  • QGLWidget ( constQGLFormat&format, QWidget*parent = 0, constchar*name = 0, constQGLWidget*shareWidget = 0, WFlagsf = 0 )
  • ~QGLWidget ()
  • void qglColor ( constQColor&c ) const
  • void qglClearColor ( constQColor&c ) const
  • bool isValid () const
  • bool isSharing () const
  • virtual void makeCurrent ()
  • bool doubleBuffer () const
  • virtual void swapBuffers ()
  • QGLFormat format () const
  • const QGLContext * context () const
  • virtual QPixmap renderPixmap ( intw = 0, inth = 0, booluseContext = FALSE )
  • virtual QImage grabFrameBuffer ( boolwithAlpha = FALSE )
  • virtual void makeOverlayCurrent ()
  • const QGLContext * overlayContext () const
  • const QGLColormap & colormap () const
  • void setColormap ( constQGLColormap&cmap )

Public Slots

  • virtual void updateGL ()
  • virtual void updateOverlayGL ()

Static Public Members

  • QImage convertToGLFormat ( constQImage&img )

Protected Members

  • virtual void initializeGL ()
  • virtual void resizeGL ( intwidth, intheight )
  • virtual void paintGL ()
  • virtual void initializeOverlayGL ()
  • virtual void resizeOverlayGL ( intwidth, intheight )
  • virtual void paintOverlayGL ()
  • void setAutoBufferSwap ( boolon )
  • bool autoBufferSwap () const
  • virtual void paintEvent ( QPaintEvent * )
  • virtual void resizeEvent ( QResizeEvent * )
  • virtual void glInit ()
  • virtual void glDraw ()

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 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:

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

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.

* 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, constchar*name = 0, constQGLWidget*shareWidget = 0, WFlagsf = 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.

See also QGLFormat::defaultFormat().

QGLWidget::QGLWidget ( constQGLFormat&format, QWidget*parent = 0, constchar*name = 0, constQGLWidget*shareWidget = 0, WFlagsf = 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.

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.

bool QGLWidget::autoBufferSwap () const [protected]

Returns TRUE if the widget is doing automatic GL buffer swapping; otherwise returns FALSE.

See also setAutoBufferSwap().

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

constQGLContext* 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.

QImage QGLWidget::convertToGLFormat ( constQImage&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. The following few lines are from the texture example. Most of the code is irrelevant, so we just quote the few lines we want:

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

Another function in the same example uses tex1 with OpenGL.

Example: opengl/texture/gltexobj.cpp.

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.

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 ( boolwithAlpha = FALSE ) [virtual]

Returns an image of the frame buffer. If withAlpha is TRUE the alpha channel is included.

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.

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.

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

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.

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.

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

constQGLContext* 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. 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.

void QGLWidget::qglClearColor ( constQColor&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 ( constQColor&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 ( intw = 0, inth = 0, booluseContext = FALSE ) [virtual]

Renders the current scene on a pixmap and returns the pixmap.

You may 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.

void QGLWidget::resizeEvent ( QResizeEvent* ) [virtual protected]

Handles resize events. Calls the virtual function resizeGL().

Reimplemented from QWidget.

void QGLWidget::resizeGL ( intwidth, intheight ) [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 ( intwidth, intheight ) [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. 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.

void QGLWidget::setAutoBufferSwap ( boolon ) [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 time the paintGL() function has been called.

The buffer auto-swapping is on by default.

See also autoBufferSwap(), doubleBuffer() and swapBuffers().

void QGLWidget::setColormap ( constQGLColormap&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]

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() and QGLFormat::setDoubleBuffer().

void QGLWidget::updateGL () [virtual slot]

Updates the widget by calling glDraw().

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文