Qt drawing events

Qt drawing is done in drawing events. The entire drawing system is based on QPainter, QPainterDeviceand QPaintEnginethree categories.

QPainter is used to draw, QPainterDevice is a drawing device, QPainter will draw the picture on QPainterDevice.

Qt drawing system

It's just that we don't see the existence of QPaintEngine. Use QPainterthe QPainterDevicedraw on, among them the use of QPaintEnginecommunication (that is, the translation QPainterof the instructions).

Of course, drawing also needs to be done in the drawing event function. We rewrite the drawing function of the base class. As follows:

void MainWindow::paintEvent(QPaintEvent *event)
{
    paint = new QPainter(this);
    paint->setPen(QColor(0,0,255));
    paint->drawLine(30,30,300,300);
    paint->drawRect(100,100,100,100);
    paint->setPen((QColor(255,0,0)));
    paint->drawEllipse(QPoint(300,300),50,50);
}

OpenGL is a state machine. The so-called state machine means that OpenGL saves only various states. For example, if you set the brush color to red, then unless you reset another color, its color will always be red. QPainterThe same is true, its state will not recover by itself unless you use various setting functions.

The results are as follows:

Note that at this time, if we change the size of the window, we will see the following output information. This means that paintEvent (), as a redraw function, will be automatically called by Qt when it needs to be redrawn. "Need to redraw" may happen in many places, such as when the component has just been created, it needs to be redrawn; when the component is maximized and minimized, it needs to be redrawn; when the component changes from occlusion to full display, it also needs to wait.

 

Reference blog: https://www.devbean.net/2012/10/qt-study-road-2-paint-sys/

 

 

Published 242 original articles · Like 180 · Visits 160,000+

Guess you like

Origin blog.csdn.net/zy010101/article/details/105437222