Qt 2D drawing: other problems in drawing

1. Redraw event

All the drawing operations mentioned above are completed in the redrawing event processing function paintEvent(), which is a function defined in the QWidget class. A redraw event is used to redraw all or part of a widget. A redraw event can occur for any of the following reasons:

  • The repaint() function or update() function is called;
  • Hidden widgets are now redisplayed;
  • some other reasons.

Most widgets can simply redraw their entire interface, but some widgets that draw slowly need to be optimized to only draw the required area (you can use QPaintEvent::region() to get the area). Qt will also speed up drawing by merging multiple redrawing events into one event. When the update() function is called multiple times, or the window system sends multiple redrawing events, then Qt will merge these events into one event, and this event has the largest area that needs to be redrawn.

The update() function will not redraw immediately, it will not be performed until Qt returns to the main event loop, so calling the update() function multiple times will generally only cause one paintEvent() function call. Calling the repaint() function will immediately call the paintEvent() function to redraw the widget. The repaint() function is only used when the repaint operation must be performed immediately (such as in animation). update() allows Qt to optimize speed and reduce flickering, but the repaint() function does not support such optimization, so it is recommended to use the update() function as much as possible under normal circumstances. Also note that when the program starts running, the redraw event is automatically sent and the paintEvent() function is called. Also, do not call the update() or repaint() functions in the paintEvent() function.

When a repaint event occurs, the area to be updated is typically erased and then painted on the widget's background. The background of the component can generally be specified using setBackgroundRole(), and then use setAutoFillBackground(true) to enable the specified color. For example, to make the interface display a darker color, you can add the following code in the component's constructor:

this->setBackgroundRole(QPalette::Dark);
this->setAutoFillBackground(true);

Two, cut

QPainter can cut any drawing operation, you can cut a rectangle, a region or the content of a path, which can be achieved by using the setClipRect(), setClipRegion() and setClipPath() functions respectively. Clipping will be done in QPainter's logical coordinate system. The following code implements cutting text in a rectangle:

QPainter painter;
painter.setClipRect(10, 0, 20, 10);
painter.drawText(10, 10, tr("yafeilinux"));

3. Play gif animation

The QMovie class is a class that uses QlmageReader to play animations. It can play simple animations without sound. It supports gif and mng file formats. This class provides convenient functions to start, pause, and stop animations. You can refer to the help documentation of this class, or check out the Movie Player sample program, which is in the Widgets category.

4. Render the SVG file

Scalable Vector Graphics (Scalable Vector Graphics, SVG) is a language that uses XML to describe two-dimensional graphics and graphics applications. In Qt, you can use the QSvgWidget class to easily load an SVG file, and use the QSvgRenderer class to render the SVG file in QSvgWidget. The use of these two classes is very simple and will not be described here.

The article is transferred from the blog garden (fengMisaka): https://www.cnblogs.com/linuxAndMcu/p/11059663.html

The benefits of this article, the fee to receive Qt development learning materials package, technical video, content includes (C++ language foundation, Qt programming introduction, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓ See below

Guess you like

Origin blog.csdn.net/QtCompany/article/details/131838850