The qpainter class is used for Qt plotting. The drawing device is usually the main component, either a qlabel component or a qtextedit component, some algorithms can be used to draw beautiful images or the images we need.
You need to define a qpainter class object before painting. You can draw optional items such as qpen and qbrush ). You can also specify the font (qfont class) when writing text using qpen)
See the following code:
Qpanter painter;
Qpen pen;
Pen. setcolor (qcolor (, 0); // set the paint brush to red.
Painter. setpen (PEN); // select a paint brush
Painter. drawline (100,100, 100,100); // draw a line with the red brush, starting point (), ending point)
Painter. End (); // end the painting. All resources used for plotting are released. Although you do not need to call end () Sometimes, The Destructor will execute it
Other functions are similar. Refer to the help document, which contains the meanings and parameter meanings of each method.
Here we will talk about how to update the drawing view when a drawing occurs.
During plotting, The paintevent (qpaintevent *) method of the qwidget class needs to be reloaded. The function prototype is
Void qwidget: paintevent (qpaintevent *)[Virtual protection] This function is a protected virtual function. It is a function used to draw events and can be re-implemented in a derived class to accept painting events.
Therefore, you must declare the paintevent function in the class and then draw the image in the function definition. Note that qpaintevent * event must be written in the parameter.
After figuring out this, we will know that drawing a graph is done in the paintevent function. That is to say, you do not have to write all the drawn code in the paintevent function (of course, many tutorials are written in this way, and the code is long and it is not recommended to do so when there is a certain framework). It can be implemented in external functions and called in paintevent.
Then we need to figure out when to draw the problem. In fact, when using class objects, if the class overwrites the paintevent event, the object will call a paintevent function, this painting event is called when an object is defined. What should I do if I need to re-paint or refresh the image? This is done using the repaint () or update () functions, both of which are member functions of the qwidget class, the derived class can directly call these two functions to erase and draw the window (note that the window is erased first and then drawn), that is, the paintevent event is called through repaint () or updata. If you need to re-draw immediately, we recommend that you use repaint (), but repiant () has a defect because if repaint () after the paintevent function has a repaint () in the function to be called, it will be in an infinite loop, and updata () will not, because updata () A mechanism allows a paintevent event to be called only once. In most cases, update () is better because it allows QT to optimize the speed and prevent flickering.
Note that during the painting, QT has automatically implemented dual buffering for the paintevent event (the X11 system needs to manually enable dual buffering ), that is to say, the method of double buffering is used for plotting. This is different from that of MFC. In MFC, we need to use double buffering. Otherwise, flashes may occur in some applications.