Qt's drawing system allows you to draw on screen and print devices using the same API. The entire plotting system is based on three classes: qpainter, qpainterdevice, and qpaintengine. Qpainter is used to perform the painting operation. qpaintdevice is an abstraction of a two-dimensional space, which can be drawn by qpainter; qpaintengine provides a unified interface for painter painting on different devices. The qpaintengine class is used between qpainter and qpaintdevice and is usually transparent to developers. Unless you need to customize a device, you must redefine qpaintengine. The hierarchical structure between the three classes is provided (from the qt api documentation ): the main benefit of this implementation is that all plotting follows the same drawing process, you can easily add new features or add a default Implementation Method for unsupported features. In addition, QT provides an independent qtopengl module that allows you to use OpenGL in the QT application Program . This module provides an OpenGL module that can be used like other QT components. The difference is that it uses OpenGL as the display technology and OpenGL functions for rendering. We will introduce this component later. Based on the previous introduction, we know that the QT plotting system is actually to say that qpainter is used to draw on qpainterdevice, and qpaintengine is used to communicate between them. Now let's take a look at how to use qpainter. First, we define a component, which is similar to the previous definition:
Class Paintedwidget: Public Qwidget
{
Public :
Paintedwidget ();
Protected :
Void Paintevent (qpaintevent * Event );
};
Here we only define one constructor and redefine the paintevent () function. It can be seen from the name that this is actually an Event Callback Function. Note that, in general, the event functions of QT are all protected. Therefore, if you want to rewrite the event, you need to inherit the class. As for the event-related things, we have already described them in detail in the previous section, so we will not repeat them here. The constructor mainly defines the size and the like. Here we will not describe it in detail: paintedwidget ()
{
Resize (800,600 );
Setwindowtitle (TR ( "Paint Demo" ));
} We are concerned about the implementation of the paintevent () function: Void Paintedwidget: paintevent (qpaintevent * Event )
{
Qpainter painter ( This );
Painter. drawline (80,100,650,500 );
Painter. setpen (QT: Red );
Painter. drawrect (10, 10,100,400 );
Painter. setpen (qpen (QT: Green, 5 ));
Painter. setbrush (QT: Blue );
Painter. drawellipse (50,150,400,200 );
} To run the program, the main () function is shown below: Int Main ( Int Argc, Char * Argv [])
{
Qapplication app (argc, argv );
Paintedwidget W;
W. Show ();
Return App.exe C ();
} The running result is as follows: first, we declare a qpainter object. Note: We have created an object in the stack space of this function, so we do not need to delete it. Qpainter receives a qpaintdevice * type parameter. Qpaintdevice has many sub-classes, such as qimage and qwidget. Remember, qpaintdevice can be understood as where to draw, and now we want to draw on this widget, so the input is the this pointer. Qpainter has many functions starting with draw for drawing various graphics, such as drawline, drawrect, and drawellipse. For specific parameters, see the API documentation. An example of the qpainter draw function is provided. The figure below is from C ++ GUI programming with qt4, 2nd edition. now, we will go here today and continue to explain this paintevent () function in the following chapter.