Overview
Qt
's drawing system can use the same API
for drawing on screen and print devices, and is mainly based on QPainter
, QPaintDevice
andQPaintEngine
classes.
QPainter
is used to perform drawing operations, QPaintDevice
is an abstraction of a two-dimensional space that can be drawn on using QPainter
, a> and are hidden from application programmers unless they create their own device types . and classes are used internally by interfaces for drawing on different types of devices. The QPaintEngine
ProvidesQPainter
QPaintEngine
QPainter
QPaintDevice
The main benefit of this approach is that all drawing follows the same drawing style, making it easy to add support for new features and provide default implementations for unsupported features.
The following classes provide support forQPainter
drawing:
kind | describe |
---|---|
QBitmap | Monochrome (1-bit depth) pixmap |
QBrush | Defines the fill mode of shapes drawn by QPainter |
QColor | Color based on RGB, HSV or CMYK values |
QColormap | Map device-independent QColors to device-dependent pixel values |
QConicalGradient | Used with QBrush to specify a tapered gradient brush |
QFont | Specify the font used to draw text |
QFontMetrics | Font metric information |
QFontMetricsF | Font metric information |
QGenericMatrix | Template class, representing an NxM transformation matrix with N columns and M rows |
QGradient | Use with QBrush to specify gradient fills |
QIcon | Scalable icons in different modes and states |
QIconEngine | Abstract base class for the QIcon renderer |
QImage | Hardware-independent image representation that allows direct access to pixel data and can be used as a painting device |
QImageReader | Format-independent interface for reading images from files or other devices |
QImageWriter | Format-independent interface for writing images to files or other devices |
QLine | 2D vectors with integer precision |
QLineF | 2D vectors use floating point precision |
QLinearGradient | Used in conjunction with QBrush to specify a linear gradient brush |
QMargins | Define the four sides of the rectangle |
QMarginsF | Define the four sides of the rectangle |
QPagedPaintDevice | Represents a drawing device that supports multiple pages |
QPaintDevice | Base class for objects that can be drawn with QPainter |
QPaintEngine | Abstract definition of how QPainter draws to a given device on a given platform |
QPainter | Perform low-level drawing on widgets and other drawing devices |
QPainterPath | Container for drawing operations, enabling graphics shapes to be constructed and reused |
QPainterPathStroker | Used to generate a fillable outline for a given painter path |
QPdfWriter | Class that generates PDFs that can be used as drawing devices |
QPen | Defines how QPainter should draw the lines and outlines of a shape |
QPixmap | Off-screen image representation that can be used as a drawing device |
QPlatformFontDatabase | Can customize how fonts are discovered and presented |
QPoint | Define a point on the plane using integer precision |
QPointF | Define a point on the plane using floating point precision |
QPolygon | Vector points use integer precision |
QPolygonF | Vector points use floating point precision |
QRadialGradient | Used in conjunction with QBrush to specify a radial gradient brush |
QRect | Defines a rectangle on a plane using integer precision |
QRectF | Define a rectangle in a plane using floating point precision |
QRegion | Specify the plotter's clipping area |
QSize | Define the size of a 2D object using integer point precision |
QSizeF | Define the size of a 2D object using floating point precision |
QStylePainter | Convenience class for drawing QStyle elements within widgets |
QSupportedWritingSystems | Used when registering fonts using the internal Qt font database |
QSvgGenerator | Drawing device for creating SVG drawings |
QSvgRenderer | Used to draw the contents of an SVG file onto a drawing device |
QSvgWidget | Widget for displaying the contents of a Scalable Vector Graphics (SVG) file |
QTransform | 2D transformation of specified coordinate system |
QVector2D | Represents a vector or vertex in two-dimensional space |
Create a painting device
QPaintDevice类
is the base class for drawable objects, that is, QPainter
can be drawn on any QPaintDevice子类
. The drawing function of QPaintDevice
is composed of QWidget
, QImage
, QPixmap
, QPicture
, QPrinter
and QOpenGLPaintDevice
.
Widget
QWidget
The class is the base class for user interface elements in theQt Widgets
module. It receives mouse, keyboard, and other events from the window system and draws its own representation on the screen.
Image
QImage
The class provides a hardware-independent image representation that is designed and optimized for I/O and direct pixel access and manipulation. QImage
Supports a variety of image formats, including monochrome, 8-bit, 32-bit and alpha blended images.
One advantage of using QImage
as a drawing device is that it guarantees pixel accuracy for any drawing operation in a platform-independent manner. Another benefit is that drawing can be performed in another thread besides the current GUI thread.
Pixmap
QPixmap
The class is an off-screen image representation that is designed and optimized to display images on the screen. Unlike QImage
, the pixel data in the pixmap is internal and managed by the underlying window system, that is, the pixels can only be passed through the QPainter
function or < a i=3> is converted to for access. To optimize plots using , provides , which can be used to store generated Expensive temporary pixmaps without using more storage than cache limits. also provides convenience classes, which inherit . Guaranteed monochrome (1-bit depth) pixmap, mainly used for creating custom and objects, constructs a> object. QPixmap
QImage
QPixmap
Qt
QPixmapCache类
Qt
QBitmap
QPixmap
QBitmap
QCursor
QBrush
QRegion
OpenGL
drawing device
As mentioned earlier, Qt provides classes that make it easy to use OpenGL
in Qt applications. For example, QOpenGLPaintDevice
enablesOpenGL API
the Picture
QPicture
class which is a record and playback QPainter
command for drawing equipment. Picture serializes painter commands to IO devices in a platform-independent format. QPicture
is also resolution independent, that is, a QPicture
can be displayed on different devices (such as svg, pdf, ps, printer and screen), and it looks like the same.
Qt provides QPicture::load()
and QPicture::save()
functions as well as stream operators for loading and saving images.
Custom Backends
Support for the new backend can be implemented by derivingQPaintDevice
the class and reimplementing the virtualQPaintDevice:: paintenengine()
functions to tell class. QPainter
qpaintenengine
draw
QPainter
提供了高度优化的功能,以完成大多数绘图GUI程序所需的功能。它可以绘制从简单的图形基元(由QPoint, QLine, QRect, QRegion和QPolygon类表示)到复杂形状(如矢量路径)的一切。在Qt矢量路径是由QPainterPath类
表示的。QPainterPath
为绘制操作提供了一个容器,使图形形状能够被构造和重用。
QPainterPath
painter
路径是由线条和曲线组成的对象。例如,矩形由直线组成,椭圆由曲线组成。
与普通绘图操作相比,绘制路径的主要优势在于,复杂的形状只需要创建一次;然后只需调用QPainter::drawPath()
函数就可以多次绘制它们。
QPainterPath
对象可用于填充、勾勒和裁剪。要为给定的painter
路径生成可填充的轮廓,可以使用QPainterPathStroker
类。
线条和轮廓是使用QPen类
绘制的。pen
由其样式(即线条类型)、宽度、笔刷、如何绘制端点(cap-style)以及如何绘制两条连接线之间的连接(join-style)来定义。pen
的笔刷是一个QBrush
对象,用来填充pen
生成的笔画,也就是说,QBrush类
定义了填充模式。
QPainter
还可以绘制对齐的文本和像素图。
绘制文本时,使用QFont类
指定字体。Qt将使用具有指定属性的字体,或者如果没有匹配的字体存在,Qt将使用最接近匹配的已安装字体。实际使用的字体属性可以使用QFontInfo
类检索。此外,QFontMetrics类
提供字体度量,QFontDatabase类
提供有关底层窗口系统中可用字体的信息。
通常,QPainter
在“自然”坐标系中绘制,但它能够使用QTransform类
执行视图和世界坐标系转换。
反锯齿绘制
绘制时,像素渲染由QPainter::Antialiasing
渲染提示控制。QPainter::RenderHint
枚举用于指定QPainter
的标志,这些标志可能被任何给定的引擎尊重,也可能不被尊重。
QPainter::Antialiasing
值表示引擎应该尽可能地消除原语的边缘,即通过使用不同的颜色强度平滑边缘。
基本图形绘制、填充
绘制线
void MainWindow::paintEvent(QPaintEvent *event)
{
QPainter p(this);
p.drawLine(50, 50, 100, 150);
}
或者,创建时不指定绘制设备,在begin()函数中设置,end时结束绘制。
QPainter p;
p.begin(this);
p.drawLine(50, 50, 100, 150);
p.end();
绘制圆弧
绘制由给定矩形、startAngle和spanAngle定义的圆弧。
起始角和伸缩角必须以1/16度指定,即一个完整的圆等于5760(16 * 360)。角度的正值表示逆时针方向,负值表示顺时针方向。零度在3点钟方位。
QPainter p;
p.begin(this);
QRectF rectangle(10.0, 20.0, 80.0, 60.0);
int startAngle = 30 * 16;
int spanAngle = 120 * 16;
p.drawArc(rectangle, startAngle, spanAngle);
p.end();
绘制弦
绘制由给定矩形、startAngle和spanAngle定义的弦。弦被当前的笔刷()填充。
起始角和伸缩角必须以1/16度指定,即一个完整的圆等于5760(16 * 360)。角度的正值表示逆时针方向,负值表示顺时针方向。零度在3点钟方位。
QPainter p;
p.begin(this);
QRectF rectangle(10.0, 20.0, 80.0, 60.0);
int startAngle = 30 * 16;
int spanAngle = 120 * 16;
p.drawChord(rectangle, startAngle, spanAngle);
p.end();
绘制凸多边形
QPainter p;
p.begin(this);
static const QPointF points[4] = {
QPointF(40.0, 110.0),
QPointF(50.0, 40.0),
QPointF(110.0, 70.0),
QPointF(120.0, 100.0)
};
p.drawConvexPolygon(points, 4);
p.end();
绘制椭圆
QPainter p;
p.begin(this);
QRectF rectangle(100.0, 50.0, 80.0, 60.0);
p.drawEllipse(rectangle);
p.end();
如果宽,高一样,就是圆。
绘制扇形
QPainter p;
p.begin(this);
QRectF rectangle(100.0, 50.0, 160.0, 120.0);
int startAngle = 30 * 16;
int spanAngle = 120 * 16;
p.drawPie(rectangle, startAngle, spanAngle);
p.end();
填充
除此之外,还可以使用画笔及画刷进行绘制和填充
绘制多边形
QPainter p;
p.begin(this);
QPen pen(QBrush(Qt::gray), 2, Qt::DashLine, Qt::FlatCap, Qt::RoundJoin);
p.setPen(pen);
p.setBrush(Qt::green);
static const QPointF points[4] = {
QPointF(40.0, 110.0),
QPointF(50.0, 40.0),
QPointF(110.0, 70.0),
QPointF(120.0, 100.0)
};
p.drawPolygon(points, 4);
p.end();
绘制圆角矩形
QPainter p;
p.begin(this);
QPen pen(QBrush(Qt::black), 2, Qt::SolidLine, Qt::RoundCap, Qt::BevelJoin);
p.setPen(pen);
p.setBrush(Qt::lightGray);
QRectF rectangle(10.0, 20.0, 80.0, 60.0);
p.drawRoundedRect(rectangle, 20.0, 15.0);
p.end();
渐变填充
使用画刷还可以进行渐变填充。
渐变有三种:
- 线性渐变:QLinearGradient
- 辐射渐变:QRadialGradient
- 锥形渐变:QConicalGradient
绘制矩形
QPainter p;
p.begin(this);
QPen pen(QBrush(Qt::black), 2, Qt::SolidLine, Qt::RoundCap, Qt::BevelJoin);
p.setPen(pen);
QLinearGradient linearGrad(QPointF(100, 100), QPointF(200, 200));
linearGrad.setColorAt(0, Qt::black);
linearGrad.setColorAt(1, Qt::white);
p.setBrush(linearGrad);
QRectF rectangle(100.0, 100.0, 100.0, 100.0);
p.drawRoundedRect(rectangle, 20.0, 15.0);
p.end();
结论
人生就是这样,有欢笑也有泪水。一部分人主要负责欢笑,另一部分人主要负责泪水
。