The 2D Painting example shows how QPainter and QOpenGLWidget can be used together to display accelerated 2D graphics on supported hardware.
The QPainter class is used to draw 2D graphics primitives onto paint devices provided by QPaintDevice subclasses, such as QWidget and QImage.
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 only difference is that the painting operations will be accelerated in hardware if it is supported by your system's OpenGL drivers.
In this example, we perform the same painting operations on a QWidget and a QOpenGLWidget. 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.
I want the code/program for paint brush in Visual C using open gl.It must include square,rectangle,triangle,circle,pencil,eraser and it must have buttons. The quality and speed of rendering in the GLWidget depends on the level of support for multisampling and hardware acceleration that your system's OpenGL driver provides. If support for either of these is lacking, the driver may fall back on a software renderer that may trade quality for speed. Example project @ code.qt.io. Tux Paint is a free, award-winning drawing program for children ages 3 to 12. It combines an easy-to-use interface, fun sound effects, and an encouraging cartoon mascot who guides children as they use the program. Kids are presented with a blank canvas and a variety of drawing tools to help them be creative.
Overview
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. 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.
Helper Class Definition
In this example, the painting operations are performed by a helper class. We do this because we want the same painting operations to be performed for both our QWidget subclass and the QOpenGLWidget subclass.
The Helper
class is minimal:
Apart from the constructor, it only provides a paint()
function to paint using a painter supplied by one of our widget subclasses.
Helper Class Implementation
The constructor of the class sets up the resources it needs to paint content onto a widget:
The actual painting is performed in the paint()
function. This takes a QPainter that has already been set up to paint onto a paint device (either a QWidget or a QOpenGLWidget), a QPaintEvent that provides information about the region to be painted, and a measure of the elapsed time (in milliseconds) since the paint device was last updated.
We begin painting by filling in the region contained in the paint event before translating the origin of the coordinate system so that the rest of the painting operations will be displaced towards the center of the paint device.
We draw a spiral pattern of circles, using the elapsed time specified to animate them so that they appear to move outward and around the coordinate system's origin:
Since the coordinate system is rotated many times during this process, we save() the QPainter's state beforehand and restore() it afterwards.
We draw some text at the origin to complete the effect.
Widget Class Definition
The Widget
class provides a basic custom widget that we use to display the simple animation painted by the Helper
class.
Apart from the constructor, it only contains a paintEvent() function, that lets us draw customized content, and a slot that is used to animate its contents. One member variable keeps track of the Helper
that the widget uses to paint its contents, and the other records the elapsed time since it was last updated.
Widget Class Implementation
The constructor only initializes the member variables, storing the Helper
object supplied and calling the base class's constructor, and enforces a fixed size for the widget:
The animate()
slot is called whenever a timer, which we define later, times out:
Here, we determine the interval that has elapsed since the timer last timed out, and we add it to any existing value before repainting the widget. Since the animation used in the Helper
class loops every second, we can use the modulo operator to ensure that the elapsed
variable is always less than 1000.
Since the Helper
class does all of the actual painting, we only have to implement a paint event that sets up a QPainter for the widget and calls the helper's paint()
function:
GLWidget Class Definition
The GLWidget
class definition is basically the same as the Widget
class except that it is derived from QOpenGLWidget.
Again, the member variables record the Helper
used to paint the widget and the elapsed time since the previous update.
GLWidget Class Implementation
The constructor differs a little from the Widget
class's constructor:
As well as initializing the elapsed
member variable and storing the Helper
object used to paint the widget, the base class's constructor is called with the format that specifies the QGL::SampleBuffers flag. This enables anti-aliasing if it is supported by your system's OpenGL driver.
The animate()
slot is exactly the same as that provided by the Widget
class:
The paintEvent()
is almost the same as that found in the Widget
class:
Since anti-aliasing will be enabled if available, we only need to set up a QPainter on the widget and call the helper's paint()
function to display the widget's contents.
Window Class Definition
The Window
class has a basic, minimal definition:
It contains a single Helper
object that will be shared between all widgets.
Window Class Implementation
The constructor does all the work, creating a widget of each type and inserting them with labels into a layout:
A timer with a 50 millisecond time out is constructed for animation purposes, and connected to the animate()
slots of the Widget
and GLWidget
objects. Once started, the widgets should be updated at around 20 frames per second.
Running the Example
The example shows the same painting operations performed at the same time in a Widget
and a GLWidget
. The quality and speed of rendering in the GLWidget
depends on the level of support for multisampling and hardware acceleration that your system's OpenGL driver provides. If support for either of these is lacking, the driver may fall back on a software renderer that may trade quality for speed.
© 2021 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.
In this lesson I shall introduce several functions and show you actual OpenGLrendering in a program. Prior to showing you the code, however, I want to goover a few things with you. This will give you a better understanding of whatis going on when you do see the code, so you don't stare at the screenwondering what you're looking at. So, on with the show.Opengl Paint Program Source Code Download
Transformations in OpenGL rely on the matrix for all mathematical computations. No, not the movie. Concentrate grasshopper. OpenGL has what is known as a matrix stack, which comes in handy for constructing models composed of many simple objects.
The modelview matrix defines the coordinate system that is being used to place and orient objects. It is a 4x4 matrix that is multiplied by vertices and transformations to create a new matrix that reflects the result of any transformations that have been applied to the vertices. When we want to modify the modelview matrix we use the command glMatrixMode(). We define this asBefore you call any transformation commands you MUST specify whether you want to modify the modelview matrix or the projection matrix. The argument for modifying the modelview matrix is GL_MODELVIEW. So the complete line would appear as:Now we will look at translation. Translation allows you to move an object from one location to another within a 3D environment. The functions for this in OpenGL are glTranslatef() and glTranslated(). Here are their descriptions:Note that you must pass float types to glTranslatef() and double types to glTranslated(). X, Y, and Z represent the amount of translation on that axis.
Opengl Paint Program Source Code
Rotation in OpenGL is accomplished through the glRotate*() function, which is defined asNow let's take a look at these, and a few others, functions mentioned in a program. The following code is taken from OpenGL Game Programming and is commented by myself. If you have any problems building and using this code, feel free to contact me.That's a lot of code! Spend some time studying the example, practice a bit, and then we will proceed to lesson 7, where more of this code will be explained. Also, in lesson 7 we will cover Projections.Happy Coding!!
Previous: WGL functions
Next: Projections in OpenGL
Back to OpenGL tutorial index