

The example shows the same painting operations performed at the same time in a Widget and a GLWidget. Once started, the widgets should be updated at around 20 frames per second. Layout - >addWidget(openGLLabel, 1, 1) Ĭonnect(timer, & QTimer ::timeout, native, &Widget ::animate) Ĭonnect(timer, & QTimer ::timeout, openGL, &GLWidget ::animate) Ī timer with a 50 millisecond time out is constructed for animation purposes, and connected to the animate() slots of the Widget and GLWidget objects. Layout - >addWidget(nativeLabel, 1, 0)

OpenGLLabel - >setAlignment( Qt ::AlignHCenter) QLabel *openGLLabel = new QLabel(tr( "OpenGL"))


NativeLabel - >setAlignment( Qt ::AlignHCenter) QLabel *nativeLabel = new QLabel(tr( "Native")) GLWidget *openGL = new GLWidget( &helper, this) Widget *native = new Widget( &helper, this) SetWindowTitle(tr( "2D Painting on Native and OpenGL Widgets")) We do this because we want the same painting operations to be performed for both our QWidget subclass and the QOpenGLWidget subclass. In this example, the painting operations are performed by a helper class. To do this, we derive subclasses of QWidget and QOpenGLWidget, using a separate Helper class to perform the same painting operations for each, and lay them out in a top-level widget, itself provided a the Window class. To be able to compare the results of painting onto a QOpenGLWidget subclass with native drawing in a QWidget subclass, we want to show both kinds of widget side by side. The QWidget is shown with anti-aliasing enabled, and the QOpenGLWidget will also use anti-aliasing if the required extensions are supported by your system's OpenGL driver. In this example, we perform the same painting operations on a QWidget and a QOpenGLWidget. The only difference is that the painting operations will be accelerated in hardware if it is supported by your system's OpenGL drivers. Since QOpenGLWidget is a subclass of QWidget, it is possible to reimplement its paintEvent() and use QPainter to draw on the device, just as you would with a QWidget. The QPainter class is used to draw 2D graphics primitives onto paint devices provided by QPaintDevice subclasses, such as QWidget and QImage.
