QPainter Class
QPainter 类可在部件和其他绘画设备上执行低级绘画。更多
Header: | #include <QPainter> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS Gui) target_link_libraries(mytarget PRIVATE Qt6::Gui) |
qmake: | QT += gui |
继承于 |
- 所有成员(包括继承成员)的列表
- QPainter 属于绘画类。
注意:该类中的所有函数都是可重入的。
公共类型
class | PixmapFragment |
enum | CompositionMode { CompositionMode_SourceOver, CompositionMode_DestinationOver, CompositionMode_Clear, CompositionMode_Source, CompositionMode_Destination, …, RasterOp_SourceOrNotDestination } |
enum | PixmapFragmentHint { OpaqueHint } |
flags | PixmapFragmentHints |
enum | RenderHint { Antialiasing, TextAntialiasing, SmoothPixmapTransform, VerticalSubpixelPositioning, LosslessImageRendering, NonCosmeticBrushPatterns } |
flags | RenderHints |
公共函数
QPainter() | |
QPainter(QPaintDevice*device) | |
~QPainter() | |
const QBrush & | background()常量 |
Qt::BGMode | backgroundMode()常量 |
bool | begin(QPaintDevice*device) |
void | beginNativePainting() |
QRectF | boundingRect(const QRectF&rectangle, intflags, const QString&text) |
QRect | boundingRect(const QRect&rectangle, intflags, const QString&text) |
QRectF | boundingRect(const QRectF&rectangle, const QString&text, const QTextOption&option= QTextOption()) |
QRect | boundingRect(intx, inty, intw, inth, intflags, const QString&text) |
const QBrush & | brush() const |
QPoint | brushOrigin常数 |
QRectF | clipBoundingRect() const |
QPainterPath | clipPath() const |
QRegion | clipRegion() const |
QTransform | combinedTransform()常量 |
QPainter::CompositionMode | compositionMode()常量 |
QPaintDevice * QPaintDevice * QPaintDevice * QPaintDevice * | device()常量 |
const QTransform & | deviceTransform() const |
void | drawArc(const QRectF&rectangle, intstartAngle, intspanAngle) |
无效 | drawArc(const QRect&rectangle, intstartAngle, intspanAngle) |
void | drawArc(intx, inty, intwidth, intheight, intstartAngle, intspanAngle) |
void | drawChord(const QRectF&rectangle, intstartAngle, intspanAngle) |
void | drawChord(const QRect&rectangle, intstartAngle, intspanAngle) |
void | drawChord(intx, inty, intwidth, intheight, intstartAngle, intspanAngle) |
void | drawConvexPolygon(const QPointF*points, intpointCount) |
void | drawConvexPolygon(const QPolygon&polygon) |
void | drawConvexPolygon(const QPolygonF&polygon) |
void | drawConvexPolygon(const QPoint*points, intpointCount) |
void | drawEllipse(const QRectF&rectangle) |
void | drawEllipse(const QRect&rectangle) |
无效 | drawEllipse(const QPoint¢er, intrx, intry) |
无效 | drawEllipse(const QPointF¢er, qrealrx, qrealry) |
无效 | drawEllipse(intx, inty, intwidth, intheight) |
无效 | drawGlyphRun(const QPointF&position, const QGlyphRun&glyphs) |
void | drawImage(const QRectF&target, const QImage&image, const QRectF&source, Qt::ImageConversionFlagsflags= Qt::AutoColor) |
void | drawImage(const QPoint&point, const QImage&image) |
void | drawImage(const QPointF&point, const QImage&image) |
void | drawImage(const QRect&rectangle, const QImage&image) |
void | drawImage(const QRectF&rectangle, const QImage&image) |
void | drawImage(const QPoint&point, const QImage&image, const QRect&source, Qt::ImageConversionFlagsflags= Qt::AutoColor) |
void | drawImage(const QPointF&point, const QImage&image, const QRectF&source, Qt::ImageConversionFlagsflags= Qt::AutoColor) |
void | drawImage(const QRect&target, const QImage&image, const QRect&source, Qt::ImageConversionFlagsflags= Qt::AutoColor) |
void | drawImage(intx, inty, const QImage&image, intsx= 0, intsy= 0, intsw= -1, intsh= -1, Qt::ImageConversionFlags flags= Qt::AutoColor) |
void | drawLine(const QLineF&line) |
void | drawLine(const QLine&line) |
void | drawLine(const QPoint&p1, const QPoint&p2) |
void | drawLine(const QPointF&p1, const QPointF&p2) |
void | drawLine(intx1, inty1, intx2, inty2) |
void | drawLines(const QLineF*lines, intlineCount) |
void | drawLines(const QList<QLine>&lines) |
void | drawLines(const QList<QLineF>&lines) |
void | drawLines(const QList<QPoint>&pointPairs) |
void | drawLines(const QList<QPointF>&pointPairs) |
void | drawLines(const QLine*lines, intlineCount) |
void | drawLines(const QPoint*pointPairs, intlineCount) |
void | drawLines(const QPointF*pointPairs, intlineCount) |
void | drawPath(const QPainterPath&path) |
void | drawPicture(const QPointF&point, const QPicture&picture) |
void | drawPicture(const QPoint&point, const QPicture&picture) |
void | drawPicture(intx, inty, const QPicture&picture) |
void | drawPie(const QRectF&rectangle, intstartAngle, intspanAngle) |
void | drawPie(const QRect&rectangle, intstartAngle, intspanAngle) |
void | drawPie(intx, inty, intwidth, intheight, intstartAngle, intspanAngle) |
void | drawPixmap(const QRectF&target, const QPixmap&pixmap, const QRectF&source) |
void | drawPixmap(const QPoint&point, const QPixmap&pixmap) |
void | drawPixmap(const QPointF&point, const QPixmap&pixmap) |
void | drawPixmap(const QRect&rectangle, const QPixmap&pixmap) |
void | drawPixmap(const QPoint&point, const QPixmap&pixmap, const QRect&source) |
void | drawPixmap(const QPointF&point, const QPixmap&pixmap, const QRectF&source) |
void | drawPixmap(const QRect&target, const QPixmap&pixmap, const QRect&source) |
void | drawPixmap(intx, inty, const QPixmap&pixmap) |
void | drawPixmap(intx, inty, intwidth, intheight, const QPixmap&pixmap) |
void | drawPixmap(intx, inty, const QPixmap&pixmap, intsx, intsy, intsw, intsh) |
void | drawPixmap(intx, inty, intw, inth, const QPixmap&pixmap, intsx, intsy, intsw, intsh) |
void | drawPixmapFragments(const QPainter::PixmapFragment*fragments, intfragmentCount, const QPixmap&pixmap, QPainter::PixmapFragmentHintshints= PixmapFragmentHints()) |
void | drawPoint(const QPointF&position) |
void | drawPoint(const QPoint&position) |
void | drawPoint(intx, inty) |
void | drawPoints(const QPointF*points, intpointCount) |
void | drawPoints(const QPolygon&points) |
void | drawPoints(const QPolygonF&points) |
无效 | drawPoints(const QPoint*points, intpointCount) |
void | drawPolygon(const QPointF*points, intpointCount, Qt::FillRulefillRule= Qt::OddEvenFill) |
void | drawPolygon(const QPolygon&points, Qt::FillRulefillRule= Qt::OddEvenFill) |
void | drawPolygon(const QPolygonF&points, Qt::FillRulefillRule= Qt::OddEvenFill) |
void | drawPolygon(const QPoint*points, intpointCount, Qt::FillRulefillRule= Qt::OddEvenFill) |
void | drawPolyline(const QPointF*points, intpointCount) |
void | drawPolyline(const QPolygon&points) |
void | drawPolyline(const QPolygonF&points) |
无效 | drawPolyline(const QPoint*points, intpointCount) |
void | drawRect(const QRectF&rectangle) |
void | drawRect(const QRect&rectangle) |
无效 | drawRect(intx, inty, intwidth, intheight) |
无效 | drawRects(const QRectF*rectangles, intrectCount) |
void | drawRects(const QList<QRect>&rectangles) |
void | drawRects(const QList<QRectF>&rectangles) |
void | drawRects(const QRect*rectangles, intrectCount) |
无效 | drawRoundedRect(const QRectF&rect, qrealxRadius, qrealyRadius, Qt::SizeModemode= Qt::AbsoluteSize) |
void | drawRoundedRect(const QRect&rect, qrealxRadius, qrealyRadius, Qt::SizeModemode= Qt::AbsoluteSize) |
void | drawRoundedRect(intx, inty, intw, inth, qrealxRadius, qrealyRadius, Qt::SizeModemode= Qt::AbsoluteSize) |
void | drawStaticText(const QPointF&topLeftPosition, const QStaticText&staticText) |
void | drawStaticText(const QPoint&topLeftPosition, const QStaticText&staticText) |
void | drawStaticText(intleft, inttop, const QStaticText&staticText) |
void | drawText(const QPointF&position, const QString&text) |
void | drawText(const QPoint&position, const QString&text) |
void | drawText(const QRectF&rectangle, const QString&text, const QTextOption&option= QTextOption()) |
void | drawText(intx, inty, const QString&text) |
void | drawText(const QRect&rectangle, intflags, const QString&text, QRect*boundingRect= nullptr) |
void | drawText(const QRectF&rectangle, intflags, const QString&text, QRectF*boundingRect= nullptr) |
void | drawText(intx, inty, intwidth, intheight, intflags, const QString&text, QRectF*boundingRect= nullptr) |
void | drawTiledPixmap(const QRectF&rectangle, const QPixmap&pixmap, const QPointF&position= QPointF()) |
void | drawTiledPixmap(const QRect&rectangle, const QPixmap&pixmap, const QPoint&position= QPoint()) |
void | drawTiledPixmap(intx, inty, intwidth, intheight, const QPixmap&pixmap, intsx= 0, intsy= 0) |
bool | end() |
void | endNativePainting() |
void | eraseRect(const QRectF&rectangle) |
无效 | eraseRect(const QRect&rectangle) |
无效 | eraseRect(intx, inty, intwidth, intheight) |
void | fillPath(const QPainterPath&path, const QBrush&brush) |
void | fillRect(const QRectF&rectangle, const QBrush&brush) |
void | fillRect(const QRect&rectangle, QGradient::Preset预设值) |
void | fillRect(const QRect&rectangle, Qt::BrushStylestyle) |
void | fillRect(const QRect&rectangle, Qt::GlobalColorcolor) |
void | fillRect(const QRect&rectangle, const QBrush&brush) |
void | fillRect(const QRect&rectangle, const QColor&color) |
void | fillRect(const QRectF&rectangle, QGradient::Preset预设值) |
无效 | fillRect(const QRectF&rectangle, Qt::BrushStylestyle) |
void | fillRect(const QRectF&rectangle, Qt::GlobalColorcolor) |
void | fillRect(const QRectF&rectangle, const QColor&color) |
void | fillRect(intx, inty, intwidth, intheight, QGradient::Preset预设值) |
无效 | fillRect(intx, inty, intwidth, intheight, Qt::BrushStylestyle) |
void | fillRect(intx, inty, intwidth, intheight, Qt::GlobalColorcolor) |
void | fillRect(intx, inty, intwidth, intheight, const QBrush&brush) |
void | fillRect(intx, inty, intwidth, intheight, const QColor&color) |
const QFont & | font() const |
QFontInfo | fontInfo()常量 |
QFontMetrics | fontMetrics() const |
bool | hasClipping() const |
bool | isActive() 常量 |
Qt::LayoutDirection | layoutDirection()常量 |
qreal | opacity()常量 |
QPaintEngine * | paintEngine()常量 |
const QPen & | pen() const |
QPainter::RenderHints | renderHints()常量 |
void | resetTransform() |
void | restore() |
void | rotate(qrealangle) |
void | save() |
void | scale(qrealsx, qrealsy) |
void | setBackground(const QBrush&brush) |
void | setBackgroundMode(Qt::BGMode模式) |
void | setBrush(const QBrush&brush) |
void | setBrush(Qt::BrushStylestyle) |
void | setBrushOrigin(const QPointF&position) |
void | setBrushOrigin(const QPoint&position) |
void | setBrushOrigin(intx, inty) |
void | setClipPath(const QPainterPath&path, Qt::ClipOperationoperation= Qt::ReplaceClip) |
void | setClipRect(const QRectF&rectangle, Qt::ClipOperationoperation= Qt::ReplaceClip) |
void | setClipRect(intx, inty, intwidth, intheight, Qt::ClipOperationoperation= Qt::ReplaceClip) |
void | setClipRect(const QRect&rectangle, Qt::ClipOperationoperation= Qt::ReplaceClip) |
void | setClipRegion(const QRegion®ion, Qt::ClipOperationoperation= Qt::ReplaceClip) |
void | setClipping(boolenable) |
void | setCompositionMode(QPainter::CompositionMode模式) |
void | setFont(const QFont&font) |
void | setLayoutDirection(Qt::LayoutDirectiondirection) |
void | setOpacity(qrealopacity) |
void | setPen(const QPen&pen) |
void | setPen(Qt::PenStylestyle) |
void | setPen(const QColor&color) |
void | setRenderHint(QPainter::RenderHinthint, boolon= true) |
void | setRenderHints(QPainter::RenderHintshints, boolon= true) |
void | setTransform(const QTransform&transform, boolcombine= false) |
void | setViewTransformEnabled(boolenable) |
void | setViewport(const QRect&rectangle) |
void | setViewport(intx, inty, intwidth, intheight) |
void | setWindow(const QRect&rectangle) |
无效 | setWindow(intx, inty, intwidth, intheight) |
void | setWorldMatrixEnabled(boolenable) |
void | setWorldTransform(const QTransform&matrix, boolcombine= false) |
void | shear(qrealsh, qrealsv) |
void | strokePath(const QPainterPath&path, const QPen&pen) |
bool | testRenderHint(QPainter::RenderHinthint) const |
const QTransform & | transform()常量 |
void | translate(const QPointF&offset) |
void | translate(const QPoint&offset) |
void | translate(qrealdx, qrealdy) |
bool | viewTransformEnabled() const |
QRect | viewport() const |
QRect | window() const |
bool | worldMatrixEnabled() const |
const QTransform & | worldTransform()常量 |
详细说明
QPainter 提供高度优化的函数,可完成 GUI 程序所需的大部分绘图工作。它可以绘制从简单线条到派和和弦等复杂形状的所有图形。它还能绘制对齐的文本和像素图。通常,它以 "自然 "坐标系绘制,但也能进行视图和世界变换。QPainter 可对继承QPaintDevice 类的任何对象进行操作。
QPainter 的常见用法是在部件的绘制事件中使用:构建并自定义(如设置钢笔或画笔)绘画器。然后绘制。记得在绘制后销毁 QPainter 对象。例如
void SimpleExampleWidget::paintEvent(QPaintEvent *) { QPainter painter(this); painter.setPen(Qt::blue); painter.setFont(QFont("Arial", 30)); painter.drawText(rect(), Qt::AlignCenter, "Qt"); }
QPainter 的核心功能是绘制,但该类还提供了几个允许您自定义 QPainter 设置和渲染质量的函数,以及其他一些实现剪切的函数。此外,您还可以通过指定绘制器的合成模式来控制不同形状的合并方式。
isActive() 函数用于指示绘制器是否处于激活状态。绘画器由begin() 函数和接收QPaintDevice 参数的构造函数激活。end() 函数和析构函数将其停用。
QPainter 与QPaintDevice 和QPaintEngine 类一起构成了 Qt 绘画系统的基础。QPainter 是用于执行绘制操作的类。QPaintDevice 表示可使用 QPainter 在其上绘制的设备。QPaintEngine 提供了绘制器用于在不同类型设备上绘制的接口。如果绘制器处于活动状态,device() 返回绘制器绘制的设备,paintEngine() 返回绘制器当前运行的绘制引擎。更多信息,请参阅 "绘制系统"。
有时,我们希望让其他人在不寻常的QPaintDevice 上绘画。QPainter 支持一个静态函数 setRedirected() 来实现这一功能。
警告: 当 paintdevice 是一个 widget 时,QPainter 只能在 paintEvent() 函数内部或由 paintEvent() 调用的函数中使用。
设置
您可以自定义多个设置,让 QPainter 根据您的偏好进行绘制:
- font() 是用于绘制文本的字体。如果画图器isActive() ,您可以分别使用fontInfo() 和fontMetrics() 函数获取有关当前设置的字体及其度量的信息。
- brush() 定义了用于填充形状的颜色或图案。
- pen() 定义了用于绘制线条或边界的颜色或纹路。
- backgroundMode() 定义是否有background() ,即要么是Qt::OpaqueMode ,要么是Qt::TransparentMode 。
- background() 仅适用于backgroundMode() 是Qt::OpaqueMode ,而pen() 是斜线的情况。在这种情况下,它描述了条纹中背景像素的颜色。
- brushOrigin() 定义了平铺画笔的原点,通常是 widget 背景的原点。
- viewport(),window(),worldTransform() 构成了画笔的坐标变换系统。更多信息,请参阅Coordinate Transformations 部分和坐标系文档。
- hasClipping() 表示绘制器是否剪辑。(如果画图器剪辑,则会剪辑到clipRegion() 。)
- layoutDirection() 定义了绘制文字时绘制器使用的布局方向。
- worldMatrixEnabled() 表示是否启用了世界变换。
- viewTransformEnabled() 表示是否启用视图变换。
请注意,其中一些设置与某些绘画设备中的设置如QWidget::font() 相同。QPainter::begin() 函数(或类似的 QPainter 构造函数)会从绘画设备中复制这些属性。
您可以随时调用save() 函数保存 QPainter 的状态,该函数会将所有可用设置保存在内部堆栈中。restore() 函数会将它们弹回。
绘制
QPainter 提供了用于绘制大多数基元的函数:drawPoint(),drawPoints(),drawLine(),drawRect(),drawRoundedRect(),drawEllipse(),drawArc(),drawPie(),drawChord(),drawPolyline(),drawPolygon(),drawConvexPolygon() 和 drawCubicBezier()。drawRects() 和drawLines() 这两个方便函数使用当前的笔和画笔在给定的QRects 或QLines 数组中绘制给定数量的矩形或直线。
QPainter 类还提供了fillRect() 函数和eraseRect() 函数,前者用于在给定的QRect 中填充给定的QBrush ,后者用于擦除给定矩形内的区域。
所有这些函数都有整数和浮点两种版本。
![]() | 基本绘图示例 基本绘图示例展示了如何使用 QPainter 类以各种样式显示基本图形基元。 |
如果您需要绘制复杂的图形,尤其是需要重复绘制时,请考虑创建一个QPainterPath 并使用drawPath() 绘制。
绘制路径示例 QPainterPath 类为绘画操作提供了一个容器,使图形形状得以构建和重复使用。 绘制路径示例展示了如何使用绘制路径来构建复杂的图形,以便进行渲染。 | ![]() |
QPainter 还提供了fillPath() 函数和strokePath() 函数,前者用于用给定的QBrush 填充给定的QPainterPath ,后者用于绘制给定路径的轮廓(即描边路径)。
另请参阅 "矢量变形"(Vector Deformation)示例,该示例展示了如何使用高级矢量技术来使用QPainterPath 绘制文本;还请参阅 "渐变"(Gradients)示例,该示例展示了 Qt 中可用的不同类型的渐变;以及 "路径描边"(Path Stroking)示例,该示例展示了 Qt 内置的破折号图案,并说明了如何使用自定义图案来扩展可用图案的范围。
文本绘制使用drawText() 完成。当您需要精细定位时,boundingRect() 会告诉您特定drawText() 命令的绘制位置。
绘制像素图和图像
绘制像素图/图像的函数有drawPixmap()、drawImage() 和drawTiledPixmap()。drawPixmap() 和drawImage() 产生的结果相同,只是drawPixmap() 在屏幕上更快,而drawImage() 在QPrinter 或其他设备上可能更快。
有一个drawPicture() 函数可以绘制整个QPicture 的内容。drawPicture() 函数是唯一一个不考虑绘制者所有设置的函数,因为QPicture 有自己的设置。
绘制高分辨率版本的像素图和图像
高分辨率版本的像素图的设备像素比值大于 1(参见QImageReader,QPixmap::devicePixelRatio() )。如果与底层QPaintDevice 的值相匹配,则会直接绘制到设备上,而不会应用额外的转换。
例如,将设备像素比为 2 的 64x64 像素大小的QPixmap 绘制到设备像素比也为 2 的高 DPI 屏幕上时就是这种情况。请注意,像素图在用户空间中实际上是 32x32 像素。Qt 中根据像素图大小计算布局几何图形的代码路径将使用这一大小。这样做的净效果是,像素图显示为高 DPI 像素图,而不是大像素图。
渲染质量
要使用 QPainter 获得最佳的渲染效果,应使用与平台无关的QImage 作为绘制设备;也就是说,使用QImage 将确保在任何平台上都能获得相同的像素显示效果。
QPainter 类还通过RenderHint 枚举和浮点精度支持提供了控制渲染质量的方法:所有绘制基元的函数都有浮点版本。
painter.drawEllipse(QRectF(-diameter / 2.0, -diameter / 2.0, diameter, diameter));
这些函数通常与QPainter::Antialiasing 渲染提示结合使用。
比较使用 int 和 float 以及使用或不使用抗锯齿渲染的同心圆。使用浮点精度版本可以绘制出间距均匀的圆环。反锯齿渲染会生成平滑的圆。 | ![]() |
RenderHint 枚举为 QPainter 指定了可由或不可由任何给定引擎遵守的标志。QPainter::Antialiasing 表示引擎应尽可能对基元的边缘进行抗锯齿处理,QPainter::TextAntialiasing 表示引擎应尽可能对文本进行抗锯齿处理,而QPainter::SmoothPixmapTransform 则表示引擎应使用平滑像素图转换算法。
renderHints() 函数返回一个标志,用于指定为该绘制器设置的渲染提示。使用setRenderHint() 函数可以设置或清除当前设置的RenderHints 。
坐标变换
通常,QPainter 在设备自身的坐标系(通常是像素)上运行,但 QPainter 对坐标变换有很好的支持。
最常用的变换是缩放、旋转、平移和剪切。使用scale() 函数可以按给定偏移量缩放坐标系,使用rotate() 函数可以顺时针旋转坐标系,使用translate() 函数可以平移坐标系(即在各点上添加给定偏移量)。您还可以使用shear() 函数围绕原点扭转坐标系。有关剪切坐标系的可视化,请参阅仿射变换示例。
另请参阅变换示例,了解变换如何影响 QPainter 渲染图形基元的方式。特别是,它展示了变换的顺序如何影响结果。
仿射变换示例 仿射变换示例展示了 Qt 在绘画操作中执行仿射变换的能力。该演示还允许用户尝试变换操作并立即看到结果。 | ![]() |
所有变换操作都在变换worldTransform() 上进行。矩阵将平面中的一个点变换为另一个点。有关变换矩阵的更多信息,请参阅坐标系和QTransform 文档。
setWorldTransform() 函数可以替换或添加当前设置的worldTransform()。resetTransform() 函数重置使用translate(),scale(),shear(),rotate(),setWorldTransform(),setViewport() 和setWindow() 函数进行的任何变换。deviceTransform() 返回将逻辑坐标转换为与平台相关的喷涂设备的设备坐标的矩阵。只有在平台相关句柄上使用平台绘制命令时,才需要使用后一个函数,而且平台不会进行原生转换。
使用 QPainter 绘画时,我们使用逻辑坐标指定点,然后将其转换为绘画设备的物理坐标。逻辑坐标到物理坐标的映射由 QPainter 的combinedTransform() 处理,它是viewport() 和window() 以及worldTransform() 的组合。viewport() 表示指定任意矩形的物理坐标,window() 用逻辑坐标描述同一矩形,worldTransform() 与转换矩阵相同。
另请参阅坐标系
剪切
QPainter 可以将任何绘图操作剪辑为矩形、区域或矢量路径。当前的剪辑可通过函数clipRegion() 和clipPath() 获得。路径或区域是否优先(更快)取决于底层的paintEngine() 。例如,QImage 绘画引擎更喜欢路径,而 X11 绘画引擎更喜欢区域。设置剪辑是在绘制者的逻辑坐标中完成的。
在 QPainter 的剪辑之后,绘画设备也可能会剪辑。例如,大多数部件会剪切掉子部件使用的像素,大多数打印机会剪切掉靠近纸张边缘的区域。clipRegion() 或hasClipping() 的返回值不会反映这种额外的剪切。
构图模式
QPainter 提供的CompositionMode 枚举定义了数字图像合成的 Porter-Duff 规则;它描述了将一个图像(即源图像)中的像素与另一个图像(即目标图像)中的像素进行组合的模式。
最常见的两种合成形式是Source 和SourceOver 。Source 用于在绘画设备上绘制不透明物体。在这种模式下,源图像中的每个像素都会替换目标图像中的相应像素。在SourceOver 合成模式下,源对象是透明的,绘制在目标对象之上。
请注意,合成变换是按像素进行操作的。因此,使用图形基元本身与其边界矩形之间是有区别的:边界矩形包含 alpha == 0 的像素(即基元周围的像素)。这些像素将覆盖其他图像的像素,从而有效清除这些像素,而基元只覆盖其自身区域。
![]() | 合成模式示例 Qt 示例目录中的合成模式示例可让您尝试使用各种合成模式,并立即看到结果。 |
限制
如果您使用 Qt 基于光栅的绘制引擎进行坐标绘制,需要注意的是,虽然可以使用大于 +/-215的坐标,但使用超出此范围的坐标进行的任何绘制都不能保证显示出来;绘制可能会被剪切。这是因为在实现过程中使用了short int
。
在处理曲线图形时,Qt 的描边器生成的轮廓只是一个近似值。在大多数情况下,贝塞尔曲线段的轮廓不可能用另一条贝塞尔曲线段来表示,因此 Qt 通过使用几条较小的曲线来近似曲线轮廓。出于性能方面的考虑,Qt 使用这些曲线的数量是有限制的,因此在使用较大的笔宽或缩放比例时,轮廓误差会增大。要生成误差较小的轮廓,可以使用QPainterPathStroker 类,该类的 setCurveThreshold 成员函数可让用户指定误差容限。另一种解决方法是先将路径转换为多边形,然后再绘制多边形。
性能
QPainter 是一个内容丰富的框架,允许开发人员进行各种图形操作,如渐变、合成模式和矢量图形。QPainter 可以在各种不同的硬件和软件堆栈中完成这些操作。当然,硬件和软件的底层组合会对性能产生一定影响,而且由于组合模式、画笔、剪切、变换等的数量众多,要确保每种操作都能快速完成几乎是不可能完成的任务。作为一种折中方案,我们选择了 QPainter API 和后端中的一个子集,以保证在给定的硬件和软件组合下,能达到尽可能好的性能。
作为高性能引擎,我们重点关注的后端包括
- 光栅--该后端以纯软件方式实现所有渲染,并始终用于渲染成 QImages。为获得最佳性能,只能使用格式类型QImage::Format_ARGB32_Premultiplied 、QImage::Format_RGB32 或QImage::Format_RGB16 。任何其他格式(包括QImage::Format_ARGB32 )的性能都会大大降低。QWidget 和QPixmap 默认使用该引擎。
- OpenGL 2.0 (ES) - 该后端是硬件加速图形的主要后端。它可在支持 OpenGL 2.0 或 OpenGL/ES 2.0 规范的台式机和嵌入式设备上运行。这包括最近几年生产的大多数图形芯片。该引擎可通过在QOpenGLWidget 上使用 QPainter 启用。
这些操作包括
- 简单变换,即平移和缩放,以及 0、90、180 和 270 度旋转。
drawPixmap()
在非平滑变换模式下,结合简单变换和不透明度( ,不作为渲染提示启用)。QPainter::SmoothPixmapTransform
- 使用纯色、双色线性渐变和简单变换进行矩形填充。
- 使用简单变换和相交剪辑进行矩形剪切。
- 合成模式
QPainter::CompositionMode_Source
和QPainter::CompositionMode_SourceOver 。 - 使用纯色和双色线性渐变填充圆角矩形。
- 通过qDrawBorderPixmap 获取 3x3 补丁像素图。
该列表说明了在性能要求较高的应用中应安全使用哪些功能。对于某些设置,其他操作可能也很快,但在广泛使用之前,建议在软件最终运行的系统上进行基准测试和验证。在某些情况下,也可以使用昂贵的操作,例如将结果缓存在QPixmap 中。
另请参见 QPaintDevice,QPaintEngine 、 Qt SVG基本绘图示例》和Drawing Utility Functions 。
成员类型文档
enum QPainter::CompositionMode
定义数字影像合成所支持的模式。合成模式用于指定如何将一幅图像(即源图像)中的像素与另一幅图像(即目标图像)中的像素合并。
请注意,以 RasterOp 为前缀的比特光栅操作模式仅在 X11 和光栅绘图引擎中受本地支持。这意味着,在 Mac 上使用这些模式的唯一方法是通过QImage 。RasterOp 表示的混合模式不支持带有 alpha 组件的钢笔和画笔。此外,打开QPainter::Antialiasing 渲染提示将有效禁用 RasterOp 模式。
最常见的混合模式是 "源覆盖"(SourceOver)(通常简称为 "alpha 混合"),在这种模式下,源像素会被混合到目标像素的顶部,这样源像素的 alpha 分量就定义了像素的半透明度。
有几种合成模式需要在源图像或目标图像中加入 alpha 通道才能产生效果。为获得最佳性能,首选图像格式为Format_ARGB32_Premultiplied 。
设置合成模式后,它适用于所有绘画操作、钢笔、画笔、渐变和像素图/图像绘制。
常数 | 值 | 说明 |
---|---|---|
QPainter::CompositionMode_SourceOver | 0 | 这是默认模式。源像素的 alpha 值用于将像素混合到目标像素之上。 |
QPainter::CompositionMode_DestinationOver | 1 | 目标像素的 alpha 值用于将其混合到源像素之上。该模式是 CompositionMode_SourceOver 的逆模式。 |
QPainter::CompositionMode_Clear | 2 | 目标像素会被清除(设置为完全透明),与源像素无关。 |
QPainter::CompositionMode_Source | 3 | 输出为源像素。(这意味着基本的复制操作,与源像素不透明时的 SourceOver 相同)。 |
QPainter::CompositionMode_Destination | 4 | 输出为目标像素。这意味着混合没有效果。该模式是 CompositionMode_Source 的逆模式。 |
QPainter::CompositionMode_SourceIn | 5 | 输出为源像素,其中的 alpha 值被目标像素的 alpha 值减小。 |
QPainter::CompositionMode_DestinationIn | 6 | 输出为目标像素,其 Alpha 值被源像素的 Alpha 值减小。该模式是 CompositionMode_SourceIn 的逆模式。 |
QPainter::CompositionMode_SourceOut | 7 | 输出为源图像,阿尔法值按目标图像的倒数减小。 |
QPainter::CompositionMode_DestinationOut | 8 | 输出为目标,阿尔法值按源的倒数减小。该模式是 CompositionMode_SourceOut 的逆模式。 |
QPainter::CompositionMode_SourceAtop | 9 | 源像素混合在目标像素之上,源像素的 alpha 值减去目标像素的 alpha 值。 |
QPainter::CompositionMode_DestinationAtop | 10 | 目标像素混合在源像素的顶部,目标像素的 alpha 会被目标像素的 alpha 所减小。此模式是合成模式(CompositionMode_SourceAtop)的逆模式。 |
QPainter::CompositionMode_Xor | 11 | 源像素的 alpha 值按照目标像素的 alpha 值的倒数减小,然后与目标像素合并,目标像素的 alpha 值按照源像素的 alpha 值的倒数减小。CompositionMode_Xor 与 bitwise Xor 不同。 |
QPainter::CompositionMode_Plus | 12 | 源像素和目标像素的 alpha 值和颜色都会相加。 |
QPainter::CompositionMode_Multiply | 13 | 输出结果是源颜色乘以目标颜色。颜色与白色相乘,颜色保持不变,而颜色与黑色相乘,则产生黑色。 |
QPainter::CompositionMode_Screen | 14 | 将源色和目标色倒置后再相乘。用白色筛选颜色会产生白色,而用黑色筛选颜色则不会改变颜色。 |
QPainter::CompositionMode_Overlay | 15 | 根据目标颜色的不同进行颜色的相乘或筛选。目的色与源色混合,以反映目的色的明暗程度。 |
QPainter::CompositionMode_Darken | 16 | 选择源颜色和目标颜色中较深的颜色。 |
QPainter::CompositionMode_Lighten | 17 | 选择源色和目标色中较亮的颜色。 |
QPainter::CompositionMode_ColorDodge | 18 | 目的地颜色变亮,以反映源颜色。源颜色为黑色时,目的地颜色保持不变。 |
QPainter::CompositionMode_ColorBurn | 19 | 目的地颜色变暗,以反映源颜色。白色源颜色会使目标颜色保持不变。 |
QPainter::CompositionMode_HardLight | 20 | 根据源颜色对颜色进行倍增或筛选。浅色源颜色会使目标颜色变浅,而深色源颜色会使目标颜色变深。 |
QPainter::CompositionMode_SoftLight | 21 | 根据源颜色加深或减淡颜色。类似于 CompositionMode_HardLight。 |
QPainter::CompositionMode_Difference | 22 | 从较亮的颜色中减去较暗的颜色。使用白色涂抹会反转目标颜色,而使用黑色涂抹则不会改变目标颜色。 |
QPainter::CompositionMode_Exclusion | 23 | 与 CompositionMode_Difference 类似,但对比度较低。使用白色涂抹会反转目标颜色,而使用黑色涂抹则会保持目标颜色不变。 |
QPainter::RasterOp_SourceOrDestination | 24 | 对源像素和目标像素(src 或 dst)进行比特 OR 运算。 |
QPainter::RasterOp_SourceAndDestination | 25 | 对源像素和目标像素进行比特 AND 运算(src AND dst)。 |
QPainter::RasterOp_SourceXorDestination | 26 | 对源像素和目标像素进行比特 XOR 运算(src XOR dst)。 |
QPainter::RasterOp_NotSourceAndNotDestination | 27 | 对源像素和目标像素进行 NOR 运算((NOT src) AND (NOT dst))。 |
QPainter::RasterOp_NotSourceOrNotDestination | 28 | 对源像素和目标像素进行按位 NAND 运算((NOT src)或(NOT dst))。 |
QPainter::RasterOp_NotSourceXorDestination | 29 | 对源像素进行反相运算,然后与目标像素进行 XOR 运算((NOT src)XOR dst)。 |
QPainter::RasterOp_NotSource | 30 | 进行位运算,将源像素反转(NOT src)。 |
QPainter::RasterOp_NotSourceAndDestination | 31 | 进行比特运算,将源像素反转,然后与目标像素 AND((NOT src)AND dst)。 |
QPainter::RasterOp_SourceAndNotDestination | 32 | 进行比特运算,将源像素与反相的目标像素进行 AND 运算(src AND (NOT dst))。 |
QPainter::RasterOp_NotSourceOrDestination | 33 | 进行比特运算,将源像素反转,然后与目标像素进行 OR 运算((NOT src)OR dst)。 |
QPainter::RasterOp_ClearDestination | 35 | 目的地像素被清零(设置为 0),与源像素无关。 |
QPainter::RasterOp_SetDestination | 36 | 目的地像素被设置(设置为 1),与源值无关。 |
QPainter::RasterOp_NotDestination | 37 | 执行位运算,目的像素被反转(NOT dst)。 |
QPainter::RasterOp_SourceOrNotDestination | 34 | 进行比特运算,将源像素与反转的目标像素进行 OR 运算(src OR (NOT dst))。 |
另请参阅 compositionMode(),setCompositionMode(),Composition Modes 和图像合成示例。
枚举 QPainter::PixmapFragmentHint
标志 QPainter::PixmapFragmentHints
常量 | 值 | 说明 |
---|---|---|
QPainter::OpaqueHint | 0x01 | 表示要绘制的像素图片段是不透明的。不透明片段可能会加快绘制速度。 |
PixmapFragmentHints 类型是QFlags<PixmapFragmentHint> 的类型定义。它存储 PixmapFragmentHint 值的 OR 组合。
另请参阅 QPainter::drawPixmapFragments() 和QPainter::PixmapFragment 。
枚举 QPainter::RenderHint
flags QPainter::RenderHints
Renderhints 用于为QPainter 指定标志,这些标志可能会也可能不会被任何给定的引擎所遵守。
常量 | 值 | 说明 |
---|---|---|
QPainter::Antialiasing | 0x01 | 表示引擎应尽可能反锯齿基元的边缘。 |
QPainter::TextAntialiasing | 0x02 | 表示引擎应尽可能对文本进行反锯齿处理。要强制禁用文本抗锯齿,请勿使用此提示。相反,请在字体样式策略中设置QFont::NoAntialias 。 |
QPainter::SmoothPixmapTransform | 0x04 | 表示引擎应使用平滑的像素图变换算法(如双线性),而不是最近邻算法。 |
QPainter::VerticalSubpixelPositioning | 0x08 | 在字体引擎支持的情况下,允许文本在垂直和水平方向上以零点几像素定位。目前,Freetype 在所有平台上(当提示首选项为QFont::PreferNoHinting 时)以及 macOS 上都支持此功能。对于大多数使用情况,这不会提高视觉质量,但可能会增加内存消耗并降低文本渲染性能。因此,除非使用情况需要,否则不建议启用。其中一种使用情况可能是将字形与其他视觉基元对齐。此值在 Qt 6.1 中添加。 |
QPainter::LosslessImageRendering | 0x40 | 尽可能使用无损图像渲染。目前,该提示仅在QPainter 通过QPrinter 或QPdfWriter 输出 PDF 文件时使用,其中drawImage()/drawPixmap() 调用将使用无损压缩算法而非有损 JPEG 压缩对图像进行编码。此值在 Qt 5.13 中添加。 |
QPainter::NonCosmeticBrushPatterns | 0x80 | 当使用带有预定义图案样式之一的画笔绘制图案时,图案也会与被绘制的对象一起变换。默认情况下,图案被视为外观图案,因此图案像素将直接映射到设备像素,与任何活动变换无关。此值在 Qt 6.4 中添加。 |
RenderHints 类型是QFlags<RenderHint> 的类型定义。它存储 RenderHint 值的 OR 组合。
另请参阅 renderHints(),setRenderHint(), 和Rendering Quality 。
成员函数文档
QPainter::QPainter()
构造一个画家。
[explicit]
QPainter::QPainter(QPaintDevice *device)
构造一个立即开始绘制device 的绘制器。
该构造函数便于短时绘制,例如在QWidget::paintEvent() 中,并且只能使用一次。构造函数会为您调用begin() ,而 QPainter 的析构函数会自动调用end() 。
void MyWidget::paintEvent(QPaintEvent *) { QPainter p; p.begin(this); p.drawLine(drawingCode); // drawing code p.end(); }
使用该构造函数的相同示例:
void MyWidget::paintEvent(QPaintEvent *) { QPainter p(this); p.drawLine(drawingCode); // drawing code }
由于构造函数无法在绘画器初始化失败时提供反馈,因此您应该使用begin() 和end() 在外部设备(如打印机)上绘画。
[noexcept]
QPainter::~QPainter()
摧毁画家
const QBrush &QPainter::background() const
返回当前背景笔刷。
另请参阅 setBackground() 和Settings 。
Qt::BGMode QPainter::backgroundMode() const
返回当前的背景模式。
另请参阅 setBackgroundMode() 和Settings 。
bool QPainter::begin(QPaintDevice *device)
开始绘制device ,如果成功则返回true
,否则返回false
。
请注意,调用 begin() 时,所有绘画器设置(setPen(),setBrush() 等)都将重置为默认值。
可能出现的错误是严重的问题,例如这些:
painter->begin(0); // impossible - paint device cannot be 0 QPixmap image(0, 0); painter->begin(&image); // impossible - image.isNull() == true; painter->begin(myWidget); painter2->begin(myWidget); // impossible - only one painter at a time
请注意,大多数情况下,您可以使用其中一个构造函数来代替 begin(),而且end() 在销毁时会自动执行。
警告: 一个绘制设备一次只能由一个绘制者绘制。
警告: 不支持在格式为QImage::Format_Indexed8 的QImage 上涂画。
void QPainter::beginNativePainting()
刷新绘制管道,为用户直接向底层图形上下文发布命令做好准备。之后必须调用endNativePainting()。
请注意,只有底层绘画引擎更改的状态才会重置为各自的默认状态。我们重置的状态可能会随版本的变化而变化。目前,OpenGL 2 引擎会重置以下状态:
- 混合被禁用
- 禁用深度、模板和剪刀测试
- 活动纹理单元重置为 0
- 深度遮罩、深度函数和清除深度重置为默认值
- 模板蒙板、模板操作和模板功能重置为默认值
- 当前颜色重置为纯白色
例如,如果用户在 beginNativePaint()/endNativePainting() 块内更改了 OpenGL 多边形模式,则该模式不会被endNativePainting() 重置为默认状态。下面的示例显示了绘制命令与原始 OpenGL 命令的混合情况:
QPainter painter(this); painter.fillRect(0, 0, 128, 128, Qt::green); painter.beginNativePainting(); glEnable(GL_SCISSOR_TEST); glScissor(0, 0, 64, 64); glClearColor(1, 0, 0, 1); glClear(GL_COLOR_BUFFER_BIT); glDisable(GL_SCISSOR_TEST); painter.endNativePainting();
另请参见 endNativePainting()。
QRectF QPainter::boundingRect(const QRectF &rectangle, int flags, const QString &text)
使用当前设置的font() 返回text 的边界矩形,该矩形将在给定的rectangle 与指定的flags 之间绘制;也就是说,该函数将告诉您drawText() 函数在给定相同参数时的绘制位置。
如果使用指定的flags 使text 与给定的rectangle 不匹配,函数将返回所需的矩形。
flags 参数是以下标志的比特 OR:
- Qt::AlignLeft
- Qt::AlignRight
- Qt::AlignHCenter
- Qt::AlignTop
- Qt::AlignBottom
- Qt::AlignVCenter
- Qt::AlignCenter
- Qt::TextSingleLine
- Qt::TextExpandTabs
- Qt::TextShowMnemonic
- Qt::TextWordWrap
- Qt::TextIncludeTrailingSpaces
如果设置了多个水平对齐标志或多个垂直对齐标志,则产生的对齐方式是未定义的。
另请参阅 drawText(),Qt::Alignment, 和Qt::TextFlag 。
QRect QPainter::boundingRect(const QRect &rectangle, int flags, const QString &text)
这是一个重载函数。
使用当前设置的font() 返回text 的边界矩形,该矩形在使用指定的flags 和给定的rectangle 内部绘制时会显示出来。
QRectF QPainter::boundingRect(const QRectF &rectangle, const QString &text, const QTextOption &option = QTextOption())
这是一个重载函数。
该重载函数不将 flags 指定为Qt::AlignmentFlag 和Qt::TextFlag 的 bitwise OR,而是使用option 作为参数。QTextOption 类提供了一般富文本属性的说明。
另请参阅 QTextOption 。
QRect QPainter::boundingRect(int x, int y, int w, int h, int flags, const QString &text)
这是一个重载函数。
返回给定text 的边界矩形,该矩形将绘制在以点 (x,y) 为起点、宽w 和高h 的矩形内。
const QBrush &QPainter::brush() const
返回画家的当前画笔。
另请参阅 QPainter::setBrush() 和Settings 。
QPoint QPainter::brushOrigin() const
返回当前设置的笔刷原点。
另请参阅 setBrushOrigin() 和Settings 。
QRectF QPainter::clipBoundingRect() const
如果有剪辑,则返回当前剪辑的边界矩形;否则返回空矩形。请注意,剪辑区域是以逻辑坐标给出的。
不保证包围矩形是紧密的。
另请参阅 setClipRect()、setClipPath() 和setClipRegion()。
QPainterPath QPainter::clipPath() const
以逻辑坐标方式返回当前剪辑路径。
警告: QPainter 不会显式存储组合剪辑,因为这是由底层QPaintEngine 处理的,因此路径会根据需要重新创建,并转换为当前的逻辑坐标系。这可能是一个昂贵的操作。
另请参阅 setClipPath()、clipRegion() 和setClipping()。
QRegion QPainter::clipRegion() const
返回当前设置的剪辑区域。请注意,剪辑区域是以逻辑坐标给出的。
警告: QPainter 不会显式存储组合剪辑,因为这是由底层QPaintEngine 处理的,因此路径会根据需要重新创建,并转换为当前的逻辑坐标系。这可能是一个昂贵的操作。
另请参阅 setClipRegion()、clipPath() 和setClipping()。
QTransform QPainter::combinedTransform() const
返回结合当前窗口/视口和世界变换的变换矩阵。
另请参阅 setWorldTransform()、setWindow() 和setViewport()。
QPainter::CompositionMode QPainter::compositionMode() const
返回当前的合成模式。
另请参阅 CompositionMode 和setCompositionMode()。
QPaintDevice *QPainter::device() const
返回该绘制器当前正在绘制的绘制设备,如果绘制器未激活,则返回nullptr
。
另请参阅 isActive()。
const QTransform &QPainter::deviceTransform() const
返回将逻辑坐标转换为平台相关绘制设备的设备坐标的矩阵。
只有在依赖于平台的句柄 (Qt::HANDLE) 上使用平台绘制命令时,才需要使用此函数,而且平台并不直接进行转换。
可以查询QPaintEngine::PaintEngineFeature 枚举来确定平台是否执行转换。
另请参阅 worldTransform() 和QPaintEngine::hasFeature()。
void QPainter::drawArc(const QRectF &rectangle, int startAngle, int spanAngle)
绘制给定的rectangle 、startAngle 和spanAngle 所定义的弧线。
startAngle 和spanAngle 必须以 1/16 分之一度为单位指定,即一整圈等于 5760 (16 * 360)。角度的正值表示逆时针方向,负值表示顺时针方向。零度位于 3 点钟位置。
![]() |
另请参阅 drawPie(),drawChord() 和坐标系。
void QPainter::drawArc(const QRect &rectangle, int startAngle, int spanAngle)
这是一个重载函数。
绘制由给定的rectangle 、startAngle 和spanAngle 定义的弧线。
void QPainter::drawArc(int x, int y, int width, int height, int startAngle, int spanAngle)
这是一个重载函数。
绘制以 (x,y) 为起点的矩形所定义的弧,并指定width 和height ,以及给定的startAngle 和spanAngle 。
void QPainter::drawChord(const QRectF &rectangle, int startAngle, int spanAngle)
绘制由给定的rectangle 、startAngle 和spanAngle 定义的弦线。该弦线由当前的brush() 填充。
起始角(startAngle)和跨度角(spanAngle)必须以 1/16 度为单位指定,即一个完整的圆等于 5760(16 * 360)。角度的正值表示逆时针方向,负值表示顺时针方向。零度位于 3 点钟位置。
![]() |
另请参阅 drawArc(),drawPie() 和坐标系。
void QPainter::drawChord(const QRect &rectangle, int startAngle, int spanAngle)
这是一个重载函数。
绘制由给定的rectangle 、startAngle 和spanAngle 定义的和弦。
void QPainter::drawChord(int x, int y, int width, int height, int startAngle, int spanAngle)
这是一个重载函数。
绘制以 (x,y) 为起点的矩形所定义的弦,并指定width 和height 以及给定的startAngle 和spanAngle 。
void QPainter::drawConvexPolygon(const QPointF *points, int pointCount)
使用当前笔绘制由数组points 中第一个pointCount 点定义的凸多边形。
![]() |
第一个点与最后一个点隐式连接,多边形由当前的brush() 填充。如果所提供的多边形不是凸形,即至少包含一个大于 180 度的角度,则结果是未定义的。
在某些平台(如 X11)上,drawConvexPolygon() 函数可能比drawPolygon() 函数更快。
另请参见 drawPolygon()、drawPolyline() 和坐标系。
void QPainter::drawConvexPolygon(const QPolygon &polygon)
这是一个重载函数。
使用当前笔和笔刷绘制polygon 定义的凸多边形。
void QPainter::drawConvexPolygon(const QPolygonF &polygon)
这是一个重载函数。
使用当前笔和笔刷绘制polygon 定义的凸多边形。
void QPainter::drawConvexPolygon(const QPoint *points, int pointCount)
这是一个重载函数。
使用当前笔绘制由数组points 中第一个pointCount 点定义的凸多边形。
void QPainter::drawEllipse(const QRectF &rectangle)
绘制由给定rectangle 定义的椭圆。
填充椭圆的大小为rectangle.size() 。描边椭圆的大小为rectangle.size() 加上笔宽。
void QPainter::drawEllipse(const QRect &rectangle)
这是一个重载函数。
绘制由给定的rectangle 定义的椭圆。
void QPainter::drawEllipse(const QPoint ¢er, int rx, int ry)
这是一个重载函数。
绘制位于center 、半径为rx 和ry 的椭圆。
void QPainter::drawEllipse(const QPointF ¢er, qreal rx, qreal ry)
这是一个重载函数。
绘制位于center 、半径为rx 和ry 的椭圆。
void QPainter::drawEllipse(int x, int y, int width, int height)
这是一个重载函数。
用给定的width 和height 绘制以 (x,y) 为起点的矩形所定义的椭圆。
void QPainter::drawGlyphRun(const QPointF &position, const QGlyphRun &glyphs)
在position 处绘制glyphs 所代表的字形。position 给出了字形字符串的基线边缘。这些字形将从glyphs 上选择的字体中获取,其偏移量由glyphs 中的位置给出。
另请参见 QGlyphRun::setRawFont()、QGlyphRun::setPositions() 和QGlyphRun::setGlyphIndexes()。
void QPainter::drawImage(const QRectF &target, const QImage &image, const QRectF &source, Qt::ImageConversionFlags flags = Qt::AutoColor)
将给定image 的矩形部分source 绘制到画图设备中的target 矩形中。
注: 如果图像和矩形的大小不一致,图像将按比例缩放以适应矩形。
如果需要修改图像以适应较低分辨率的结果(例如从 32 位转换为 8 位),请使用flags 来指定您希望如何进行修改。
另请参阅 drawPixmap() 和QImage::devicePixelRatio()。
void QPainter::drawImage(const QPoint &point, const QImage &image)
这是一个重载函数。
在给定的point 处绘制给定的image 。
void QPainter::drawImage(const QPointF &point, const QImage &image)
这是一个重载函数。
在给定的point 处绘制给定的image 。
void QPainter::drawImage(const QRect &rectangle, const QImage &image)
这是一个重载函数。
将给定的image 绘制到给定的rectangle 中。
注: 如果图像和矩形的大小不一致,图像将按比例缩放以适应矩形。
void QPainter::drawImage(const QRectF &rectangle, const QImage &image)
这是一个重载函数。
将给定的image 绘制到给定的rectangle 中。
注: 如果图像和矩形的大小不一致,图像将按比例缩放以适应矩形。
void QPainter::drawImage(const QPoint &point, const QImage &image, const QRect &source, Qt::ImageConversionFlags flags = Qt::AutoColor)
这是一个重载函数。
绘制给定image 的矩形部分source ,其原点位于给定的point 。
void QPainter::drawImage(const QPointF &point, const QImage &image, const QRectF &source, Qt::ImageConversionFlags flags = Qt::AutoColor)
这是一个重载函数。
绘制给定image 的矩形部分source ,其原点位于给定的point 。
void QPainter::drawImage(const QRect &target, const QImage &image, const QRect &source, Qt::ImageConversionFlags flags = Qt::AutoColor)
这是一个重载函数。
将给定image 的矩形部分source 绘制到绘画设备中的target 矩形中。
注: 如果图像和矩形的大小不一致,图像将按比例缩放以适应矩形。
void QPainter::drawImage(int x, int y, const QImage &image, int sx = 0, int sy = 0, int sw = -1, int sh = -1, Qt::ImageConversionFlags flags = Qt::AutoColor)
这是一个重载函数。
通过将image 的一部分复制到绘图设备,在 (x,y) 处绘制图像。
(x,y) 指定要绘制到的绘图设备中的左上角点。(sx,sy) 指定要绘制的image 中的左上角点。默认值为 (0, 0)。
(sw,sh) 指定要绘制图像的大小。默认值为 (0, 0)(和负值),表示一直到图像的右下角。
void QPainter::drawLine(const QLineF &line)
绘制由line 定义的直线。
另请参阅 drawLines(),drawPolyline() 和坐标系。
void QPainter::drawLine(const QLine &line)
这是一个重载函数。
绘制由line 定义的直线。
void QPainter::drawLine(const QPoint &p1, const QPoint &p2)
这是一个重载函数。
绘制一条从p1 到p2 的直线。
void QPainter::drawLine(const QPointF &p1, const QPointF &p2)
这是一个重载函数。
绘制一条从p1 到p2 的直线。
void QPainter::drawLine(int x1, int y1, int x2, int y2)
这是一个重载函数。
绘制一条从 (x1,y1) 到 (x2,y2) 的直线。
void QPainter::drawLines(const QLineF *lines, int lineCount)
使用当前笔在数组lines 中绘制前lineCount 行。
另请参阅 drawLine() 和drawPolyline()。
void QPainter::drawLines(const QList<QLine> &lines)
这是一个重载函数。
使用当前笔和笔刷绘制列表lines 所定义的线条集。
void QPainter::drawLines(const QList<QLineF> &lines)
这是一个重载函数。
使用当前笔和笔刷绘制列表lines 所定义的线条集。
void QPainter::drawLines(const QList<QPoint> &pointPairs)
这是一个重载函数。
使用当前笔为向量pointPairs 中的每对点绘制一条直线。
void QPainter::drawLines(const QList<QPointF> &pointPairs)
这是一个重载函数。
使用当前笔为向量pointPairs 中的每对点绘制一条直线。如果数组中的点数为奇数,最后一个点将被忽略。
void QPainter::drawLines(const QLine *lines, int lineCount)
这是一个重载函数。
使用当前笔在数组lines 中绘制前lineCount 行。
void QPainter::drawLines(const QPoint *pointPairs, int lineCount)
这是一个重载函数。
使用当前笔在数组pointPairs 中绘制前lineCount 行。
void QPainter::drawLines(const QPointF *pointPairs, int lineCount)
这是一个重载函数。
使用当前笔绘制pointPairs 数组中的前lineCount 行。线条是以点对的形式指定的,因此pointPairs 中的条目数必须至少为lineCount * 2。
void QPainter::drawPath(const QPainterPath &path)
使用当前钢笔绘制轮廓,使用当前画笔填充,绘制给定的绘画器path 。
![]() | QPainterPath path; path.moveTo(20, 80); path.lineTo(20, 30); path.cubicTo(80, 0, 50, 50, 80, 80); QPainter painter(this); painter.drawPath(path); |
void QPainter::drawPicture(const QPointF &point, const QPicture &picture)
在给定的point 上重播给定的picture 。
QPicture 类是记录和重播QPainter 命令的绘画设备。图片以独立于平台的格式将绘制命令序列化到 IO 设备中。所有可以在 widget 或像素图上绘制的内容都可以存储在图片中。
当调用point =QPointF(0, 0) 时,该函数的功能与QPicture::play() 完全相同。
注意: 该函数保留了绘制者的状态。
另请参见 QPicture::play()。
void QPainter::drawPicture(const QPoint &point, const QPicture &picture)
这是一个重载函数。
在给定的point 处重播给定的picture 。
void QPainter::drawPicture(int x, int y, const QPicture &picture)
这是一个重载函数。
在点 (x,y) 处绘制给定的picture 。
void QPainter::drawPie(const QRectF &rectangle, int startAngle, int spanAngle)
绘制由给定的rectangle 、startAngle 和spanAngle 定义的饼。
圆饼由当前的brush() 填充。
起始角(startAngle)和跨度角(spanAngle)必须以 1/16 度为单位指定,即一个完整的圆等于 5760(16 * 360)。角度的正值表示逆时针方向,负值表示顺时针方向。零度位于 3 点钟位置。
![]() |
另请参阅 drawEllipse(),drawChord() 和坐标系。
void QPainter::drawPie(const QRect &rectangle, int startAngle, int spanAngle)
这是一个重载函数。
绘制由给定的rectangle,startAngle 和spanAngle 定义的饼。
void QPainter::drawPie(int x, int y, int width, int height, int startAngle, int spanAngle)
这是一个重载函数。
绘制以 (x,y) 为起点的矩形所定义的饼,并指定width 和height 以及给定的startAngle 和spanAngle 。
void QPainter::drawPixmap(const QRectF &target, const QPixmap &pixmap, const QRectF &source)
将给定pixmap 的矩形部分source 绘制到绘画设备中的给定target 中。
注: 如果像素图和矩形的大小不一致,像素图会按比例调整以适应矩形。
如果pixmap 是QBitmap ,则使用 "设置 "了笔颜色的位来绘制。如果backgroundMode 是Qt::OpaqueMode ,"未设置 "的位将使用背景画笔的颜色绘制;如果backgroundMode 是Qt::TransparentMode ,"未设置 "的位将是透明的。不支持绘制带有渐变色或纹理色的位图。
另请参阅 drawImage() 和QPixmap::devicePixelRatio()。
void QPainter::drawPixmap(const QPoint &point, const QPixmap &pixmap)
这是一个重载函数。
绘制给定的pixmap ,原点位于给定的point 。
void QPainter::drawPixmap(const QPointF &point, const QPixmap &pixmap)
这是一个重载函数。
绘制给定的pixmap ,原点位于给定的point 。
void QPainter::drawPixmap(const QRect &rectangle, const QPixmap &pixmap)
这是一个重载函数。
将给定的pixmap 绘制到给定的rectangle 中。
注: 如果像素图和矩形的大小不一致,像素图将按比例调整以适应矩形。
void QPainter::drawPixmap(const QPoint &point, const QPixmap &pixmap, const QRect &source)
这是一个重载函数。
绘制给定pixmap 的矩形部分source ,其原点位于给定的point 。
void QPainter::drawPixmap(const QPointF &point, const QPixmap &pixmap, const QRectF &source)
这是一个重载函数。
绘制给定pixmap 的矩形部分source ,其原点位于给定的point 。
void QPainter::drawPixmap(const QRect &target, const QPixmap &pixmap, const QRect &source)
这是一个重载函数。
将给定pixmap 的矩形部分source 绘制到绘画设备中的给定target 中。
注: 如果像素图和矩形的大小不一致,则像素图将按比例调整以适应矩形。
void QPainter::drawPixmap(int x, int y, const QPixmap &pixmap)
这是一个重载函数。
在位置 (x,y) 处绘制给定的pixmap 。
void QPainter::drawPixmap(int x, int y, int width, int height, const QPixmap &pixmap)
这是一个重载函数。
使用给定的width 和height 将pixmap 绘制到位置 (x,y) 的矩形中。
void QPainter::drawPixmap(int x, int y, const QPixmap &pixmap, int sx, int sy, int sw, int sh)
这是一个重载函数。
通过将给定的pixmap 的一部分复制到绘制设备中,在 (x,y) 处绘制像素图。
(x,y) 指定要绘制到的绘画设备中的左上角点。(sx,sy) 指定要绘制的pixmap 中的左上角点。默认值为 (0, 0)。
(sw,sh) 指定要绘制的像素图的大小。默认值为 (0, 0)(和负值),表示一直到像素图的右下角。
void QPainter::drawPixmap(int x, int y, int w, int h, const QPixmap &pixmap, int sx, int sy, int sw, int sh)
这是一个重载函数。
以给定的pixmap 为原点(sx,sy ),宽度为sw ,高度为sh ,在点(x,y )处绘制矩形部分,宽度为w ,高度为h 。如果 sw 或 sh 等于零,则使用像素图的宽度/高度,并通过偏移 sx/sy 进行调整;
void QPainter::drawPixmapFragments(const QPainter::PixmapFragment *fragments, int fragmentCount, const QPixmap &pixmap, QPainter::PixmapFragmentHints hints = PixmapFragmentHints())
该函数用于在多个位置以不同的比例、旋转和不透明度绘制pixmap 或pixmap 的子矩形。fragments 是一个由fragmentCount 元素组成的数组,其中指定了用于绘制每个像素图片段的参数。hints 参数可用于传递绘制提示。
该函数可能比多次调用drawPixmap() 更快,因为后台可以优化状态变化。
另请参见 QPainter::PixmapFragment 和QPainter::PixmapFragmentHint 。
void QPainter::drawPoint(const QPointF &position)
使用当前钢笔的颜色在给定的position 处绘制一个点。
另请参阅 坐标系。
void QPainter::drawPoint(const QPoint &position)
这是一个重载函数。
使用当前钢笔的颜色在给定的position 处绘制一个点。
void QPainter::drawPoint(int x, int y)
这是一个重载函数。
在位置 (x,y) 处绘制一个点。
void QPainter::drawPoints(const QPointF *points, int pointCount)
使用当前笔的颜色绘制数组points 中的第一个pointCount 点。
另请参阅 坐标系。
void QPainter::drawPoints(const QPolygon &points)
这是一个重载函数。
绘制矢量points 中的点。
void QPainter::drawPoints(const QPolygonF &points)
这是一个重载函数。
绘制矢量points 中的点。
void QPainter::drawPoints(const QPoint *points, int pointCount)
这是一个重载函数。
使用当前笔的颜色绘制数组points 中的第一个pointCount 点。
void QPainter::drawPolygon(const QPointF *points, int pointCount, Qt::FillRule fillRule = Qt::OddEvenFill)
使用当前钢笔和笔刷绘制数组points 中第一个pointCount 点定义的多边形。
![]() |
第一个点与最后一个点隐式连接,多边形用当前的brush() 填充。
如果fillRule 是Qt::WindingFill ,则使用缠绕填充算法填充多边形。如果fillRule 是Qt::OddEvenFill ,多边形将使用奇偶填充算法填充。有关这些填充规则的详细说明,请参见Qt::FillRule 。
另请参阅 drawConvexPolygon(),drawPolyline() 和坐标系。
void QPainter::drawPolygon(const QPolygon &points, Qt::FillRule fillRule = Qt::OddEvenFill)
这是一个重载函数。
使用填充规则fillRule 绘制由给定的points 定义的多边形。
void QPainter::drawPolygon(const QPolygonF &points, Qt::FillRule fillRule = Qt::OddEvenFill)
这是一个重载函数。
使用填充规则fillRule 绘制由给定的points 定义的多边形。
void QPainter::drawPolygon(const QPoint *points, int pointCount, Qt::FillRule fillRule = Qt::OddEvenFill)
这是一个重载函数。
绘制数组points 中第一个pointCount 点所定义的多边形。
void QPainter::drawPolyline(const QPointF *points, int pointCount)
使用当前钢笔绘制points 中第一个pointCount 点所定义的折线。
请注意,与drawPolygon() 函数不同的是,最后一个点不会连接到第一个点,多段线也不会被填充。
另请参阅 drawLines()、drawPolygon() 和坐标系。
void QPainter::drawPolyline(const QPolygon &points)
这是一个重载函数。
使用当前笔绘制由给定的points 定义的折线。
void QPainter::drawPolyline(const QPolygonF &points)
这是一个重载函数。
使用当前笔绘制由给定的points 定义的折线。
void QPainter::drawPolyline(const QPoint *points, int pointCount)
这是一个重载函数。
使用当前笔绘制points 中第一个pointCount 点所定义的折线。
void QPainter::drawRect(const QRectF &rectangle)
使用当前钢笔和笔刷绘制当前rectangle 。
填充矩形的大小为rectangle.size()。描边矩形的大小为rectangle.size() 加上笔宽。
另请参阅 drawRects(),drawPolygon() 和坐标系。
void QPainter::drawRect(const QRect &rectangle)
这是一个重载函数。
使用当前的笔和笔刷绘制当前的rectangle 。
void QPainter::drawRect(int x, int y, int width, int height)
这是一个重载函数。
绘制一个左上角位于 (x,y) 的矩形,并指定width 和height 。
void QPainter::drawRects(const QRectF *rectangles, int rectCount)
使用当前钢笔和笔刷绘制给定rectangles 的第一个rectCount 。
另请参阅 drawRect().
void QPainter::drawRects(const QList<QRect> &rectangles)
这是一个重载函数。
使用当前钢笔和笔刷绘制给定的rectangles 。
void QPainter::drawRects(const QList<QRectF> &rectangles)
这是一个重载函数。
使用当前钢笔和笔刷绘制给定的rectangles 。
void QPainter::drawRects(const QRect *rectangles, int rectCount)
这是一个重载函数。
使用当前钢笔和笔刷绘制给定rectangles 的第一个rectCount 。
void QPainter::drawRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius, Qt::SizeMode mode = Qt::AbsoluteSize)
绘制带有圆角的给定矩形rect 。
xRadius 和yRadius 参数指定了定义圆角矩形四角的椭圆的半径。当mode 为Qt::RelativeSize 时,xRadius 和yRadius 分别以矩形宽度和高度一半的百分比指定,范围应在 0.0 到 100.0 之间。
填充矩形的大小为 rect.size()。描边矩形的大小为 rect.size() 加上笔宽。
![]() |
void QPainter::drawRoundedRect(const QRect &rect, qreal xRadius, qreal yRadius, Qt::SizeMode mode = Qt::AbsoluteSize)
这是一个重载函数。
绘制带有圆角的给定矩形rect 。
void QPainter::drawRoundedRect(int x, int y, int w, int h, qreal xRadius, qreal yRadius, Qt::SizeMode mode = Qt::AbsoluteSize)
这是一个重载函数。
以圆角绘制给定的矩形x,y,w,h 。
void QPainter::drawStaticText(const QPointF &topLeftPosition, const QStaticText &staticText)
在给定的topLeftPosition 处绘制给定的staticText 。
文字将使用绘制者设置的字体和变换进行绘制。如果画图器上设置的字体和/或变换与初始化QStaticText 布局时使用的字体和/或变换不同,则必须重新计算布局。使用QStaticText::prepare() 将staticText 初始化为以后要绘制的字体和变换。
如果topLeftPosition 与初始化时的staticText 或上次绘制时的 不一样,那么在将文本转换到新位置时会有轻微的开销。
注: 如果绘制者的变换不是仿射变换,那么staticText 将使用对drawText() 的常规调用来绘制,从而失去了提高性能的潜力。
注: Y 位置用作字体的顶端。
另请参见 QStaticText 。
void QPainter::drawStaticText(const QPoint &topLeftPosition, const QStaticText &staticText)
这是一个重载函数。
在topLeftPosition 处绘制staticText 。
注: Y 位置用作字体的顶端。
void QPainter::drawStaticText(int left, int top, const QStaticText &staticText)
这是一个重载函数。
在坐标left 和top 处绘制staticText 。
注: Y 位置用作字体的顶部。
void QPainter::drawText(const QPointF &position, const QString &text)
以当前定义的文本方向绘制给定的text ,从给定的position 开始。
该函数不处理换行符(\n),因为它无法将文本分成多行,也无法显示换行符。如果您想绘制带有换行符的多行文本,或想将文本包裹起来,请使用 QPainter::drawText() 重载,该重载会获取一个矩形。
默认情况下,QPainter 会绘制抗锯齿文本。
注: y-位置用作字体的基线。
void QPainter::drawText(const QPoint &position, const QString &text)
这是一个重载函数。
以当前定义的文本方向绘制给定的text ,从给定的position 开始。
默认情况下,QPainter 会绘制抗锯齿文本。
注: y-位置用作字体的基线。
void QPainter::drawText(const QRectF &rectangle, const QString &text, const QTextOption &option = QTextOption())
这是一个重载函数。
在指定的rectangle 中绘制给定的text ,使用option 控制其定位、方向和方位。option 中给出的选项覆盖QPainter 对象本身设置的选项。
默认情况下,QPainter 会绘制抗锯齿文本。
注: rectangle 的 y 坐标用作字体的顶端。
void QPainter::drawText(int x, int y, const QString &text)
这是一个重载函数。
使用绘制器当前定义的文本方向,在位置 (x,y) 绘制给定的text 。
默认情况下,QPainter 会绘制反锯齿文本。
注: y-位置用作字体的基线。
void QPainter::drawText(const QRect &rectangle, int flags, const QString &text, QRect *boundingRect = nullptr)
这是一个重载函数。
根据指定的flags ,在提供的rectangle 中绘制给定的text 。
boundingRect (如果不是空值)将被设置为包围整个文本所需的边界矩形。例如,在下图中,虚线表示函数计算出的boundingRect ,虚线表示rectangle :
![]() | QPainter painter(this); QFont font = painter.font(); font.setPixelSize(48); painter.setFont(font); const QRect rectangle = QRect(0, 0, 100, 50); QRect boundingRect; painter.drawText(rectangle, 0, tr("Hello"), &boundingRect); QPen pen = painter.pen(); pen.setStyle(Qt::DotLine); painter.setPen(pen); painter.drawRect(boundingRect.adjusted(0, 0, -pen.width(), -pen.width())); pen.setStyle(Qt::DashLine); painter.setPen(pen); painter.drawRect(rectangle.adjusted(0, 0, -pen.width(), -pen.width())); |
默认情况下,QPainter 为文本绘制抗锯齿效果。
注: rectangle 的 y 坐标被用作字体的顶端。
void QPainter::drawText(const QRectF &rectangle, int flags, const QString &text, QRectF *boundingRect = nullptr)
这是一个重载函数。
在提供的rectangle 内绘制给定的text 。rectangle 和对齐方式flags 定义了text 的锚点。
![]() | QPainter painter(this); painter.drawText(rect, Qt::AlignCenter, tr("Qt\nProject")); |
boundingRect (如果不为空)被设置为包围整个文本的边界矩形。例如,在下图中,虚线表示函数计算出的boundingRect ,虚线表示rectangle :
![]() | QPainter painter(this); QFont font = painter.font(); font.setPixelSize(48); painter.setFont(font); const QRect rectangle = QRect(0, 0, 100, 50); QRect boundingRect; painter.drawText(rectangle, 0, tr("Hello"), &boundingRect); QPen pen = painter.pen(); pen.setStyle(Qt::DotLine); painter.setPen(pen); painter.drawRect(boundingRect.adjusted(0, 0, -pen.width(), -pen.width())); pen.setStyle(Qt::DashLine); painter.setPen(pen); painter.drawRect(rectangle.adjusted(0, 0, -pen.width(), -pen.width())); |
flags 参数是下列标志的位顺向 OR:
- Qt::AlignLeft
- Qt::AlignRight
- Qt::AlignHCenter
- Qt::AlignJustify
- Qt::AlignTop
- Qt::AlignBottom
- Qt::AlignVCenter
- Qt::AlignCenter
- Qt::TextDontClip
- Qt::TextSingleLine
- Qt::TextExpandTabs
- Qt::TextShowMnemonic
- Qt::TextWordWrap
- Qt::TextIncludeTrailingSpaces
默认情况下,QPainter 为文本绘制抗锯齿效果。
注: rectangle 的 y 坐标被用作字体的顶端。
另请参阅 Qt::AlignmentFlag,Qt::TextFlag,boundingRect() 和layoutDirection() 。
void QPainter::drawText(int x, int y, int width, int height, int flags, const QString &text, QRect *boundingRect = nullptr)
这是一个重载函数。
在以原点 (x,y),width 和height 为边界的矩形内绘制给定的text 。
boundingRect (如果不为空)将被设置为包围整个文本所需的矩形边界。例如,在下图中,虚线表示函数计算出的boundingRect ,虚线表示由x,y,width 和height 定义的矩形:
![]() | QPainter painter(this); QFont font = painter.font(); font.setPixelSize(48); painter.setFont(font); const QRect rectangle = QRect(0, 0, 100, 50); QRect boundingRect; painter.drawText(rectangle, 0, tr("Hello"), &boundingRect); QPen pen = painter.pen(); pen.setStyle(Qt::DotLine); painter.setPen(pen); painter.drawRect(boundingRect.adjusted(0, 0, -pen.width(), -pen.width())); pen.setStyle(Qt::DashLine); painter.setPen(pen); painter.drawRect(rectangle.adjusted(0, 0, -pen.width(), -pen.width())); |
flags 参数是以下标志的位相 OR:
- Qt::AlignLeft
- Qt::AlignRight
- Qt::AlignHCenter
- Qt::AlignJustify
- Qt::AlignTop
- Qt::AlignBottom
- Qt::AlignVCenter
- Qt::AlignCenter
- Qt::TextSingleLine
- Qt::TextExpandTabs
- Qt::TextShowMnemonic
- Qt::TextWordWrap
默认情况下,QPainter 会绘制抗锯齿文本。
注: y-位置用作字体的顶端。
另请参阅 Qt::AlignmentFlag,Qt::TextFlag,setFont() 和setPen() 。
void QPainter::drawTiledPixmap(const QRectF &rectangle, const QPixmap &pixmap, const QPointF &position = QPointF())
在给定的rectangle 内绘制一个平铺的pixmap ,其原点位于给定的position 。
调用 drawTiledPixmap() 类似于多次调用drawPixmap() 以像素图填充(平铺)一个区域,但根据底层窗口系统的不同,其效率可能要高得多。
与普通 dpi 显示器相比,drawTiledPixmap() 在高 dpi 显示器(devicePixelRatio > 1)上产生的平铺图案视觉效果相同。在pixmap 上设置 devicePixelRatio 可控制平铺大小。例如,将其设置为 2 会将磁贴宽度和高度减半(在 1x 和 2x 显示器上),并在 2x 显示器上产生高分辨率输出。
position 偏移量始终位于绘制者坐标系中,与显示设备像素比例无关。
另请参阅 drawPixmap().
void QPainter::drawTiledPixmap(const QRect &rectangle, const QPixmap &pixmap, const QPoint &position = QPoint())
这是一个重载函数。
在给定的rectangle 内绘制一个平铺的pixmap ,其原点位于给定的position 。
void QPainter::drawTiledPixmap(int x, int y, int width, int height, const QPixmap &pixmap, int sx = 0, int sy = 0)
这是一个重载函数。
在指定的矩形中绘制平铺的pixmap 。
(x,y) 指定要绘制到的绘图设备中的左上角点;与给定的width 和height 。 (sx,sy) 指定要绘制到的pixmap 中的左上角点;默认值为 (0,0)。
bool QPainter::end()
结束绘制。绘制时使用的任何资源都会被释放。通常不需要调用此函数,因为析构函数会调用它。
如果绘画器不再活动,则返回true
;否则返回false
。
void QPainter::endNativePainting()
在手动发出本地绘制命令后恢复绘制器。让绘制器在调用任何其他绘制命令前恢复它所依赖的任何本地状态。
另请参阅 beginNativePainting()。
void QPainter::eraseRect(const QRectF &rectangle)
擦除给定rectangle 内的区域。等同于调用
fillRect(rectangle, background());
另请参见 fillRect().
void QPainter::eraseRect(const QRect &rectangle)
这是一个重载函数。
擦除给定rectangle 内的区域。
void QPainter::eraseRect(int x, int y, int width, int height)
这是一个重载函数。
用给定的width 和height 擦除从 (x,y) 开始的矩形内的区域。
void QPainter::fillPath(const QPainterPath &path, const QBrush &brush)
使用给定的brush 填充给定的path 。不绘制轮廓。
另外,您也可以指定QColor 而不是QBrush ;QBrush 构造函数(接受QColor 参数)将自动创建一个实体图案画笔。
另请参阅 drawPath() 。
void QPainter::fillRect(const QRectF &rectangle, const QBrush &brush)
用指定的brush 填充给定的rectangle 。
另外,您也可以指定QColor 而不是QBrush ;QBrush 构造函数(接受QColor 参数)将自动创建一个实体图案画笔。
另请参阅 drawRect().
void QPainter::fillRect(const QRect &rectangle, QGradient::Preset preset)
这是一个重载函数。
使用指定的梯度preset 填充给定的rectangle 。
void QPainter::fillRect(const QRect &rectangle, Qt::BrushStyle style)
这是一个重载函数。
使用指定的笔刷style 填充给定的rectangle 。
void QPainter::fillRect(const QRect &rectangle, Qt::GlobalColor color)
这是一个重载函数。
用指定的color 填充给定的rectangle 。
void QPainter::fillRect(const QRect &rectangle, const QBrush &brush)
这是一个重载函数。
用指定的brush 填充给定的rectangle 。
void QPainter::fillRect(const QRect &rectangle, const QColor &color)
这是一个重载函数。
用指定的color 填充给定的rectangle 。
void QPainter::fillRect(const QRectF &rectangle, QGradient::Preset preset)
这是一个重载函数。
使用指定的梯度preset 填充给定的rectangle 。
void QPainter::fillRect(const QRectF &rectangle, Qt::BrushStyle style)
这是一个重载函数。
使用指定的笔刷style 填充给定的rectangle 。
void QPainter::fillRect(const QRectF &rectangle, Qt::GlobalColor color)
这是一个重载函数。
用指定的color 填充给定的rectangle 。
void QPainter::fillRect(const QRectF &rectangle, const QColor &color)
这是一个重载函数。
用指定的color 填充给定的rectangle 。
void QPainter::fillRect(int x, int y, int width, int height, QGradient::Preset preset)
这是一个重载函数。
使用给定的梯度preset ,用给定的width 和height 填充从 (x,y) 开始的矩形。
void QPainter::fillRect(int x, int y, int width, int height, Qt::BrushStyle style)
这是一个重载函数。
使用指定的画笔style ,用给定的width 和height 填充从 (x,y) 开始的矩形。
void QPainter::fillRect(int x, int y, int width, int height, Qt::GlobalColor color)
这是一个重载函数。
用给定的width 和height 填充从 (x,y) 开始的矩形,并使用给定的color 。
void QPainter::fillRect(int x, int y, int width, int height, const QBrush &brush)
这是一个重载函数。
用给定的width 和height 填充从 (x,y) 开始的矩形,并使用给定的brush 。
void QPainter::fillRect(int x, int y, int width, int height, const QColor &color)
这是一个重载函数。
用给定的width 和height 填充从 (x,y) 开始的矩形,并使用给定的color 。
const QFont &QPainter::font() const
返回当前设置的用于绘制文本的字体。
另请参阅 setFont(),drawText() 和Settings 。
QFontInfo QPainter::fontInfo() const
如果绘制器处于活动状态,则返回绘制器的字体信息。否则,返回值未定义。
另请参阅 font(),isActive() 和Settings 。
QFontMetrics QPainter::fontMetrics() const
如果绘制器处于活动状态,则返回绘制器的字体度量值。否则,返回值未定义。
另请参阅 font(),isActive() 和Settings 。
bool QPainter::hasClipping() const
如果设置了剪切,则返回true
;否则返回false
。
另请参阅 setClipping() 和Clipping 。
bool QPainter::isActive() const
如果begin() 已被调用且end() 尚未被调用,则返回true
;否则返回false
。
另请参见 begin() 和QPaintDevice::paintingActive()。
Qt::LayoutDirection QPainter::layoutDirection() const
返回绘制文本时画家使用的布局方向。
另请参阅 QTextOption::textDirection()、setLayoutDirection()、drawText() 和Settings 。
qreal QPainter::opacity() const
返回绘制器的不透明度。默认值为 1。
另请参阅 setOpacity()。
QPaintEngine *QPainter::paintEngine() const
如果油漆工处于活动状态,则返回油漆工当前使用的油漆引擎;否则返回 0。
另请参见 isActive()。
const QPen &QPainter::pen() const
返回画家当前使用的笔。
QPainter::RenderHints QPainter::renderHints() const
返回一个标志,用于指定为该绘制器设置的渲染提示。
另请参阅 setRenderHints(),testRenderHint() 和Rendering Quality 。
void QPainter::resetTransform()
重置使用translate(),scale(),shear(),rotate(),setWorldTransform(),setViewport() 和setWindow() 进行的转换。
另请参阅 Coordinate Transformations 。
void QPainter::restore()
恢复当前的绘制状态(从堆栈中弹出保存的状态)。
另请参阅 save().
void QPainter::rotate(qreal angle)
顺时针旋转坐标系。angle 参数的单位为度。
另请参阅 setWorldTransform() 和Coordinate Transformations 。
void QPainter::save()
保存当前的绘画状态(将状态推入堆栈)。save()之后必须有一个相应的restore() 函数;end() 函数会展开堆栈。
另请参阅 restore()。
void QPainter::scale(qreal sx, qreal sy)
通过 (sx,sy) 对坐标系进行缩放。
另请参见 setWorldTransform() 和Coordinate Transformations 。
void QPainter::setBackground(const QBrush &brush)
将画笔的背景笔刷设置为给定的brush 。
背景画笔是绘制不透明文本、条纹线和位图时填充的画笔。在透明背景模式(默认)下,背景画笔不起作用。
另请参阅 background(),setBackgroundMode() 和Settings 。
void QPainter::setBackgroundMode(Qt::BGMode mode)
将画图器的背景模式设置为给定的mode
Qt::TransparentMode (默认值),在不设置背景像素的情况下绘制条纹线和文本。 用当前背景色填充这些空间。Qt::OpaqueMode
请注意,要透明地绘制位图或像素图,必须使用QPixmap::setMask() 。
另请参阅 backgroundMode(),setBackground() 和Settings 。
void QPainter::setBrush(const QBrush &brush)
将画笔设置为给定的brush 。
画笔定义了填充形状的方式。
void QPainter::setBrush(Qt::BrushStyle style)
这是一个重载函数。
将画笔设置为黑色,并设置指定的style 。
void QPainter::setBrushOrigin(const QPointF &position)
将画笔原点设置为position 。
画笔原点指定了画笔的(0,0)坐标。
请注意,虽然在 Qt XML 3 中需要使用brushOrigin() 来为 widget 采用父代的背景,但现在不再需要了,因为 Qt XML 4 画笔不会绘制背景,除非您通过将 widget 的autoFillBackground 属性设置为 true 来明确告诉它这样做。
另请参阅 brushOrigin() 和Settings 。
void QPainter::setBrushOrigin(const QPoint &position)
这是一个重载函数。
将画笔的原点设置为给定的position 。
void QPainter::setBrushOrigin(int x, int y)
这是一个重载函数。
将画笔的原点设置为点 (x,y)。
void QPainter::setClipPath(const QPainterPath &path, Qt::ClipOperation operation = Qt::ReplaceClip)
启用剪辑,并将画图器的剪辑路径设置为给定的path ,剪辑值为operation 。
请注意,剪辑路径是以逻辑(绘画者)坐标指定的。
另请参阅 clipPath(),clipRegion() 和Clipping 。
void QPainter::setClipRect(const QRectF &rectangle, Qt::ClipOperation operation = Qt::ReplaceClip)
启用剪辑,并使用给定的剪辑operation 将剪辑区域设置为给定的rectangle 。默认操作是替换当前剪辑矩形。
请注意,剪辑矩形是以逻辑(绘画)坐标指定的。
另请参阅 clipRegion(),setClipping(), 和Clipping 。
void QPainter::setClipRect(int x, int y, int width, int height, Qt::ClipOperation operation = Qt::ReplaceClip)
启用剪切功能,并将剪切区域设置为从 (x,y) 开始的矩形,以及给定的width 和height 。
void QPainter::setClipRect(const QRect &rectangle, Qt::ClipOperation operation = Qt::ReplaceClip)
这是一个重载函数。
启用剪辑,并使用给定的剪辑operation 将剪辑区域设置为给定的rectangle 。
void QPainter::setClipRegion(const QRegion ®ion, Qt::ClipOperation operation = Qt::ReplaceClip)
使用指定的剪辑operation ,将剪辑区域设置为给定的region 。默认的剪辑操作是替换当前的剪辑区域。
请注意,剪辑区域是以逻辑坐标给出的。
另请参阅 clipRegion(),setClipRect(), 和Clipping 。
void QPainter::setClipping(bool enable)
如果enable 为 true,则启用削波;如果enable 为 false,则禁用削波。
另请参阅 hasClipping() 和Clipping 。
void QPainter::setCompositionMode(QPainter::CompositionMode mode)
将合成模式设置为给定的mode 。
警告: 只有在QImage 上运行的QPainter 才能完全支持所有合成模式。如compositionMode() 所述,X11 支持 RasterOp 模式。
另请参阅 compositionMode()。
void QPainter::setFont(const QFont &font)
将绘画者的字体设置为给定的font 。
随后的drawText() 函数将使用该字体。文本颜色与笔的颜色相同。
如果您设置的字体不可用,Qt 会找到一个接近的匹配字体。font() 将返回您使用 setFont() 设置的字体,而fontInfo() 将返回实际使用的字体(可能相同)。
另请参阅 font()、drawText() 和Settings 。
void QPainter::setLayoutDirection(Qt::LayoutDirection direction)
将绘制文本时画家使用的布局方向设置为指定的direction 。
默认值是Qt::LayoutDirectionAuto ,它将根据绘制的文本隐式确定方向。
另请参阅 QTextOption::setTextDirection()、layoutDirection()、drawText() 和Settings 。
void QPainter::setOpacity(qreal opacity)
将绘制器的不透明度设置为opacity 。该值应在 0.0 至 1.0 之间,其中 0.0 表示完全透明,1.0 表示完全不透明。
在绘制器上设置的不透明度将单独应用于所有绘制操作。
另请参见 opacity()。
void QPainter::setPen(const QPen &pen)
将画笔设置为给定的pen 。
pen 定义了如何绘制线条和轮廓,还定义了文字颜色。
void QPainter::setPen(Qt::PenStyle style)
这是一个重载函数。
将画笔设置为style 、宽度 1 和黑色。
void QPainter::setPen(const QColor &color)
这是一个重载函数。
将画笔设置为样式Qt::SolidLine 、宽度 1 和指定的color 。
void QPainter::setRenderHint(QPainter::RenderHint hint, bool on = true)
如果on 为 true,则在绘制器上设置给定的呈现hint ;否则清除呈现提示。
另请参阅 setRenderHints(),renderHints() 和Rendering Quality 。
void QPainter::setRenderHints(QPainter::RenderHints hints, bool on = true)
如果on 为 true,则在绘制器上设置给定的呈现hints ;否则清除呈现提示。
另请参阅 setRenderHint(),renderHints() 和Rendering Quality 。
void QPainter::setTransform(const QTransform &transform, bool combine = false)
设置世界变换矩阵。如果combine 为真,指定的transform 将与当前矩阵合并;否则,它将取代当前矩阵。
另请参阅 transform() 和setWorldTransform()。
void QPainter::setViewTransformEnabled(bool enable)
如果enable 为 true,则启用视图转换;如果enable 为 false,则禁用视图转换。
另请参阅 viewTransformEnabled() 和Window-Viewport Conversion。
void QPainter::setViewport(const QRect &rectangle)
将画图器的视口矩形设置为给定的rectangle ,并启用视图变换。
视口矩形是视图变换的一部分。视口指定了设备坐标系。它的姐妹篇window() 则指定逻辑坐标系。
默认视口矩形与设备矩形相同。
另请参阅 viewport()、viewTransformEnabled() 和窗口-视口转换。
void QPainter::setViewport(int x, int y, int width, int height)
这是一个重载函数。
将画图器的视口矩形设置为以 (x,y) 为起点,加上给定的width 和height 的矩形。
void QPainter::setWindow(const QRect &rectangle)
将画图器的窗口设置为给定的rectangle ,并启用视图变换。
窗口矩形是视图变换的一部分。窗口指定逻辑坐标系。它的姐妹篇viewport() 则指定设备坐标系。
默认窗口矩形与设备矩形相同。
另请参阅 window()、viewTransformEnabled() 和窗口-视口转换。
void QPainter::setWindow(int x, int y, int width, int height)
这是一个重载函数。
将画图器的窗口设置为以 (x,y) 和给定的width 和height 为起点的矩形。
void QPainter::setWorldMatrixEnabled(bool enable)
如果enable 为 true,则启用变换;如果enable 为 false,则禁用变换。世界变换矩阵不会改变。
另请参阅 worldMatrixEnabled(),worldTransform() 和Coordinate Transformations 。
void QPainter::setWorldTransform(const QTransform &matrix, bool combine = false)
设置世界变换矩阵。如果combine 为真,指定的matrix 将与当前矩阵合并;否则,它将取代当前矩阵。
另请参阅 worldTransform()、transform() 和setTransform()。
void QPainter::shear(qreal sh, qreal sv)
通过 (sh,sv) 对坐标系进行剪切。
另见 setWorldTransform() 和Coordinate Transformations 。
void QPainter::strokePath(const QPainterPath &path, const QPen &pen)
使用path 指定的笔绘制路径的轮廓(笔画)。pen
bool QPainter::testRenderHint(QPainter::RenderHint hint) const
如果hint 已设置,则返回true
;否则返回false
。
另请参阅 renderHints() 和setRenderHint()。
const QTransform &QPainter::transform() const
worldTransform() 的别名。返回世界变换矩阵。
另请参阅 setTransform() 和worldTransform()。
void QPainter::translate(const QPointF &offset)
通过给定的offset 来平移坐标系;即给定的offset 添加到点上。
另请参阅 setWorldTransform() 和Coordinate Transformations 。
void QPainter::translate(const QPoint &offset)
这是一个重载函数。
通过给定的offset 来平移坐标系。
void QPainter::translate(qreal dx, qreal dy)
这是一个重载函数。
通过矢量 (dx,dy) 转换坐标系。
bool QPainter::viewTransformEnabled() const
如果启用了视图转换,则返回true
;否则返回 false。
另请参阅 setViewTransformEnabled() 和worldTransform() 。
QRect QPainter::viewport() const
返回视口矩形。
另请参阅 setViewport() 和setViewTransformEnabled()。
QRect QPainter::window() const
返回窗口矩形。
另请参阅 setWindow() 和setViewTransformEnabled()。
bool QPainter::worldMatrixEnabled() const
如果启用了世界变换,则返回true
;否则返回 false。
另请参阅 setWorldMatrixEnabled(),worldTransform() 和坐标系。
const QTransform &QPainter::worldTransform() const
返回世界变换矩阵。
另请参见 setWorldTransform()。
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.