[Qt Development Process] 2D Drawing 2: Coordinate System

Overview

Qt’s coordinate system is controlled by the QPainter class. QPainter, together with the QPaintDevice and QPaintEngine classes, form the basis of Qt's painting system. QPainter is used to perform drawing operations. QPaintDevice is an abstraction of a two-dimensional space that can be drawn on using QPainter. QPaintEngine provides an interface for QPainter to draw on different types of devices.
The QPaintDevice class is the base class for objects that can be drawn: its drawing capabilities are inherited by the QWidget, QImage, QPixmap, QPicture, and QOpenGLPaintDevice classes. The origin of the drawing device's default coordinate system is in the upper left corner. The x value increases to the right and the y value increases downward. The default unit is one pixel on pixel-based devices and one point (1/72 inch) on printers.
The mapping of logical QPainter coordinates to physical QPaintDevice coordinates is handled by QPainter's transformation matrix, viewport and "window". By default, logical and physical coordinate systems are consistent. QPainter also supports coordinate transformations (such as rotation and scaling).

Logical representation

The size (width and height) of a figure always corresponds to its mathematical model, ignoring the width of the pen rendering it:
Insert image description here

Anti-aliased drawing

When is drawn, pixel rendering is controlled by the QPainter::Antialiasing rendering hint.
The RenderHint enumeration is used to specify flags for QPainter that may or may not be respected by any given engine. The QPainter::Antialiasing value indicates that the engine should try to eliminate the edges of graphics as much as possible, that is, by smoothing the edges using different color intensities.
When rendering with a one-pixel-wide pen, the pixel will be rendered to the right and below the mathematically defined point. For example:
Insert image description here
When rendering with a pen with an even number of pixels, the pixels will be rendered symmetrically around a mathematically defined point, whereas when rendering with a pen with an odd number of pixels, the extra pixels will be like A pixel is also presented to the right and below the mathematical point. As shown in the QRectF diagram below.
Insert image description here
Please note that due to historical reasons, the return value of the QRect::right() and QRect::bottom() functions deviates from the true lower right corner of the rectangle.
The right() function of QRect returns left() + width() - 1, and the bottom() function returns top() + height() - 1. The green dots in the lower right corner of the figure represent the return coordinates of these functions.
Therefore, you can use QRectF instead: the QRectF class uses floating point coordinates to define a rectangle in the plane to ensure accuracy (QRect uses integer coordinates), and QRectF::right() and QRectF:: The bottom() function returns the real lower right corner.
Alternatively, use QRect, apply x() + width() and y() + height() to find the bottom right corner, and avoid using the right() and bottom() functions.

If QPainter's anti-aliased rendering hint is set, pixels will be rendered symmetrically on either side of a mathematically defined point:
Insert image description here

Coordinate transformation

Guess you like

Origin blog.csdn.net/MrHHHHHH/article/details/134938871