1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtGui module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
50 #include "qpaintdevice.h"
51 #include "qpaintengine.h"
53 #include "qpainter_p.h"
54 #include "qpainterpath.h"
56 #include "qpixmapcache.h"
58 #include "qtextlayout.h"
60 #include "qvarlengtharray.h"
61 #include "qstatictext.h"
62 #include "qglyphrun.h"
64 #include <private/qfontengine_p.h>
65 #include <private/qpaintengine_p.h>
66 #include <private/qemulationpaintengine_p.h>
67 #include <private/qpainterpath_p.h>
68 #include <private/qtextengine_p.h>
69 #include <private/qpaintengine_raster_p.h>
70 #include <private/qmath_p.h>
71 #include <private/qstatictext_p.h>
72 #include <private/qglyphrun_p.h>
73 #include <private/qhexstring_p.h>
74 #include <private/qguiapplication_p.h>
75 #include <private/qrawfont_p.h>
79 #define QGradient_StretchToDevice 0x10000000
80 #define QPaintEngine_OpaqueBackground 0x40000000
82 // #define QT_DEBUG_DRAW
84 bool qt_show_painter_debug_output = true;
87 extern QPixmap qt_pixmapForBrush(int style, bool invert);
89 void qt_format_text(const QFont &font,
90 const QRectF &_r, int tf, const QTextOption *option, const QString& str, QRectF *brect,
91 int tabstops, int* tabarray, int tabarraylen,
93 static void drawTextItemDecoration(QPainter *painter, const QPointF &pos, const QFontEngine *fe, QTextEngine *textEngine,
94 QTextCharFormat::UnderlineStyle underlineStyle,
95 QTextItem::RenderFlags flags, qreal width,
96 const QTextCharFormat &charFormat);
97 // Helper function to calculate left most position, width and flags for decoration drawing
98 Q_GUI_EXPORT void qt_draw_decoration_for_glyphs(QPainter *painter, const glyph_t *glyphArray,
99 const QFixedPoint *positions, int glyphCount,
100 QFontEngine *fontEngine, const QFont &font,
101 const QTextCharFormat &charFormat);
103 static inline QGradient::CoordinateMode coordinateMode(const QBrush &brush)
105 switch (brush.style()) {
106 case Qt::LinearGradientPattern:
107 case Qt::RadialGradientPattern:
108 case Qt::ConicalGradientPattern:
109 return brush.gradient()->coordinateMode();
113 return QGradient::LogicalMode;
116 /* Returns true if the gradient requires stretch to device...*/
117 static inline bool check_gradient(const QBrush &brush)
119 return coordinateMode(brush) == QGradient::StretchToDeviceMode;
122 extern bool qHasPixmapTexture(const QBrush &);
124 static inline bool is_brush_transparent(const QBrush &brush) {
125 Qt::BrushStyle s = brush.style();
126 bool brushBitmap = qHasPixmapTexture(brush)
127 ? brush.texture().isQBitmap()
128 : (brush.textureImage().depth() == 1);
129 return ((s >= Qt::Dense1Pattern && s <= Qt::DiagCrossPattern)
130 || (s == Qt::TexturePattern && brushBitmap));
133 static inline bool is_pen_transparent(const QPen &pen) {
134 return pen.style() > Qt::SolidLine || is_brush_transparent(pen.brush());
137 /* Discards the emulation flags that are not relevant for line drawing
138 and returns the result
140 static inline uint line_emulation(uint emulation)
142 return emulation & (QPaintEngine::PrimitiveTransform
143 | QPaintEngine::AlphaBlend
144 | QPaintEngine::Antialiasing
145 | QPaintEngine::BrushStroke
146 | QPaintEngine::ConstantOpacity
147 | QGradient_StretchToDevice
148 | QPaintEngine::ObjectBoundingModeGradients
149 | QPaintEngine_OpaqueBackground);
153 static bool qt_painter_thread_test(int devType, const char *what, bool extraCondition = false)
156 case QInternal::Image:
157 case QInternal::Printer:
158 case QInternal::Picture:
159 // can be drawn onto these devices safely from any thread
163 if (!extraCondition && QThread::currentThread() != qApp->thread()) {
164 qWarning("QPainter: It is not safe to use %s outside the GUI thread", what);
173 void QPainterPrivate::checkEmulation()
176 if (extended->flags() & QPaintEngineEx::DoNotEmulate)
179 bool doEmulation = false;
180 if (state->bgMode == Qt::OpaqueMode)
183 const QGradient *bg = state->brush.gradient();
184 if (bg && bg->coordinateMode() > QGradient::LogicalMode)
187 const QGradient *pg = qpen_brush(state->pen).gradient();
188 if (pg && pg->coordinateMode() > QGradient::LogicalMode)
192 if (extended != emulationEngine) {
193 if (!emulationEngine)
194 emulationEngine = new QEmulationPaintEngine(extended);
195 extended = emulationEngine;
196 extended->setState(state);
198 } else if (emulationEngine == extended) {
199 extended = emulationEngine->real_engine;
204 QPainterPrivate::~QPainterPrivate()
206 delete emulationEngine;
207 for (int i=0; i<states.size(); ++i)
215 QTransform QPainterPrivate::viewTransform() const
218 qreal scaleW = qreal(state->vw)/qreal(state->ww);
219 qreal scaleH = qreal(state->vh)/qreal(state->wh);
220 return QTransform(scaleW, 0, 0, scaleH,
221 state->vx - state->wx*scaleW, state->vy - state->wy*scaleH);
229 Returns true if using a shared painter; otherwise false.
231 bool QPainterPrivate::attachPainterPrivate(QPainter *q, QPaintDevice *pdev)
236 QPainter *sp = pdev->sharedPainter();
240 // Save the current state of the shared painter and assign
241 // the current d_ptr to the shared painter's d_ptr.
243 if (!sp->d_ptr->d_ptrs) {
244 // Allocate space for 4 d-pointers (enough for up to 4 sub-sequent
245 // redirections within the same paintEvent(), which should be enough
246 // in 99% of all cases). E.g: A renders B which renders C which renders D.
247 sp->d_ptr->d_ptrs_size = 4;
248 sp->d_ptr->d_ptrs = (QPainterPrivate **)malloc(4 * sizeof(QPainterPrivate *));
249 Q_CHECK_PTR(sp->d_ptr->d_ptrs);
250 } else if (sp->d_ptr->refcount - 1 == sp->d_ptr->d_ptrs_size) {
251 // However, to support corner cases we grow the array dynamically if needed.
252 sp->d_ptr->d_ptrs_size <<= 1;
253 const int newSize = sp->d_ptr->d_ptrs_size * sizeof(QPainterPrivate *);
254 sp->d_ptr->d_ptrs = q_check_ptr((QPainterPrivate **)realloc(sp->d_ptr->d_ptrs, newSize));
256 sp->d_ptr->d_ptrs[++sp->d_ptr->refcount - 2] = q->d_ptr.data();
258 q->d_ptr.reset(sp->d_ptr.data());
260 Q_ASSERT(q->d_ptr->state);
262 // Now initialize the painter with correct widget properties.
265 pdev->redirected(&offset);
266 offset += q->d_ptr->engine->coordinateOffset();
268 // Update system rect.
269 q->d_ptr->state->ww = q->d_ptr->state->vw = pdev->width();
270 q->d_ptr->state->wh = q->d_ptr->state->vh = pdev->height();
273 if (q->d_ptr->state->WxF) {
274 q->d_ptr->state->redirectionMatrix = q->d_ptr->state->matrix;
275 q->d_ptr->state->redirectionMatrix.translate(-offset.x(), -offset.y());
276 q->d_ptr->state->worldMatrix = QTransform();
277 q->d_ptr->state->WxF = false;
279 q->d_ptr->state->redirectionMatrix = QTransform::fromTranslate(-offset.x(), -offset.y());
281 q->d_ptr->updateMatrix();
283 QPaintEnginePrivate *enginePrivate = q->d_ptr->engine->d_func();
284 if (enginePrivate->currentClipDevice == pdev) {
285 enginePrivate->systemStateChanged();
289 // Update system transform and clip.
290 enginePrivate->currentClipDevice = pdev;
291 enginePrivate->setSystemTransform(q->d_ptr->state->matrix);
295 void QPainterPrivate::detachPainterPrivate(QPainter *q)
297 Q_ASSERT(refcount > 1);
300 QPainterPrivate *original = d_ptrs[--refcount - 1];
302 inDestructor = false;
304 original->inDestructor = true;
305 } else if (!original) {
306 original = new QPainterPrivate(q);
309 d_ptrs[refcount - 1] = 0;
312 q->d_ptr.reset(original);
314 if (emulationEngine) {
315 extended = emulationEngine->real_engine;
316 delete emulationEngine;
322 void QPainterPrivate::draw_helper(const QPainterPath &originalPath, DrawOperation op)
325 if (qt_show_painter_debug_output) {
326 printf("QPainter::drawHelper\n");
330 if (originalPath.isEmpty())
333 QPaintEngine::PaintEngineFeatures gradientStretch =
334 QPaintEngine::PaintEngineFeatures(QGradient_StretchToDevice
335 | QPaintEngine::ObjectBoundingModeGradients);
337 const bool mustEmulateObjectBoundingModeGradients = extended
338 || ((state->emulationSpecifier & QPaintEngine::ObjectBoundingModeGradients)
339 && !engine->hasFeature(QPaintEngine::PatternTransform));
341 if (!(state->emulationSpecifier & ~gradientStretch)
342 && !mustEmulateObjectBoundingModeGradients) {
343 drawStretchedGradient(originalPath, op);
345 } else if (state->emulationSpecifier & QPaintEngine_OpaqueBackground) {
346 drawOpaqueBackground(originalPath, op);
352 qreal strokeOffsetX = 0, strokeOffsetY = 0;
354 QPainterPath path = originalPath * state->matrix;
355 QRectF pathBounds = path.boundingRect();
357 bool doStroke = (op & StrokeDraw) && (state->pen.style() != Qt::NoPen);
359 qreal penWidth = state->pen.widthF();
364 // In case of complex xform
365 if (state->matrix.type() > QTransform::TxScale) {
366 QPainterPathStroker stroker;
367 stroker.setWidth(penWidth);
368 stroker.setJoinStyle(state->pen.joinStyle());
369 stroker.setCapStyle(state->pen.capStyle());
370 QPainterPath stroke = stroker.createStroke(originalPath);
371 strokeBounds = (stroke * state->matrix).boundingRect();
373 strokeOffsetX = qAbs(penWidth * state->matrix.m11() / 2.0);
374 strokeOffsetY = qAbs(penWidth * state->matrix.m22() / 2.0);
380 if (!strokeBounds.isEmpty()) {
381 absPathRect = strokeBounds.intersected(QRectF(0, 0, device->width(), device->height())).toAlignedRect();
383 absPathRect = pathBounds.adjusted(-strokeOffsetX, -strokeOffsetY, strokeOffsetX, strokeOffsetY)
384 .intersected(QRectF(0, 0, device->width(), device->height())).toAlignedRect();
387 if (q->hasClipping()) {
388 bool hasPerspectiveTransform = false;
389 for (int i = 0; i < state->clipInfo.size(); ++i) {
390 const QPainterClipInfo &info = state->clipInfo.at(i);
391 if (info.matrix.type() == QTransform::TxProject) {
392 hasPerspectiveTransform = true;
396 // avoid mapping QRegions with perspective transforms
397 if (!hasPerspectiveTransform) {
398 // The trick with txinv and invMatrix is done in order to
399 // avoid transforming the clip to logical coordinates, and
400 // then back to device coordinates. This is a problem with
401 // QRegion/QRect based clips, since they use integer
402 // coordinates and converting to/from logical coordinates will
404 bool old_txinv = txinv;
405 QTransform old_invMatrix = invMatrix;
407 invMatrix = QTransform();
408 QPainterPath clipPath = q->clipPath();
409 QRectF r = clipPath.boundingRect().intersected(absPathRect);
410 absPathRect = r.toAlignedRect();
412 invMatrix = old_invMatrix;
416 // qDebug("\nQPainterPrivate::draw_helper(), x=%d, y=%d, w=%d, h=%d",
417 // devMinX, devMinY, device->width(), device->height());
418 // qDebug() << " - matrix" << state->matrix;
419 // qDebug() << " - originalPath.bounds" << originalPath.boundingRect();
420 // qDebug() << " - path.bounds" << path.boundingRect();
422 if (absPathRect.width() <= 0 || absPathRect.height() <= 0)
425 QImage image(absPathRect.width(), absPathRect.height(), QImage::Format_ARGB32_Premultiplied);
430 p.d_ptr->helper_device = helper_device;
432 p.setOpacity(state->opacity);
433 p.translate(-absPathRect.x(), -absPathRect.y());
434 p.setTransform(state->matrix, true);
435 p.setPen(doStroke ? state->pen : QPen(Qt::NoPen));
436 p.setBrush((op & FillDraw) ? state->brush : QBrush(Qt::NoBrush));
437 p.setBackground(state->bgBrush);
438 p.setBackgroundMode(state->bgMode);
439 p.setBrushOrigin(state->brushOrigin);
441 p.setRenderHint(QPainter::Antialiasing, state->renderHints & QPainter::Antialiasing);
442 p.setRenderHint(QPainter::SmoothPixmapTransform,
443 state->renderHints & QPainter::SmoothPixmapTransform);
445 p.drawPath(originalPath);
448 static bool do_fallback_overlay = qgetenv("QT_PAINT_FALLBACK_OVERLAY").size() > 0;
449 if (do_fallback_overlay) {
450 QImage block(8, 8, QImage::Format_ARGB32_Premultiplied);
452 pt.fillRect(0, 0, 8, 8, QColor(196, 0, 196));
453 pt.drawLine(0, 0, 8, 8);
456 p.setCompositionMode(QPainter::CompositionMode_SourceAtop);
458 p.fillRect(0, 0, image.width(), image.height(), QBrush(block));
465 state->matrix = QTransform();
467 extended->transformChanged();
469 state->dirtyFlags |= QPaintEngine::DirtyTransform;
472 engine->drawImage(absPathRect,
474 QRectF(0, 0, absPathRect.width(), absPathRect.height()),
475 Qt::OrderedDither | Qt::OrderedAlphaDither);
479 void QPainterPrivate::drawOpaqueBackground(const QPainterPath &path, DrawOperation op)
483 q->setBackgroundMode(Qt::TransparentMode);
485 if (op & FillDraw && state->brush.style() != Qt::NoBrush) {
486 q->fillPath(path, state->bgBrush.color());
487 q->fillPath(path, state->brush);
490 if (op & StrokeDraw && state->pen.style() != Qt::NoPen) {
491 q->strokePath(path, QPen(state->bgBrush.color(), state->pen.width()));
492 q->strokePath(path, state->pen);
495 q->setBackgroundMode(Qt::OpaqueMode);
498 static inline QBrush stretchGradientToUserSpace(const QBrush &brush, const QRectF &boundingRect)
500 Q_ASSERT(brush.style() >= Qt::LinearGradientPattern
501 && brush.style() <= Qt::ConicalGradientPattern);
503 QTransform gradientToUser(boundingRect.width(), 0, 0, boundingRect.height(),
504 boundingRect.x(), boundingRect.y());
506 QGradient g = *brush.gradient();
507 g.setCoordinateMode(QGradient::LogicalMode);
510 b.setTransform(gradientToUser * b.transform());
514 void QPainterPrivate::drawStretchedGradient(const QPainterPath &path, DrawOperation op)
518 const qreal sw = helper_device->width();
519 const qreal sh = helper_device->height();
521 bool changedPen = false;
522 bool changedBrush = false;
523 bool needsFill = false;
525 const QPen pen = state->pen;
526 const QBrush brush = state->brush;
528 const QGradient::CoordinateMode penMode = coordinateMode(pen.brush());
529 const QGradient::CoordinateMode brushMode = coordinateMode(brush);
533 // Draw the xformed fill if the brush is a stretch gradient.
534 if ((op & FillDraw) && brush.style() != Qt::NoBrush) {
535 if (brushMode == QGradient::StretchToDeviceMode) {
536 q->setPen(Qt::NoPen);
537 changedPen = pen.style() != Qt::NoPen;
541 const qreal isw = 1.0 / sw;
542 const qreal ish = 1.0 / sh;
543 QTransform inv(isw, 0, 0, ish, 0, 0);
544 engine->drawPath(path * inv);
549 if (brushMode == QGradient::ObjectBoundingMode) {
550 Q_ASSERT(engine->hasFeature(QPaintEngine::PatternTransform));
551 boundingRect = path.boundingRect();
552 q->setBrush(stretchGradientToUserSpace(brush, boundingRect));
558 if ((op & StrokeDraw) && pen.style() != Qt::NoPen) {
559 // Draw the xformed outline if the pen is a stretch gradient.
560 if (penMode == QGradient::StretchToDeviceMode) {
561 q->setPen(Qt::NoPen);
566 engine->drawPath(path);
570 q->setBrush(pen.brush());
574 QPainterPathStroker stroker;
575 stroker.setDashPattern(pen.style());
576 stroker.setWidth(pen.widthF());
577 stroker.setJoinStyle(pen.joinStyle());
578 stroker.setCapStyle(pen.capStyle());
579 stroker.setMiterLimit(pen.miterLimit());
580 QPainterPath stroke = stroker.createStroke(path);
582 const qreal isw = 1.0 / sw;
583 const qreal ish = 1.0 / sh;
584 QTransform inv(isw, 0, 0, ish, 0, 0);
585 engine->drawPath(stroke * inv);
588 if (!needsFill && brush.style() != Qt::NoBrush) {
589 q->setBrush(Qt::NoBrush);
593 if (penMode == QGradient::ObjectBoundingMode) {
594 Q_ASSERT(engine->hasFeature(QPaintEngine::PatternTransform));
596 // avoid computing the bounding rect twice
597 if (!needsFill || brushMode != QGradient::ObjectBoundingMode)
598 boundingRect = path.boundingRect();
601 p.setBrush(stretchGradientToUserSpace(pen.brush(), boundingRect));
604 } else if (changedPen) {
610 engine->drawPath(path);
612 } else if (needsFill) {
613 if (pen.style() != Qt::NoPen) {
614 q->setPen(Qt::NoPen);
619 engine->drawPath(path);
629 void QPainterPrivate::updateMatrix()
631 state->matrix = state->WxF ? state->worldMatrix : QTransform();
633 state->matrix *= viewTransform();
635 txinv = false; // no inverted matrix
636 state->matrix *= state->redirectionMatrix;
638 extended->transformChanged();
640 state->dirtyFlags |= QPaintEngine::DirtyTransform;
642 // printf("VxF=%d, WxF=%d\n", state->VxF, state->WxF);
643 // qDebug() << " --- using matrix" << state->matrix << redirection_offset;
647 void QPainterPrivate::updateInvMatrix()
649 Q_ASSERT(txinv == false);
650 txinv = true; // creating inverted matrix
651 invMatrix = state->matrix.inverted();
654 extern bool qt_isExtendedRadialGradient(const QBrush &brush);
656 void QPainterPrivate::updateEmulationSpecifier(QPainterState *s)
659 bool linearGradient = false;
660 bool radialGradient = false;
661 bool extendedRadialGradient = false;
662 bool conicalGradient = false;
663 bool patternBrush = false;
665 bool complexXform = false;
669 // Pen and brush properties (we have to check both if one changes because the
670 // one that's unchanged can still be in a state which requires emulation)
671 if (s->state() & (QPaintEngine::DirtyPen | QPaintEngine::DirtyBrush | QPaintEngine::DirtyHints)) {
672 // Check Brush stroke emulation
673 if (!s->pen.isSolid() && !engine->hasFeature(QPaintEngine::BrushStroke))
674 s->emulationSpecifier |= QPaintEngine::BrushStroke;
676 s->emulationSpecifier &= ~QPaintEngine::BrushStroke;
680 QBrush penBrush = (qpen_style(s->pen) == Qt::NoPen) ? QBrush(Qt::NoBrush) : qpen_brush(s->pen);
681 Qt::BrushStyle brushStyle = qbrush_style(s->brush);
682 Qt::BrushStyle penBrushStyle = qbrush_style(penBrush);
683 alpha = (penBrushStyle != Qt::NoBrush
684 && (penBrushStyle < Qt::LinearGradientPattern && penBrush.color().alpha() != 255)
685 && !penBrush.isOpaque())
686 || (brushStyle != Qt::NoBrush
687 && (brushStyle < Qt::LinearGradientPattern && s->brush.color().alpha() != 255)
688 && !s->brush.isOpaque());
689 linearGradient = ((penBrushStyle == Qt::LinearGradientPattern) ||
690 (brushStyle == Qt::LinearGradientPattern));
691 radialGradient = ((penBrushStyle == Qt::RadialGradientPattern) ||
692 (brushStyle == Qt::RadialGradientPattern));
693 extendedRadialGradient = radialGradient && (qt_isExtendedRadialGradient(penBrush) || qt_isExtendedRadialGradient(s->brush));
694 conicalGradient = ((penBrushStyle == Qt::ConicalGradientPattern) ||
695 (brushStyle == Qt::ConicalGradientPattern));
696 patternBrush = (((penBrushStyle > Qt::SolidPattern
697 && penBrushStyle < Qt::LinearGradientPattern)
698 || penBrushStyle == Qt::TexturePattern) ||
699 ((brushStyle > Qt::SolidPattern
700 && brushStyle < Qt::LinearGradientPattern)
701 || brushStyle == Qt::TexturePattern));
703 bool penTextureAlpha = false;
704 if (penBrush.style() == Qt::TexturePattern)
705 penTextureAlpha = qHasPixmapTexture(penBrush)
706 ? (penBrush.texture().depth() > 1) && penBrush.texture().hasAlpha()
707 : penBrush.textureImage().hasAlphaChannel();
708 bool brushTextureAlpha = false;
709 if (s->brush.style() == Qt::TexturePattern) {
710 brushTextureAlpha = qHasPixmapTexture(s->brush)
711 ? (s->brush.texture().depth() > 1) && s->brush.texture().hasAlpha()
712 : s->brush.textureImage().hasAlphaChannel();
714 if (((penBrush.style() == Qt::TexturePattern && penTextureAlpha)
715 || (s->brush.style() == Qt::TexturePattern && brushTextureAlpha))
716 && !engine->hasFeature(QPaintEngine::MaskedBrush))
717 s->emulationSpecifier |= QPaintEngine::MaskedBrush;
719 s->emulationSpecifier &= ~QPaintEngine::MaskedBrush;
722 if (s->state() & (QPaintEngine::DirtyHints
723 | QPaintEngine::DirtyOpacity
724 | QPaintEngine::DirtyBackgroundMode)) {
732 qDebug("QPainterPrivate::updateEmulationSpecifier, state=%p\n"
734 " - linearGradient: %d\n"
735 " - radialGradient: %d\n"
736 " - conicalGradient: %d\n"
737 " - patternBrush: %d\n"
746 uint(s->renderHints),
751 if (s->state() & QPaintEngine::DirtyTransform) {
752 xform = !s->matrix.isIdentity();
753 complexXform = !s->matrix.isAffine();
754 } else if (s->matrix.type() >= QTransform::TxTranslate) {
756 complexXform = !s->matrix.isAffine();
759 const bool brushXform = (!s->brush.transform().type() == QTransform::TxNone);
760 const bool penXform = (!s->pen.brush().transform().type() == QTransform::TxNone);
762 const bool patternXform = patternBrush && (xform || brushXform || penXform);
764 // Check alphablending
765 if (alpha && !engine->hasFeature(QPaintEngine::AlphaBlend))
766 s->emulationSpecifier |= QPaintEngine::AlphaBlend;
768 s->emulationSpecifier &= ~QPaintEngine::AlphaBlend;
770 // Linear gradient emulation
771 if (linearGradient && !engine->hasFeature(QPaintEngine::LinearGradientFill))
772 s->emulationSpecifier |= QPaintEngine::LinearGradientFill;
774 s->emulationSpecifier &= ~QPaintEngine::LinearGradientFill;
776 // Radial gradient emulation
777 if (extendedRadialGradient || (radialGradient && !engine->hasFeature(QPaintEngine::RadialGradientFill)))
778 s->emulationSpecifier |= QPaintEngine::RadialGradientFill;
780 s->emulationSpecifier &= ~QPaintEngine::RadialGradientFill;
782 // Conical gradient emulation
783 if (conicalGradient && !engine->hasFeature(QPaintEngine::ConicalGradientFill))
784 s->emulationSpecifier |= QPaintEngine::ConicalGradientFill;
786 s->emulationSpecifier &= ~QPaintEngine::ConicalGradientFill;
789 if (patternBrush && !engine->hasFeature(QPaintEngine::PatternBrush))
790 s->emulationSpecifier |= QPaintEngine::PatternBrush;
792 s->emulationSpecifier &= ~QPaintEngine::PatternBrush;
795 if (patternXform && !engine->hasFeature(QPaintEngine::PatternTransform))
796 s->emulationSpecifier |= QPaintEngine::PatternTransform;
798 s->emulationSpecifier &= ~QPaintEngine::PatternTransform;
801 if (xform && !engine->hasFeature(QPaintEngine::PrimitiveTransform))
802 s->emulationSpecifier |= QPaintEngine::PrimitiveTransform;
804 s->emulationSpecifier &= ~QPaintEngine::PrimitiveTransform;
806 // Perspective XForms
807 if (complexXform && !engine->hasFeature(QPaintEngine::PerspectiveTransform))
808 s->emulationSpecifier |= QPaintEngine::PerspectiveTransform;
810 s->emulationSpecifier &= ~QPaintEngine::PerspectiveTransform;
813 if (state->opacity != 1 && !engine->hasFeature(QPaintEngine::ConstantOpacity))
814 s->emulationSpecifier |= QPaintEngine::ConstantOpacity;
816 s->emulationSpecifier &= ~QPaintEngine::ConstantOpacity;
818 bool gradientStretch = false;
819 bool objectBoundingMode = false;
820 if (linearGradient || conicalGradient || radialGradient) {
821 QGradient::CoordinateMode brushMode = coordinateMode(s->brush);
822 QGradient::CoordinateMode penMode = coordinateMode(s->pen.brush());
824 gradientStretch |= (brushMode == QGradient::StretchToDeviceMode);
825 gradientStretch |= (penMode == QGradient::StretchToDeviceMode);
827 objectBoundingMode |= (brushMode == QGradient::ObjectBoundingMode);
828 objectBoundingMode |= (penMode == QGradient::ObjectBoundingMode);
831 s->emulationSpecifier |= QGradient_StretchToDevice;
833 s->emulationSpecifier &= ~QGradient_StretchToDevice;
835 if (objectBoundingMode && !engine->hasFeature(QPaintEngine::ObjectBoundingModeGradients))
836 s->emulationSpecifier |= QPaintEngine::ObjectBoundingModeGradients;
838 s->emulationSpecifier &= ~QPaintEngine::ObjectBoundingModeGradients;
840 // Opaque backgrounds...
841 if (s->bgMode == Qt::OpaqueMode &&
842 (is_pen_transparent(s->pen) || is_brush_transparent(s->brush)))
843 s->emulationSpecifier |= QPaintEngine_OpaqueBackground;
845 s->emulationSpecifier &= ~QPaintEngine_OpaqueBackground;
848 //won't be correct either way because the device can already have
849 // something rendered to it in which case subsequent emulation
850 // on a fully transparent qimage and then blitting the results
851 // won't produce correct results
853 if (state->composition_mode > QPainter::CompositionMode_Xor &&
854 !engine->hasFeature(QPaintEngine::BlendModes))
855 s->emulationSpecifier |= QPaintEngine::BlendModes;
857 s->emulationSpecifier &= ~QPaintEngine::BlendModes;
861 void QPainterPrivate::updateStateImpl(QPainterState *newState)
863 // ### we might have to call QPainter::begin() here...
864 if (!engine->state) {
865 engine->state = newState;
866 engine->setDirty(QPaintEngine::AllDirty);
869 if (engine->state->painter() != newState->painter)
870 // ### this could break with clip regions vs paths.
871 engine->setDirty(QPaintEngine::AllDirty);
873 // Upon restore, revert all changes since last save
874 else if (engine->state != newState)
875 newState->dirtyFlags |= QPaintEngine::DirtyFlags(static_cast<QPainterState *>(engine->state)->changeFlags);
877 // We need to store all changes made so that restore can deal with them
879 newState->changeFlags |= newState->dirtyFlags;
881 updateEmulationSpecifier(newState);
883 // Unset potential dirty background mode
884 newState->dirtyFlags &= ~(QPaintEngine::DirtyBackgroundMode
885 | QPaintEngine::DirtyBackground);
887 engine->state = newState;
888 engine->updateState(*newState);
889 engine->clearDirty(QPaintEngine::AllDirty);
893 void QPainterPrivate::updateState(QPainterState *newState)
897 engine->state = newState;
899 } else if (newState->state() || engine->state!=newState) {
900 bool setNonCosmeticPen = (newState->renderHints & QPainter::NonCosmeticDefaultPen)
901 && newState->pen.widthF() == 0;
902 if (setNonCosmeticPen) {
903 // Override the default pen's cosmetic state if the
904 // NonCosmeticDefaultPen render hint is used.
905 QPen oldPen = newState->pen;
906 newState->pen.setWidth(1);
907 newState->pen.setCosmetic(false);
908 newState->dirtyFlags |= QPaintEngine::DirtyPen;
910 updateStateImpl(newState);
912 // Restore the state pen back to its default to preserve visible
914 newState->pen = oldPen;
916 updateStateImpl(newState);
924 \brief The QPainter class performs low-level painting on widgets and
931 QPainter provides highly optimized functions to do most of the
932 drawing GUI programs require. It can draw everything from simple
933 lines to complex shapes like pies and chords. It can also draw
934 aligned text and pixmaps. Normally, it draws in a "natural"
935 coordinate system, but it can also do view and world
936 transformation. QPainter can operate on any object that inherits
937 the QPaintDevice class.
939 The common use of QPainter is inside a widget's paint event:
940 Construct and customize (e.g. set the pen or the brush) the
941 painter. Then draw. Remember to destroy the QPainter object after
942 drawing. For example:
944 \snippet code/src_gui_painting_qpainter.cpp 0
946 The core functionality of QPainter is drawing, but the class also
947 provide several functions that allows you to customize QPainter's
948 settings and its rendering quality, and others that enable
949 clipping. In addition you can control how different shapes are
950 merged together by specifying the painter's composition mode.
952 The isActive() function indicates whether the painter is active. A
953 painter is activated by the begin() function and the constructor
954 that takes a QPaintDevice argument. The end() function, and the
955 destructor, deactivates it.
957 Together with the QPaintDevice and QPaintEngine classes, QPainter
958 form the basis for Qt's paint system. QPainter is the class used
959 to perform drawing operations. QPaintDevice represents a device
960 that can be painted on using a QPainter. QPaintEngine provides the
961 interface that the painter uses to draw onto different types of
962 devices. If the painter is active, device() returns the paint
963 device on which the painter paints, and paintEngine() returns the
964 paint engine that the painter is currently operating on. For more
965 information, see the \l {Paint System}.
967 Sometimes it is desirable to make someone else paint on an unusual
968 QPaintDevice. QPainter supports a static function to do this,
971 \warning When the paintdevice is a widget, QPainter can only be
972 used inside a paintEvent() function or in a function called by
979 There are several settings that you can customize to make QPainter
980 draw according to your preferences:
984 \li font() is the font used for drawing text. If the painter
985 isActive(), you can retrieve information about the currently set
986 font, and its metrics, using the fontInfo() and fontMetrics()
987 functions respectively.
989 \li brush() defines the color or pattern that is used for filling
992 \li pen() defines the color or stipple that is used for drawing
995 \li backgroundMode() defines whether there is a background() or
996 not, i.e it is either Qt::OpaqueMode or Qt::TransparentMode.
998 \li background() only applies when backgroundMode() is \l
999 Qt::OpaqueMode and pen() is a stipple. In that case, it
1000 describes the color of the background pixels in the stipple.
1002 \li brushOrigin() defines the origin of the tiled brushes, normally
1003 the origin of widget's background.
1005 \li viewport(), window(), worldTransform() make up the painter's coordinate
1006 transformation system. For more information, see the \l
1007 {Coordinate Transformations} section and the \l {Coordinate
1008 System} documentation.
1010 \li hasClipping() tells whether the painter clips at all. (The paint
1011 device clips, too.) If the painter clips, it clips to clipRegion().
1013 \li layoutDirection() defines the layout direction used by the
1014 painter when drawing text.
1016 \li worldMatrixEnabled() tells whether world transformation is enabled.
1018 \li viewTransformEnabled() tells whether view transformation is
1023 Note that some of these settings mirror settings in some paint
1024 devices, e.g. QWidget::font(). The QPainter::begin() function (or
1025 equivalently the QPainter constructor) copies these attributes
1026 from the paint device.
1028 You can at any time save the QPainter's state by calling the
1029 save() function which saves all the available settings on an
1030 internal stack. The restore() function pops them back.
1034 QPainter provides functions to draw most primitives: drawPoint(),
1035 drawPoints(), drawLine(), drawRect(), drawRoundedRect(),
1036 drawEllipse(), drawArc(), drawPie(), drawChord(), drawPolyline(),
1037 drawPolygon(), drawConvexPolygon() and drawCubicBezier(). The two
1038 convenience functions, drawRects() and drawLines(), draw the given
1039 number of rectangles or lines in the given array of \l
1040 {QRect}{QRects} or \l {QLine}{QLines} using the current pen and
1043 The QPainter class also provides the fillRect() function which
1044 fills the given QRect, with the given QBrush, and the eraseRect()
1045 function that erases the area inside the given rectangle.
1047 All of these functions have both integer and floating point
1052 \li \inlineimage qpainter-basicdrawing.png
1054 \b {Basic Drawing Example}
1056 The \l {painting/basicdrawing}{Basic Drawing} example shows how to
1057 display basic graphics primitives in a variety of styles using the
1062 If you need to draw a complex shape, especially if you need to do
1063 so repeatedly, consider creating a QPainterPath and drawing it
1069 \b {Painter Paths example}
1071 The QPainterPath class provides a container for painting
1072 operations, enabling graphical shapes to be constructed and
1075 The \l {painting/painterpaths}{Painter Paths} example shows how
1076 painter paths can be used to build complex shapes for rendering.
1078 \li \inlineimage qpainter-painterpaths.png
1081 QPainter also provides the fillPath() function which fills the
1082 given QPainterPath with the given QBrush, and the strokePath()
1083 function that draws the outline of the given path (i.e. strokes
1086 See also the \l {painting/deform}{Vector Deformation} example which
1087 shows how to use advanced vector techniques to draw text using a
1088 QPainterPath, the \l {painting/gradients}{Gradients} example which shows
1089 the different types of gradients that are available in Qt, and the \l
1090 {painting/pathstroke}{Path Stroking} example which shows Qt's built-in
1091 dash patterns and shows how custom patterns can be used to extend
1092 the range of available patterns.
1096 \li \l {painting/deform}{Vector Deformation}
1097 \li \l {painting/gradients}{Gradients}
1098 \li \l {painting/pathstroke}{Path Stroking}
1100 \li \inlineimage qpainter-vectordeformation.png
1101 \li \inlineimage qpainter-gradients.png
1102 \li \inlineimage qpainter-pathstroking.png
1106 There are functions to draw pixmaps/images, namely drawPixmap(),
1107 drawImage() and drawTiledPixmap(). Both drawPixmap() and drawImage()
1108 produce the same result, except that drawPixmap() is faster
1109 on-screen while drawImage() may be faster on a QPrinter or other
1112 Text drawing is done using drawText(). When you need
1113 fine-grained positioning, boundingRect() tells you where a given
1114 drawText() command will draw.
1116 There is a drawPicture() function that draws the contents of an
1117 entire QPicture. The drawPicture() function is the only function
1118 that disregards all the painter's settings as QPicture has its own
1121 \section1 Rendering Quality
1123 To get the optimal rendering result using QPainter, you should use
1124 the platform independent QImage as paint device; i.e. using QImage
1125 will ensure that the result has an identical pixel representation
1128 The QPainter class also provides a means of controlling the
1129 rendering quality through its RenderHint enum and the support for
1130 floating point precision: All the functions for drawing primitives
1131 has a floating point version. These are often used in combination
1132 with the \l {RenderHint}{QPainter::Antialiasing} render hint.
1136 \li \inlineimage qpainter-concentriccircles.png
1138 \b {Concentric Circles Example}
1140 The \l {painting/concentriccircles}{Concentric Circles} example
1141 shows the improved rendering quality that can be obtained using
1142 floating point precision and anti-aliasing when drawing custom
1145 The application's main window displays several widgets which are
1146 drawn using the various combinations of precision and
1151 The RenderHint enum specifies flags to QPainter that may or may
1152 not be respected by any given engine. \l
1153 {RenderHint}{QPainter::Antialiasing} indicates that the engine
1154 should antialias edges of primitives if possible, \l
1155 {RenderHint}{QPainter::TextAntialiasing} indicates that the engine
1156 should antialias text if possible, and the \l
1157 {RenderHint}{QPainter::SmoothPixmapTransform} indicates that the
1158 engine should use a smooth pixmap transformation algorithm.
1159 \l {RenderHint}{HighQualityAntialiasing} is an OpenGL-specific rendering hint
1160 indicating that the engine should use fragment programs and offscreen
1161 rendering for antialiasing.
1163 The renderHints() function returns a flag that specifies the
1164 rendering hints that are set for this painter. Use the
1165 setRenderHint() function to set or clear the currently set
1168 \section1 Coordinate Transformations
1170 Normally, the QPainter operates on the device's own coordinate
1171 system (usually pixels), but QPainter has good support for
1172 coordinate transformations.
1176 \li nop \li rotate() \li scale() \li translate()
1178 \li \inlineimage qpainter-clock.png
1179 \li \inlineimage qpainter-rotation.png
1180 \li \inlineimage qpainter-scale.png
1181 \li \inlineimage qpainter-translation.png
1184 The most commonly used transformations are scaling, rotation,
1185 translation and shearing. Use the scale() function to scale the
1186 coordinate system by a given offset, the rotate() function to
1187 rotate it clockwise and translate() to translate it (i.e. adding a
1188 given offset to the points). You can also twist the coordinate
1189 system around the origin using the shear() function. See the \l
1190 {painting/affine}{Affine Transformations} example for a visualization of
1191 a sheared coordinate system.
1193 See also the \l {painting/transformations}{Transformations}
1194 example which shows how transformations influence the way that
1195 QPainter renders graphics primitives. In particular it shows how
1196 the order of transformations affects the result.
1201 \b {Affine Transformations Example}
1203 The \l {painting/affine}{Affine Transformations} example shows Qt's
1204 ability to perform affine transformations on painting
1205 operations. The demo also allows the user to experiment with the
1206 transformation operations and see the results immediately.
1208 \li \inlineimage qpainter-affinetransformations.png
1211 All the tranformation operations operate on the transformation
1212 worldTransform(). A matrix transforms a point in the plane to another
1213 point. For more information about the transformation matrix, see
1214 the \l {Coordinate System} and QTransform documentation.
1216 The setWorldTransform() function can replace or add to the currently
1217 set worldTransform(). The resetTransform() function resets any
1218 transformations that were made using translate(), scale(),
1219 shear(), rotate(), setWorldTransform(), setViewport() and setWindow()
1220 functions. The deviceTransform() returns the matrix that transforms
1221 from logical coordinates to device coordinates of the platform
1222 dependent paint device. The latter function is only needed when
1223 using platform painting commands on the platform dependent handle,
1224 and the platform does not do transformations nativly.
1226 When drawing with QPainter, we specify points using logical
1227 coordinates which then are converted into the physical coordinates
1228 of the paint device. The mapping of the logical coordinates to the
1229 physical coordinates are handled by QPainter's combinedTransform(), a
1230 combination of viewport() and window() and worldTransform(). The
1231 viewport() represents the physical coordinates specifying an
1232 arbitrary rectangle, the window() describes the same rectangle in
1233 logical coordinates, and the worldTransform() is identical with the
1234 transformation matrix.
1236 See also \l {Coordinate System}
1240 QPainter can clip any drawing operation to a rectangle, a region,
1241 or a vector path. The current clip is available using the
1242 functions clipRegion() and clipPath(). Whether paths or regions are
1243 preferred (faster) depends on the underlying paintEngine(). For
1244 example, the QImage paint engine prefers paths while the X11 paint
1245 engine prefers regions. Setting a clip is done in the painters
1246 logical coordinates.
1248 After QPainter's clipping, the paint device may also clip. For
1249 example, most widgets clip away the pixels used by child widgets,
1250 and most printers clip away an area near the edges of the paper.
1251 This additional clipping is not reflected by the return value of
1252 clipRegion() or hasClipping().
1254 \section1 Composition Modes
1255 \target Composition Modes
1257 QPainter provides the CompositionMode enum which defines the
1258 Porter-Duff rules for digital image compositing; it describes a
1259 model for combining the pixels in one image, the source, with the
1260 pixels in another image, the destination.
1262 The two most common forms of composition are \l
1263 {QPainter::CompositionMode}{Source} and \l
1264 {QPainter::CompositionMode}{SourceOver}. \l
1265 {QPainter::CompositionMode}{Source} is used to draw opaque objects
1266 onto a paint device. In this mode, each pixel in the source
1267 replaces the corresponding pixel in the destination. In \l
1268 {QPainter::CompositionMode}{SourceOver} composition mode, the
1269 source object is transparent and is drawn on top of the
1272 Note that composition transformation operates pixelwise. For that
1273 reason, there is a difference between using the graphic primitive
1274 itself and its bounding rectangle: The bounding rect contains
1275 pixels with alpha == 0 (i.e the pixels surrounding the
1276 primitive). These pixels will overwrite the other image's pixels,
1277 affectively clearing those, while the primitive only overwrites
1282 \li \inlineimage qpainter-compositiondemo.png
1285 \b {Composition Modes Example}
1287 The \l {painting/composition}{Composition Modes} example, available in
1288 Qt's examples directory, allows you to experiment with the various
1289 composition modes and see the results immediately.
1293 \section1 Limitations
1296 If you are using coordinates with Qt's raster-based paint engine, it is
1297 important to note that, while coordinates greater than +/- 2\sup 15 can
1298 be used, any painting performed with coordinates outside this range is not
1299 guaranteed to be shown; the drawing may be clipped. This is due to the
1300 use of \c{short int} in the implementation.
1302 The outlines generated by Qt's stroker are only an approximation when dealing
1303 with curved shapes. It is in most cases impossible to represent the outline of
1304 a bezier curve segment using another bezier curve segment, and so Qt approximates
1305 the curve outlines by using several smaller curves. For performance reasons there
1306 is a limit to how many curves Qt uses for these outlines, and thus when using
1307 large pen widths or scales the outline error increases. To generate outlines with
1308 smaller errors it is possible to use the QPainterPathStroker class, which has the
1309 setCurveThreshold member function which let's the user specify the error tolerance.
1310 Another workaround is to convert the paths to polygons first and then draw the
1313 \section1 Performance
1315 QPainter is a rich framework that allows developers to do a great
1316 variety of graphical operations, such as gradients, composition
1317 modes and vector graphics. And QPainter can do this across a
1318 variety of different hardware and software stacks. Naturally the
1319 underlying combination of hardware and software has some
1320 implications for performance, and ensuring that every single
1321 operation is fast in combination with all the various combinations
1322 of composition modes, brushes, clipping, transformation, etc, is
1323 close to an impossible task because of the number of
1324 permutations. As a compromise we have selected a subset of the
1325 QPainter API and backends, where performance is guaranteed to be as
1326 good as we can sensibly get it for the given combination of
1327 hardware and software.
1329 The backends we focus on as high-performance engines are:
1333 \li Raster - This backend implements all rendering in pure software
1334 and is always used to render into QImages. For optimal performance
1335 only use the format types QImage::Format_ARGB32_Premultiplied,
1336 QImage::Format_RGB32 or QImage::Format_RGB16. Any other format,
1337 including QImage::Format_ARGB32, has significantly worse
1338 performance. This engine is used by default for QWidget and QPixmap.
1340 \li OpenGL 2.0 (ES) - This backend is the primary backend for
1341 hardware accelerated graphics. It can be run on desktop machines
1342 and embedded devices supporting the OpenGL 2.0 or OpenGL/ES 2.0
1343 specification. This includes most graphics chips produced in the
1344 last couple of years. The engine can be enabled by using QPainter
1345 onto a QOpenGLWidget or by passing \c {-graphicssystem opengl} on the
1346 command line when the underlying system supports it.
1348 \li OpenVG - This backend implements the Khronos standard for 2D
1349 and Vector Graphics. It is primarily for embedded devices with
1350 hardware support for OpenVG. The engine can be enabled by
1351 passing \c {-graphicssystem openvg} on the command line when
1352 the underlying system supports it.
1356 These operations are:
1360 \li Simple transformations, meaning translation and scaling, pluss
1361 0, 90, 180, 270 degree rotations.
1363 \li \c drawPixmap() in combination with simple transformations and
1364 opacity with non-smooth transformation mode
1365 (\c QPainter::SmoothPixmapTransform not enabled as a render hint).
1367 \li Rectangle fills with solid color, two-color linear gradients
1368 and simple transforms.
1370 \li Rectangular clipping with simple transformations and intersect
1373 \li Composition Modes \c QPainter::CompositionMode_Source and
1374 QPainter::CompositionMode_SourceOver
1376 \li Rounded rectangle filling using solid color and two-color
1377 linear gradients fills.
1379 \li 3x3 patched pixmaps, via qDrawBorderPixmap.
1383 This list gives an indication of which features to safely use in
1384 an application where performance is critical. For certain setups,
1385 other operations may be fast too, but before making extensive use
1386 of them, it is recommended to benchmark and verify them on the
1387 system where the software will run in the end. There are also
1388 cases where expensive operations are ok to use, for instance when
1389 the result is cached in a QPixmap.
1391 \sa QPaintDevice, QPaintEngine, {QtSvg Module}, {Basic Drawing Example},
1392 {Drawing Utility Functions}
1396 \enum QPainter::RenderHint
1398 Renderhints are used to specify flags to QPainter that may or
1399 may not be respected by any given engine.
1401 \value Antialiasing Indicates that the engine should antialias
1402 edges of primitives if possible.
1404 \value TextAntialiasing Indicates that the engine should antialias
1405 text if possible. To forcibly disable antialiasing for text, do not
1406 use this hint. Instead, set QFont::NoAntialias on your font's style
1409 \value SmoothPixmapTransform Indicates that the engine should use
1410 a smooth pixmap transformation algorithm (such as bilinear) rather
1411 than nearest neighbor.
1413 \value HighQualityAntialiasing An OpenGL-specific rendering hint
1414 indicating that the engine should use fragment programs and offscreen
1415 rendering for antialiasing.
1417 \value NonCosmeticDefaultPen The engine should interpret pens with a width
1418 of 0 (which otherwise enables QPen::isCosmetic()) as being a non-cosmetic
1419 pen with a width of 1.
1421 \sa renderHints(), setRenderHint(), {QPainter#Rendering
1422 Quality}{Rendering Quality}, {Concentric Circles Example}
1427 Constructs a painter.
1432 QPainter::QPainter()
1433 : d_ptr(new QPainterPrivate(this))
1438 \fn QPainter::QPainter(QPaintDevice *device)
1440 Constructs a painter that begins painting the paint \a device
1443 This constructor is convenient for short-lived painters, e.g. in a
1444 QWidget::paintEvent() and should be used only once. The
1445 constructor calls begin() for you and the QPainter destructor
1446 automatically calls end().
1448 Here's an example using begin() and end():
1449 \snippet code/src_gui_painting_qpainter.cpp 1
1451 The same example using this constructor:
1452 \snippet code/src_gui_painting_qpainter.cpp 2
1454 Since the constructor cannot provide feedback when the initialization
1455 of the painter failed you should rather use begin() and end() to paint
1456 on external devices, e.g. printers.
1461 QPainter::QPainter(QPaintDevice *pd)
1465 if (!QPainterPrivate::attachPainterPrivate(this, pd)) {
1466 d_ptr.reset(new QPainterPrivate(this));
1473 Destroys the painter.
1475 QPainter::~QPainter()
1477 d_ptr->inDestructor = true;
1481 else if (d_ptr->refcount > 1)
1482 d_ptr->detachPainterPrivate(this);
1484 // don't throw anything in the destructor.
1487 // Make sure we haven't messed things up.
1488 Q_ASSERT(d_ptr->inDestructor);
1489 d_ptr->inDestructor = false;
1490 Q_ASSERT(d_ptr->refcount == 1);
1492 free(d_ptr->d_ptrs);
1497 Returns the paint device on which this painter is currently
1498 painting, or 0 if the painter is not active.
1503 QPaintDevice *QPainter::device() const
1505 Q_D(const QPainter);
1506 if (isActive() && d->engine->d_func()->currentClipDevice)
1507 return d->engine->d_func()->currentClipDevice;
1508 return d->original_device;
1512 Returns true if begin() has been called and end() has not yet been
1513 called; otherwise returns false.
1515 \sa begin(), QPaintDevice::paintingActive()
1518 bool QPainter::isActive() const
1520 Q_D(const QPainter);
1525 Initializes the painters pen, background and font to the same as
1526 the given \a paint device.
1530 \sa begin(), {QPainter#Settings}{Settings}
1532 void QPainter::initFrom(const QPaintDevice *device)
1534 Q_ASSERT_X(device, "QPainter::initFrom(const QPaintDevice *device)", "QPaintDevice cannot be 0");
1537 qWarning("QPainter::initFrom: Painter not active, aborted");
1544 d->extended->penChanged();
1545 } else if (d->engine) {
1546 d->engine->setDirty(QPaintEngine::DirtyPen);
1547 d->engine->setDirty(QPaintEngine::DirtyBrush);
1548 d->engine->setDirty(QPaintEngine::DirtyFont);
1554 Saves the current painter state (pushes the state onto a stack). A
1555 save() must be followed by a corresponding restore(); the end()
1556 function unwinds the stack.
1561 void QPainter::save()
1563 #ifdef QT_DEBUG_DRAW
1564 if (qt_show_painter_debug_output)
1565 printf("QPainter::save()\n");
1569 qWarning("QPainter::save: Painter not active");
1574 d->state = d->extended->createState(d->states.back());
1575 d->extended->setState(d->state);
1577 d->updateState(d->state);
1578 d->state = new QPainterState(d->states.back());
1579 d->engine->state = d->state;
1581 d->states.push_back(d->state);
1585 Restores the current painter state (pops a saved state off the
1591 void QPainter::restore()
1593 #ifdef QT_DEBUG_DRAW
1594 if (qt_show_painter_debug_output)
1595 printf("QPainter::restore()\n");
1598 if (d->states.size()<=1) {
1599 qWarning("QPainter::restore: Unbalanced save/restore");
1601 } else if (!d->engine) {
1602 qWarning("QPainter::restore: Painter not active");
1606 QPainterState *tmp = d->state;
1607 d->states.pop_back();
1608 d->state = d->states.back();
1612 d->checkEmulation();
1613 d->extended->setState(d->state);
1618 // trigger clip update if the clip path/region has changed since
1620 if (!d->state->clipInfo.isEmpty()
1621 && (tmp->changeFlags & (QPaintEngine::DirtyClipRegion | QPaintEngine::DirtyClipPath))) {
1622 // reuse the tmp state to avoid any extra allocs...
1623 tmp->dirtyFlags = QPaintEngine::DirtyClipPath;
1624 tmp->clipOperation = Qt::NoClip;
1625 tmp->clipPath = QPainterPath();
1626 d->engine->updateState(*tmp);
1627 // replay the list of clip states,
1628 for (int i=0; i<d->state->clipInfo.size(); ++i) {
1629 const QPainterClipInfo &info = d->state->clipInfo.at(i);
1630 tmp->matrix = info.matrix;
1631 tmp->matrix *= d->state->redirectionMatrix;
1632 tmp->clipOperation = info.operation;
1633 if (info.clipType == QPainterClipInfo::RectClip) {
1634 tmp->dirtyFlags = QPaintEngine::DirtyClipRegion | QPaintEngine::DirtyTransform;
1635 tmp->clipRegion = info.rect;
1636 } else if (info.clipType == QPainterClipInfo::RegionClip) {
1637 tmp->dirtyFlags = QPaintEngine::DirtyClipRegion | QPaintEngine::DirtyTransform;
1638 tmp->clipRegion = info.region;
1639 } else { // clipType == QPainterClipInfo::PathClip
1640 tmp->dirtyFlags = QPaintEngine::DirtyClipPath | QPaintEngine::DirtyTransform;
1641 tmp->clipPath = info.path;
1643 d->engine->updateState(*tmp);
1647 //Since we've updated the clip region anyway, pretend that the clip path hasn't changed:
1648 d->state->dirtyFlags &= ~(QPaintEngine::DirtyClipPath | QPaintEngine::DirtyClipRegion);
1649 tmp->changeFlags &= ~(QPaintEngine::DirtyClipPath | QPaintEngine::DirtyClipRegion);
1650 tmp->changeFlags |= QPaintEngine::DirtyTransform;
1653 d->updateState(d->state);
1660 \fn bool QPainter::begin(QPaintDevice *device)
1662 Begins painting the paint \a device and returns true if
1663 successful; otherwise returns false.
1665 Notice that all painter settings (setPen(), setBrush() etc.) are reset
1666 to default values when begin() is called.
1668 The errors that can occur are serious problems, such as these:
1670 \snippet code/src_gui_painting_qpainter.cpp 3
1672 Note that most of the time, you can use one of the constructors
1673 instead of begin(), and that end() is automatically done at
1676 \warning A paint device can only be painted by one painter at a
1679 \warning Painting on a QImage with the format
1680 QImage::Format_Indexed8 is not supported.
1682 \sa end(), QPainter()
1685 static inline void qt_cleanup_painter_state(QPainterPrivate *d)
1694 bool QPainter::begin(QPaintDevice *pd)
1698 if (pd->painters > 0) {
1699 qWarning("QPainter::begin: A paint device can only be painted by one painter at a time.");
1703 if (d_ptr->engine) {
1704 qWarning("QPainter::begin: Painter already active");
1708 if (QPainterPrivate::attachPainterPrivate(this, pd))
1713 d->helper_device = pd;
1714 d->original_device = pd;
1716 QPoint redirectionOffset;
1717 QPaintDevice *rpd = pd->redirected(&redirectionOffset);
1721 #ifdef QT_DEBUG_DRAW
1722 if (qt_show_painter_debug_output)
1723 printf("QPainter::begin(), device=%p, type=%d\n", pd, pd->devType());
1726 if (pd->devType() == QInternal::Pixmap)
1727 static_cast<QPixmap *>(pd)->detach();
1728 else if (pd->devType() == QInternal::Image)
1729 static_cast<QImage *>(pd)->detach();
1731 d->engine = pd->paintEngine();
1734 qWarning("QPainter::begin: Paint device returned engine == 0, type: %d", pd->devType());
1740 d->extended = d->engine->isExtended() ? static_cast<QPaintEngineEx *>(d->engine) : 0;
1741 if (d->emulationEngine)
1742 d->emulationEngine->real_engine = d->extended;
1744 // Setup new state...
1745 Q_ASSERT(!d->state);
1746 d->state = d->extended ? d->extended->createState(0) : new QPainterState;
1747 d->state->painter = this;
1748 d->states.push_back(d->state);
1750 d->state->redirectionMatrix.translate(-redirectionOffset.x(), -redirectionOffset.y());
1751 d->state->brushOrigin = QPointF();
1753 // Slip a painter state into the engine before we do any other operations
1755 d->extended->setState(d->state);
1757 d->engine->state = d->state;
1759 switch (pd->devType()) {
1760 case QInternal::Pixmap:
1762 QPixmap *pm = static_cast<QPixmap *>(pd);
1765 qWarning("QPainter::begin: Cannot paint on a null pixmap");
1766 qt_cleanup_painter_state(d);
1770 if (pm->depth() == 1) {
1771 d->state->pen = QPen(Qt::color1);
1772 d->state->brush = QBrush(Qt::color0);
1776 case QInternal::Image:
1778 QImage *img = static_cast<QImage *>(pd);
1780 if (img->isNull()) {
1781 qWarning("QPainter::begin: Cannot paint on a null image");
1782 qt_cleanup_painter_state(d);
1784 } else if (img->format() == QImage::Format_Indexed8) {
1785 // Painting on indexed8 images is not supported.
1786 qWarning("QPainter::begin: Cannot paint on an image with the QImage::Format_Indexed8 format");
1787 qt_cleanup_painter_state(d);
1790 if (img->depth() == 1) {
1791 d->state->pen = QPen(Qt::color1);
1792 d->state->brush = QBrush(Qt::color0);
1799 if (d->state->ww == 0) // For compat with 3.x painter defaults
1800 d->state->ww = d->state->wh = d->state->vw = d->state->vh = 1024;
1802 d->engine->setPaintDevice(pd);
1804 bool begun = d->engine->begin(pd);
1806 qWarning("QPainter::begin(): Returned false");
1807 if (d->engine->isActive()) {
1810 qt_cleanup_painter_state(d);
1814 d->engine->setActive(begun);
1817 // Copy painter properties from original paint device,
1818 // required for QPixmap::grabWidget()
1819 if (d->original_device->devType() == QInternal::Widget) {
1820 initFrom(d->original_device);
1822 d->state->layoutDirection = Qt::LayoutDirectionAuto;
1823 // make sure we have a font compatible with the paintdevice
1824 d->state->deviceFont = d->state->font = QFont(d->state->deviceFont, device());
1827 QRect systemRect = d->engine->systemRect();
1828 if (!systemRect.isEmpty()) {
1829 d->state->ww = d->state->vw = systemRect.width();
1830 d->state->wh = d->state->vh = systemRect.height();
1832 d->state->ww = d->state->vw = pd->metric(QPaintDevice::PdmWidth);
1833 d->state->wh = d->state->vh = pd->metric(QPaintDevice::PdmHeight);
1836 const QPoint coordinateOffset = d->engine->coordinateOffset();
1837 d->state->redirectionMatrix.translate(-coordinateOffset.x(), -coordinateOffset.y());
1839 Q_ASSERT(d->engine->isActive());
1841 if (!d->state->redirectionMatrix.isIdentity())
1844 Q_ASSERT(d->engine->isActive());
1845 d->state->renderHints = QPainter::TextAntialiasing;
1846 ++d->device->painters;
1848 d->state->emulationSpecifier = 0;
1854 Ends painting. Any resources used while painting are released. You
1855 don't normally need to call this since it is called by the
1858 Returns true if the painter is no longer active; otherwise returns false.
1860 \sa begin(), isActive()
1863 bool QPainter::end()
1865 #ifdef QT_DEBUG_DRAW
1866 if (qt_show_painter_debug_output)
1867 printf("QPainter::end()\n");
1872 qWarning("QPainter::end: Painter not active, aborted");
1873 qt_cleanup_painter_state(d);
1877 if (d->refcount > 1) {
1878 d->detachPainterPrivate(this);
1884 if (d->engine->isActive()) {
1885 ended = d->engine->end();
1888 --d->device->painters;
1889 if (d->device->painters == 0) {
1890 d->engine->setPaintDevice(0);
1891 d->engine->setActive(false);
1895 if (d->states.size() > 1) {
1896 qWarning("QPainter::end: Painter ended with %d saved states",
1900 if (d->engine->autoDestruct()) {
1904 if (d->emulationEngine) {
1905 delete d->emulationEngine;
1906 d->emulationEngine = 0;
1913 qt_cleanup_painter_state(d);
1920 Returns the paint engine that the painter is currently operating
1921 on if the painter is active; otherwise 0.
1925 QPaintEngine *QPainter::paintEngine() const
1927 Q_D(const QPainter);
1934 Flushes the painting pipeline and prepares for the user issuing commands
1935 directly to the underlying graphics context. Must be followed by a call to
1936 endNativePainting().
1938 Note that only the states the underlying paint engine changes will be reset
1939 to their respective default states. The states we reset may change from
1940 release to release. The following states are currently reset in the OpenGL
1944 \li blending is disabled
1945 \li the depth, stencil and scissor tests are disabled
1946 \li the active texture unit is reset to 0
1947 \li the depth mask, depth function and the clear depth are reset to their
1949 \li the stencil mask, stencil operation and stencil function are reset to
1950 their default values
1951 \li the current color is reset to solid white
1954 If, for example, the OpenGL polygon mode is changed by the user inside a
1955 beginNativePaint()/endNativePainting() block, it will not be reset to the
1956 default state by endNativePainting(). Here is an example that shows
1957 intermixing of painter commands and raw OpenGL commands:
1959 \snippet code/src_gui_painting_qpainter.cpp 21
1961 \sa endNativePainting()
1963 void QPainter::beginNativePainting()
1967 qWarning("QPainter::beginNativePainting: Painter not active");
1972 d->extended->beginNativePainting();
1978 Restores the painter after manually issuing native painting commands. Lets
1979 the painter restore any native state that it relies on before calling any
1980 other painter commands.
1982 \sa beginNativePainting()
1984 void QPainter::endNativePainting()
1986 Q_D(const QPainter);
1988 qWarning("QPainter::beginNativePainting: Painter not active");
1993 d->extended->endNativePainting();
1995 d->engine->syncState();
1999 Returns the font metrics for the painter if the painter is
2000 active. Otherwise, the return value is undefined.
2002 \sa font(), isActive(), {QPainter#Settings}{Settings}
2005 QFontMetrics QPainter::fontMetrics() const
2007 Q_D(const QPainter);
2009 qWarning("QPainter::fontMetrics: Painter not active");
2010 return QFontMetrics(QFont());
2012 return QFontMetrics(d->state->font);
2017 Returns the font info for the painter if the painter is
2018 active. Otherwise, the return value is undefined.
2020 \sa font(), isActive(), {QPainter#Settings}{Settings}
2023 QFontInfo QPainter::fontInfo() const
2025 Q_D(const QPainter);
2027 qWarning("QPainter::fontInfo: Painter not active");
2028 return QFontInfo(QFont());
2030 return QFontInfo(d->state->font);
2036 Returns the opacity of the painter. The default value is
2040 qreal QPainter::opacity() const
2042 Q_D(const QPainter);
2044 qWarning("QPainter::opacity: Painter not active");
2047 return d->state->opacity;
2053 Sets the opacity of the painter to \a opacity. The value should
2054 be in the range 0.0 to 1.0, where 0.0 is fully transparent and
2055 1.0 is fully opaque.
2057 Opacity set on the painter will apply to all drawing operations
2061 void QPainter::setOpacity(qreal opacity)
2066 qWarning("QPainter::setOpacity: Painter not active");
2070 opacity = qMin(qreal(1), qMax(qreal(0), opacity));
2072 if (opacity == d->state->opacity)
2075 d->state->opacity = opacity;
2078 d->extended->opacityChanged();
2080 d->state->dirtyFlags |= QPaintEngine::DirtyOpacity;
2085 Returns the currently set brush origin.
2087 \sa setBrushOrigin(), {QPainter#Settings}{Settings}
2090 QPoint QPainter::brushOrigin() const
2092 Q_D(const QPainter);
2094 qWarning("QPainter::brushOrigin: Painter not active");
2097 return QPointF(d->state->brushOrigin).toPoint();
2101 \fn void QPainter::setBrushOrigin(const QPointF &position)
2103 Sets the brush origin to \a position.
2105 The brush origin specifies the (0, 0) coordinate of the painter's
2108 Note that while the brushOrigin() was necessary to adopt the
2109 parent's background for a widget in Qt 3, this is no longer the
2110 case since the Qt 4 painter doesn't paint the background unless
2111 you explicitly tell it to do so by setting the widget's \l
2112 {QWidget::autoFillBackground}{autoFillBackground} property to
2115 \sa brushOrigin(), {QPainter#Settings}{Settings}
2118 void QPainter::setBrushOrigin(const QPointF &p)
2121 #ifdef QT_DEBUG_DRAW
2122 if (qt_show_painter_debug_output)
2123 printf("QPainter::setBrushOrigin(), (%.2f,%.2f)\n", p.x(), p.y());
2127 qWarning("QPainter::setBrushOrigin: Painter not active");
2131 d->state->brushOrigin = p;
2134 d->extended->brushOriginChanged();
2138 d->state->dirtyFlags |= QPaintEngine::DirtyBrushOrigin;
2142 \fn void QPainter::setBrushOrigin(const QPoint &position)
2145 Sets the brush's origin to the given \a position.
2149 \fn void QPainter::setBrushOrigin(int x, int y)
2153 Sets the brush's origin to point (\a x, \a y).
2157 \enum QPainter::CompositionMode
2159 Defines the modes supported for digital image compositing.
2160 Composition modes are used to specify how the pixels in one image,
2161 the source, are merged with the pixel in another image, the
2164 Please note that the bitwise raster operation modes, denoted with
2165 a RasterOp prefix, are only natively supported in the X11 and
2166 raster paint engines. This means that the only way to utilize
2167 these modes on the Mac is via a QImage. The RasterOp denoted blend
2168 modes are \e not supported for pens and brushes with alpha
2169 components. Also, turning on the QPainter::Antialiasing render
2170 hint will effectively disable the RasterOp modes.
2173 \image qpainter-compositionmode1.png
2174 \image qpainter-compositionmode2.png
2176 The most common type is SourceOver (often referred to as just
2177 alpha blending) where the source pixel is blended on top of the
2178 destination pixel in such a way that the alpha component of the
2179 source defines the translucency of the pixel.
2181 When the paint device is a QImage, the image format must be set to
2182 \l {QImage::Format}{Format_ARGB32_Premultiplied} or
2183 \l {QImage::Format}{Format_ARGB32} for the composition modes to have
2184 any effect. For performance the premultiplied version is the preferred
2187 When a composition mode is set it applies to all painting
2188 operator, pens, brushes, gradients and pixmap/image drawing.
2190 \value CompositionMode_SourceOver This is the default mode. The
2191 alpha of the source is used to blend the pixel on top of the
2194 \value CompositionMode_DestinationOver The alpha of the
2195 destination is used to blend it on top of the source pixels. This
2196 mode is the inverse of CompositionMode_SourceOver.
2198 \value CompositionMode_Clear The pixels in the destination are
2199 cleared (set to fully transparent) independent of the source.
2201 \value CompositionMode_Source The output is the source
2202 pixel. (This means a basic copy operation and is identical to
2203 SourceOver when the source pixel is opaque).
2205 \value CompositionMode_Destination The output is the destination
2206 pixel. This means that the blending has no effect. This mode is
2207 the inverse of CompositionMode_Source.
2209 \value CompositionMode_SourceIn The output is the source, where
2210 the alpha is reduced by that of the destination.
2212 \value CompositionMode_DestinationIn The output is the
2213 destination, where the alpha is reduced by that of the
2214 source. This mode is the inverse of CompositionMode_SourceIn.
2216 \value CompositionMode_SourceOut The output is the source, where
2217 the alpha is reduced by the inverse of destination.
2219 \value CompositionMode_DestinationOut The output is the
2220 destination, where the alpha is reduced by the inverse of the
2221 source. This mode is the inverse of CompositionMode_SourceOut.
2223 \value CompositionMode_SourceAtop The source pixel is blended on
2224 top of the destination, with the alpha of the source pixel reduced
2225 by the alpha of the destination pixel.
2227 \value CompositionMode_DestinationAtop The destination pixel is
2228 blended on top of the source, with the alpha of the destination
2229 pixel is reduced by the alpha of the destination pixel. This mode
2230 is the inverse of CompositionMode_SourceAtop.
2232 \value CompositionMode_Xor The source, whose alpha is reduced with
2233 the inverse of the destination alpha, is merged with the
2234 destination, whose alpha is reduced by the inverse of the source
2235 alpha. CompositionMode_Xor is not the same as the bitwise Xor.
2237 \value CompositionMode_Plus Both the alpha and color of the source
2238 and destination pixels are added together.
2240 \value CompositionMode_Multiply The output is the source color
2241 multiplied by the destination. Multiplying a color with white
2242 leaves the color unchanged, while multiplying a color
2243 with black produces black.
2245 \value CompositionMode_Screen The source and destination colors
2246 are inverted and then multiplied. Screening a color with white
2247 produces white, whereas screening a color with black leaves the
2250 \value CompositionMode_Overlay Multiplies or screens the colors
2251 depending on the destination color. The destination color is mixed
2252 with the source color to reflect the lightness or darkness of the
2255 \value CompositionMode_Darken The darker of the source and
2256 destination colors is selected.
2258 \value CompositionMode_Lighten The lighter of the source and
2259 destination colors is selected.
2261 \value CompositionMode_ColorDodge The destination color is
2262 brightened to reflect the source color. A black source color
2263 leaves the destination color unchanged.
2265 \value CompositionMode_ColorBurn The destination color is darkened
2266 to reflect the source color. A white source color leaves the
2267 destination color unchanged.
2269 \value CompositionMode_HardLight Multiplies or screens the colors
2270 depending on the source color. A light source color will lighten
2271 the destination color, whereas a dark source color will darken the
2274 \value CompositionMode_SoftLight Darkens or lightens the colors
2275 depending on the source color. Similar to
2276 CompositionMode_HardLight.
2278 \value CompositionMode_Difference Subtracts the darker of the
2279 colors from the lighter. Painting with white inverts the
2280 destination color, whereas painting with black leaves the
2281 destination color unchanged.
2283 \value CompositionMode_Exclusion Similar to
2284 CompositionMode_Difference, but with a lower contrast. Painting
2285 with white inverts the destination color, whereas painting with
2286 black leaves the destination color unchanged.
2288 \value RasterOp_SourceOrDestination Does a bitwise OR operation on
2289 the source and destination pixels (src OR dst).
2291 \value RasterOp_SourceAndDestination Does a bitwise AND operation
2292 on the source and destination pixels (src AND dst).
2294 \value RasterOp_SourceXorDestination Does a bitwise XOR operation
2295 on the source and destination pixels (src XOR dst).
2297 \value RasterOp_NotSourceAndNotDestination Does a bitwise NOR
2298 operation on the source and destination pixels ((NOT src) AND (NOT
2301 \value RasterOp_NotSourceOrNotDestination Does a bitwise NAND
2302 operation on the source and destination pixels ((NOT src) OR (NOT
2305 \value RasterOp_NotSourceXorDestination Does a bitwise operation
2306 where the source pixels are inverted and then XOR'ed with the
2307 destination ((NOT src) XOR dst).
2309 \value RasterOp_NotSource Does a bitwise operation where the
2310 source pixels are inverted (NOT src).
2312 \value RasterOp_NotSourceAndDestination Does a bitwise operation
2313 where the source is inverted and then AND'ed with the destination
2314 ((NOT src) AND dst).
2316 \value RasterOp_SourceAndNotDestination Does a bitwise operation
2317 where the source is AND'ed with the inverted destination pixels
2318 (src AND (NOT dst)).
2320 \sa compositionMode(), setCompositionMode(), {QPainter#Composition
2321 Modes}{Composition Modes}, {Image Composition Example}
2325 Sets the composition mode to the given \a mode.
2327 \warning Only a QPainter operating on a QImage fully supports all
2328 composition modes. The RasterOp modes are supported for X11 as
2329 described in compositionMode().
2331 \sa compositionMode()
2333 void QPainter::setCompositionMode(CompositionMode mode)
2337 qWarning("QPainter::setCompositionMode: Painter not active");
2340 if (d->state->composition_mode == mode)
2343 d->state->composition_mode = mode;
2344 d->extended->compositionModeChanged();
2348 if (mode >= QPainter::RasterOp_SourceOrDestination) {
2349 if (!d->engine->hasFeature(QPaintEngine::RasterOpModes)) {
2350 qWarning("QPainter::setCompositionMode: "
2351 "Raster operation modes not supported on device");
2354 } else if (mode >= QPainter::CompositionMode_Plus) {
2355 if (!d->engine->hasFeature(QPaintEngine::BlendModes)) {
2356 qWarning("QPainter::setCompositionMode: "
2357 "Blend modes not supported on device");
2360 } else if (!d->engine->hasFeature(QPaintEngine::PorterDuff)) {
2361 if (mode != CompositionMode_Source && mode != CompositionMode_SourceOver) {
2362 qWarning("QPainter::setCompositionMode: "
2363 "PorterDuff modes not supported on device");
2368 d->state->composition_mode = mode;
2369 d->state->dirtyFlags |= QPaintEngine::DirtyCompositionMode;
2373 Returns the current composition mode.
2375 \sa CompositionMode, setCompositionMode()
2377 QPainter::CompositionMode QPainter::compositionMode() const
2379 Q_D(const QPainter);
2381 qWarning("QPainter::compositionMode: Painter not active");
2382 return QPainter::CompositionMode_SourceOver;
2384 return d->state->composition_mode;
2388 Returns the current background brush.
2390 \sa setBackground(), {QPainter#Settings}{Settings}
2393 const QBrush &QPainter::background() const
2395 Q_D(const QPainter);
2397 qWarning("QPainter::background: Painter not active");
2398 return d->fakeState()->brush;
2400 return d->state->bgBrush;
2405 Returns true if clipping has been set; otherwise returns false.
2407 \sa setClipping(), {QPainter#Clipping}{Clipping}
2410 bool QPainter::hasClipping() const
2412 Q_D(const QPainter);
2414 qWarning("QPainter::hasClipping: Painter not active");
2417 return d->state->clipEnabled && d->state->clipOperation != Qt::NoClip;
2422 Enables clipping if \a enable is true, or disables clipping if \a
2425 \sa hasClipping(), {QPainter#Clipping}{Clipping}
2428 void QPainter::setClipping(bool enable)
2431 #ifdef QT_DEBUG_DRAW
2432 if (qt_show_painter_debug_output)
2433 printf("QPainter::setClipping(), enable=%s, was=%s\n",
2434 enable ? "on" : "off",
2435 hasClipping() ? "on" : "off");
2438 qWarning("QPainter::setClipping: Painter not active, state will be reset by begin");
2442 if (hasClipping() == enable)
2445 // we can't enable clipping if we don't have a clip
2447 && (d->state->clipInfo.isEmpty() || d->state->clipInfo.last().operation == Qt::NoClip))
2449 d->state->clipEnabled = enable;
2452 d->extended->clipEnabledChanged();
2456 d->state->dirtyFlags |= QPaintEngine::DirtyClipEnabled;
2457 d->updateState(d->state);
2462 Returns the currently set clip region. Note that the clip region
2463 is given in logical coordinates.
2465 \warning QPainter does not store the combined clip explicitly as
2466 this is handled by the underlying QPaintEngine, so the path is
2467 recreated on demand and transformed to the current logical
2468 coordinate system. This is potentially an expensive operation.
2470 \sa setClipRegion(), clipPath(), setClipping()
2473 QRegion QPainter::clipRegion() const
2475 Q_D(const QPainter);
2477 qWarning("QPainter::clipRegion: Painter not active");
2482 bool lastWasNothing = true;
2485 const_cast<QPainter *>(this)->d_ptr->updateInvMatrix();
2487 // ### Falcon: Use QPainterPath
2488 for (int i=0; i<d->state->clipInfo.size(); ++i) {
2489 const QPainterClipInfo &info = d->state->clipInfo.at(i);
2490 switch (info.clipType) {
2492 case QPainterClipInfo::RegionClip: {
2493 QTransform matrix = (info.matrix * d->invMatrix);
2494 if (lastWasNothing) {
2495 region = info.region * matrix;
2496 lastWasNothing = false;
2499 if (info.operation == Qt::IntersectClip)
2500 region &= info.region * matrix;
2501 else if (info.operation == Qt::NoClip) {
2502 lastWasNothing = true;
2505 region = info.region * matrix;
2509 case QPainterClipInfo::PathClip: {
2510 QTransform matrix = (info.matrix * d->invMatrix);
2511 if (lastWasNothing) {
2512 region = QRegion((info.path * matrix).toFillPolygon().toPolygon(),
2513 info.path.fillRule());
2514 lastWasNothing = false;
2517 if (info.operation == Qt::IntersectClip) {
2518 region &= QRegion((info.path * matrix).toFillPolygon().toPolygon(),
2519 info.path.fillRule());
2520 } else if (info.operation == Qt::NoClip) {
2521 lastWasNothing = true;
2524 region = QRegion((info.path * matrix).toFillPolygon().toPolygon(),
2525 info.path.fillRule());
2530 case QPainterClipInfo::RectClip: {
2531 QTransform matrix = (info.matrix * d->invMatrix);
2532 if (lastWasNothing) {
2533 region = QRegion(info.rect) * matrix;
2534 lastWasNothing = false;
2537 if (info.operation == Qt::IntersectClip) {
2538 // Use rect intersection if possible.
2539 if (matrix.type() <= QTransform::TxScale)
2540 region &= matrix.mapRect(info.rect);
2542 region &= matrix.map(QRegion(info.rect));
2543 } else if (info.operation == Qt::NoClip) {
2544 lastWasNothing = true;
2547 region = QRegion(info.rect) * matrix;
2552 case QPainterClipInfo::RectFClip: {
2553 QTransform matrix = (info.matrix * d->invMatrix);
2554 if (lastWasNothing) {
2555 region = QRegion(info.rectf.toRect()) * matrix;
2556 lastWasNothing = false;
2559 if (info.operation == Qt::IntersectClip) {
2560 // Use rect intersection if possible.
2561 if (matrix.type() <= QTransform::TxScale)
2562 region &= matrix.mapRect(info.rectf.toRect());
2564 region &= matrix.map(QRegion(info.rectf.toRect()));
2565 } else if (info.operation == Qt::NoClip) {
2566 lastWasNothing = true;
2569 region = QRegion(info.rectf.toRect()) * matrix;
2579 extern QPainterPath qt_regionToPath(const QRegion ®ion);
2582 Returns the currently clip as a path. Note that the clip path is
2583 given in logical coordinates.
2585 \warning QPainter does not store the combined clip explicitly as
2586 this is handled by the underlying QPaintEngine, so the path is
2587 recreated on demand and transformed to the current logical
2588 coordinate system. This is potentially an expensive operation.
2590 \sa setClipPath(), clipRegion(), setClipping()
2592 QPainterPath QPainter::clipPath() const
2594 Q_D(const QPainter);
2596 // ### Since we do not support path intersections and path unions yet,
2597 // we just use clipRegion() here...
2599 qWarning("QPainter::clipPath: Painter not active");
2600 return QPainterPath();
2603 // No clip, return empty
2604 if (d->state->clipInfo.size() == 0) {
2605 return QPainterPath();
2608 // Update inverse matrix, used below.
2610 const_cast<QPainter *>(this)->d_ptr->updateInvMatrix();
2612 // For the simple case avoid conversion.
2613 if (d->state->clipInfo.size() == 1
2614 && d->state->clipInfo.at(0).clipType == QPainterClipInfo::PathClip) {
2615 QTransform matrix = (d->state->clipInfo.at(0).matrix * d->invMatrix);
2616 return d->state->clipInfo.at(0).path * matrix;
2618 } else if (d->state->clipInfo.size() == 1
2619 && d->state->clipInfo.at(0).clipType == QPainterClipInfo::RectClip) {
2620 QTransform matrix = (d->state->clipInfo.at(0).matrix * d->invMatrix);
2622 path.addRect(d->state->clipInfo.at(0).rect);
2623 return path * matrix;
2625 // Fallback to clipRegion() for now, since we don't have isect/unite for paths
2626 return qt_regionToPath(clipRegion());
2632 Returns the bounding rectangle of the current clip if there is a clip;
2633 otherwise returns an empty rectangle. Note that the clip region is
2634 given in logical coordinates.
2636 The bounding rectangle is not guaranteed to be tight.
2638 \sa setClipRect(), setClipPath(), setClipRegion()
2643 QRectF QPainter::clipBoundingRect() const
2645 Q_D(const QPainter);
2648 qWarning("QPainter::clipBoundingRect: Painter not active");
2652 // Accumulate the bounding box in device space. This is not 100%
2653 // precise, but it fits within the guarantee and it is reasonably
2656 for (int i=0; i<d->state->clipInfo.size(); ++i) {
2658 const QPainterClipInfo &info = d->state->clipInfo.at(i);
2660 if (info.clipType == QPainterClipInfo::RectClip)
2662 else if (info.clipType == QPainterClipInfo::RectFClip)
2664 else if (info.clipType == QPainterClipInfo::RegionClip)
2665 r = info.region.boundingRect();
2667 r = info.path.boundingRect();
2669 r = info.matrix.mapRect(r);
2673 else if (info.operation == Qt::IntersectClip)
2678 // Map the rectangle back into logical space using the inverse
2681 const_cast<QPainter *>(this)->d_ptr->updateInvMatrix();
2683 return d->invMatrix.mapRect(bounds);
2687 \fn void QPainter::setClipRect(const QRectF &rectangle, Qt::ClipOperation operation)
2689 Enables clipping, and sets the clip region to the given \a
2690 rectangle using the given clip \a operation. The default operation
2691 is to replace the current clip rectangle.
2693 Note that the clip rectangle is specified in logical (painter)
2696 \sa clipRegion(), setClipping(), {QPainter#Clipping}{Clipping}
2698 void QPainter::setClipRect(const QRectF &rect, Qt::ClipOperation op)
2703 if ((!d->state->clipEnabled && op != Qt::NoClip))
2704 op = Qt::ReplaceClip;
2707 qWarning("QPainter::setClipRect: Painter not active");
2710 qreal right = rect.x() + rect.width();
2711 qreal bottom = rect.y() + rect.height();
2712 qreal pts[] = { rect.x(), rect.y(),
2716 QVectorPath vp(pts, 4, 0, QVectorPath::RectangleHint);
2717 d->state->clipEnabled = true;
2718 d->extended->clip(vp, op);
2719 if (op == Qt::ReplaceClip || op == Qt::NoClip)
2720 d->state->clipInfo.clear();
2721 d->state->clipInfo << QPainterClipInfo(rect, op, d->state->matrix);
2722 d->state->clipOperation = op;
2726 if (qreal(int(rect.top())) == rect.top()
2727 && qreal(int(rect.bottom())) == rect.bottom()
2728 && qreal(int(rect.left())) == rect.left()
2729 && qreal(int(rect.right())) == rect.right())
2731 setClipRect(rect.toRect(), op);
2735 if (rect.isEmpty()) {
2736 setClipRegion(QRegion(), op);
2742 setClipPath(path, op);
2746 \fn void QPainter::setClipRect(const QRect &rectangle, Qt::ClipOperation operation)
2749 Enables clipping, and sets the clip region to the given \a rectangle using the given
2752 void QPainter::setClipRect(const QRect &rect, Qt::ClipOperation op)
2757 qWarning("QPainter::setClipRect: Painter not active");
2761 if ((!d->state->clipEnabled && op != Qt::NoClip))
2762 op = Qt::ReplaceClip;
2765 d->state->clipEnabled = true;
2766 d->extended->clip(rect, op);
2767 if (op == Qt::ReplaceClip || op == Qt::NoClip)
2768 d->state->clipInfo.clear();
2769 d->state->clipInfo << QPainterClipInfo(rect, op, d->state->matrix);
2770 d->state->clipOperation = op;
2774 if (d->state->clipOperation == Qt::NoClip && op == Qt::IntersectClip)
2775 op = Qt::ReplaceClip;
2777 d->state->clipRegion = rect;
2778 d->state->clipOperation = op;
2779 if (op == Qt::NoClip || op == Qt::ReplaceClip)
2780 d->state->clipInfo.clear();
2781 d->state->clipInfo << QPainterClipInfo(rect, op, d->state->matrix);
2782 d->state->clipEnabled = true;
2783 d->state->dirtyFlags |= QPaintEngine::DirtyClipRegion | QPaintEngine::DirtyClipEnabled;
2784 d->updateState(d->state);
2788 \fn void QPainter::setClipRect(int x, int y, int width, int height, Qt::ClipOperation operation)
2790 Enables clipping, and sets the clip region to the rectangle beginning at (\a x, \a y)
2791 with the given \a width and \a height.
2795 \fn void QPainter::setClipRegion(const QRegion ®ion, Qt::ClipOperation operation)
2797 Sets the clip region to the given \a region using the specified clip
2798 \a operation. The default clip operation is to replace the current
2801 Note that the clip region is given in logical coordinates.
2803 \sa clipRegion(), setClipRect(), {QPainter#Clipping}{Clipping}
2805 void QPainter::setClipRegion(const QRegion &r, Qt::ClipOperation op)
2808 #ifdef QT_DEBUG_DRAW
2809 QRect rect = r.boundingRect();
2810 if (qt_show_painter_debug_output)
2811 printf("QPainter::setClipRegion(), size=%d, [%d,%d,%d,%d]\n",
2812 r.rects().size(), rect.x(), rect.y(), rect.width(), rect.height());
2815 qWarning("QPainter::setClipRegion: Painter not active");
2819 if ((!d->state->clipEnabled && op != Qt::NoClip))
2820 op = Qt::ReplaceClip;
2823 d->state->clipEnabled = true;
2824 d->extended->clip(r, op);
2825 if (op == Qt::NoClip || op == Qt::ReplaceClip)
2826 d->state->clipInfo.clear();
2827 d->state->clipInfo << QPainterClipInfo(r, op, d->state->matrix);
2828 d->state->clipOperation = op;
2832 if (d->state->clipOperation == Qt::NoClip && op == Qt::IntersectClip)
2833 op = Qt::ReplaceClip;
2835 d->state->clipRegion = r;
2836 d->state->clipOperation = op;
2837 if (op == Qt::NoClip || op == Qt::ReplaceClip)
2838 d->state->clipInfo.clear();
2839 d->state->clipInfo << QPainterClipInfo(r, op, d->state->matrix);
2840 d->state->clipEnabled = true;
2841 d->state->dirtyFlags |= QPaintEngine::DirtyClipRegion | QPaintEngine::DirtyClipEnabled;
2842 d->updateState(d->state);
2849 Sets the transformation matrix to \a matrix and enables transformations.
2851 \note It is advisable to use setWorldTransform() instead of this function to
2852 preserve the properties of perspective transformations.
2854 If \a combine is true, then \a matrix is combined with the current
2855 transformation matrix; otherwise \a matrix replaces the current
2856 transformation matrix.
2858 If \a matrix is the identity matrix and \a combine is false, this
2859 function calls setWorldMatrixEnabled(false). (The identity matrix is the
2860 matrix where QMatrix::m11() and QMatrix::m22() are 1.0 and the
2863 The following functions can transform the coordinate system without using
2872 They operate on the painter's worldMatrix() and are implemented like this:
2874 \snippet code/src_gui_painting_qpainter.cpp 4
2876 Note that when using setWorldMatrix() function you should always have
2877 \a combine be true when you are drawing into a QPicture. Otherwise
2878 it may not be possible to replay the picture with additional
2879 transformations; using the translate(), scale(), etc. convenience
2882 For more information about the coordinate system, transformations
2883 and window-viewport conversion, see \l {Coordinate System}.
2885 \sa setWorldTransform(), QTransform
2888 void QPainter::setWorldMatrix(const QMatrix &matrix, bool combine)
2890 setWorldTransform(QTransform(matrix), combine);
2897 Returns the world transformation matrix.
2899 It is advisable to use worldTransform() because worldMatrix() does not
2900 preserve the properties of perspective transformations.
2902 \sa {QPainter#Coordinate Transformations}{Coordinate Transformations},
2906 const QMatrix &QPainter::worldMatrix() const
2908 Q_D(const QPainter);
2910 qWarning("QPainter::worldMatrix: Painter not active");
2911 return d->fakeState()->transform.toAffine();
2913 return d->state->worldMatrix.toAffine();
2919 Use setWorldTransform() instead.
2921 \sa setWorldTransform()
2924 void QPainter::setMatrix(const QMatrix &matrix, bool combine)
2926 setWorldTransform(QTransform(matrix), combine);
2932 Use worldTransform() instead.
2934 \sa worldTransform()
2937 const QMatrix &QPainter::matrix() const
2939 return worldMatrix();
2947 Returns the transformation matrix combining the current
2948 window/viewport and world transformation.
2950 It is advisable to use combinedTransform() instead of this
2951 function to preserve the properties of perspective transformations.
2953 \sa setWorldTransform(), setWindow(), setViewport()
2955 QMatrix QPainter::combinedMatrix() const
2957 return combinedTransform().toAffine();
2964 Returns the matrix that transforms from logical coordinates to
2965 device coordinates of the platform dependent paint device.
2967 \note It is advisable to use deviceTransform() instead of this
2968 function to preserve the properties of perspective transformations.
2970 This function is \e only needed when using platform painting
2971 commands on the platform dependent handle (Qt::HANDLE), and the
2972 platform does not do transformations nativly.
2974 The QPaintEngine::PaintEngineFeature enum can be queried to
2975 determine whether the platform performs the transformations or
2978 \sa worldMatrix(), QPaintEngine::hasFeature(),
2980 const QMatrix &QPainter::deviceMatrix() const
2982 Q_D(const QPainter);
2984 qWarning("QPainter::deviceMatrix: Painter not active");
2985 return d->fakeState()->transform.toAffine();
2987 return d->state->matrix.toAffine();
2993 Resets any transformations that were made using translate(), scale(),
2994 shear(), rotate(), setWorldMatrix(), setViewport() and
2997 It is advisable to use resetTransform() instead of this function
2998 to preserve the properties of perspective transformations.
3000 \sa {QPainter#Coordinate Transformations}{Coordinate
3004 void QPainter::resetMatrix()
3013 Enables transformations if \a enable is true, or disables
3014 transformations if \a enable is false. The world transformation
3015 matrix is not changed.
3017 \sa worldMatrixEnabled(), worldTransform(), {QPainter#Coordinate
3018 Transformations}{Coordinate Transformations}
3021 void QPainter::setWorldMatrixEnabled(bool enable)
3024 #ifdef QT_DEBUG_DRAW
3025 if (qt_show_painter_debug_output)
3026 printf("QPainter::setMatrixEnabled(), enable=%d\n", enable);
3030 qWarning("QPainter::setMatrixEnabled: Painter not active");
3033 if (enable == d->state->WxF)
3036 d->state->WxF = enable;
3043 Returns true if world transformation is enabled; otherwise returns
3046 \sa setWorldMatrixEnabled(), worldTransform(), {Coordinate System}
3049 bool QPainter::worldMatrixEnabled() const
3051 Q_D(const QPainter);
3053 qWarning("QPainter::worldMatrixEnabled: Painter not active");
3056 return d->state->WxF;
3062 Use setWorldMatrixEnabled() instead.
3064 \sa setWorldMatrixEnabled()
3067 void QPainter::setMatrixEnabled(bool enable)
3069 setWorldMatrixEnabled(enable);
3075 Use worldMatrixEnabled() instead
3077 \sa worldMatrixEnabled()
3080 bool QPainter::matrixEnabled() const
3082 return worldMatrixEnabled();
3086 Scales the coordinate system by (\a{sx}, \a{sy}).
3088 \sa setWorldTransform(), {QPainter#Coordinate Transformations}{Coordinate Transformations}
3091 void QPainter::scale(qreal sx, qreal sy)
3093 #ifdef QT_DEBUG_DRAW
3094 if (qt_show_painter_debug_output)
3095 printf("QPainter::scale(), sx=%f, sy=%f\n", sx, sy);
3099 qWarning("QPainter::scale: Painter not active");
3103 d->state->worldMatrix.scale(sx,sy);
3104 d->state->WxF = true;
3109 Shears the coordinate system by (\a{sh}, \a{sv}).
3111 \sa setWorldTransform(), {QPainter#Coordinate Transformations}{Coordinate Transformations}
3114 void QPainter::shear(qreal sh, qreal sv)
3116 #ifdef QT_DEBUG_DRAW
3117 if (qt_show_painter_debug_output)
3118 printf("QPainter::shear(), sh=%f, sv=%f\n", sh, sv);
3122 qWarning("QPainter::shear: Painter not active");
3126 d->state->worldMatrix.shear(sh, sv);
3127 d->state->WxF = true;
3132 \fn void QPainter::rotate(qreal angle)
3134 Rotates the coordinate system the given \a angle clockwise.
3136 \sa setWorldTransform(), {QPainter#Coordinate Transformations}{Coordinate Transformations}
3139 void QPainter::rotate(qreal a)
3141 #ifdef QT_DEBUG_DRAW
3142 if (qt_show_painter_debug_output)
3143 printf("QPainter::rotate(), angle=%f\n", a);
3147 qWarning("QPainter::rotate: Painter not active");
3151 d->state->worldMatrix.rotate(a);
3152 d->state->WxF = true;
3157 Translates the coordinate system by the given \a offset; i.e. the
3158 given \a offset is added to points.
3160 \sa setWorldTransform(), {QPainter#Coordinate Transformations}{Coordinate Transformations}
3162 void QPainter::translate(const QPointF &offset)
3164 qreal dx = offset.x();
3165 qreal dy = offset.y();
3166 #ifdef QT_DEBUG_DRAW
3167 if (qt_show_painter_debug_output)
3168 printf("QPainter::translate(), dx=%f, dy=%f\n", dx, dy);
3172 qWarning("QPainter::translate: Painter not active");
3176 d->state->worldMatrix.translate(dx, dy);
3177 d->state->WxF = true;
3182 \fn void QPainter::translate(const QPoint &offset)
3185 Translates the coordinate system by the given \a offset.
3189 \fn void QPainter::translate(qreal dx, qreal dy)
3192 Translates the coordinate system by the vector (\a dx, \a dy).
3196 \fn void QPainter::setClipPath(const QPainterPath &path, Qt::ClipOperation operation)
3198 Enables clipping, and sets the clip path for the painter to the
3199 given \a path, with the clip \a operation.
3201 Note that the clip path is specified in logical (painter)
3204 \sa clipPath(), clipRegion(), {QPainter#Clipping}{Clipping}
3207 void QPainter::setClipPath(const QPainterPath &path, Qt::ClipOperation op)
3209 #ifdef QT_DEBUG_DRAW
3210 if (qt_show_painter_debug_output) {
3211 QRectF b = path.boundingRect();
3212 printf("QPainter::setClipPath(), size=%d, op=%d, bounds=[%.2f,%.2f,%.2f,%.2f]\n",
3213 path.elementCount(), op, b.x(), b.y(), b.width(), b.height());
3219 qWarning("QPainter::setClipPath: Painter not active");
3223 if ((!d->state->clipEnabled && op != Qt::NoClip))
3224 op = Qt::ReplaceClip;
3227 d->state->clipEnabled = true;
3228 d->extended->clip(path, op);
3229 if (op == Qt::NoClip || op == Qt::ReplaceClip)
3230 d->state->clipInfo.clear();
3231 d->state->clipInfo << QPainterClipInfo(path, op, d->state->matrix);
3232 d->state->clipOperation = op;
3236 if (d->state->clipOperation == Qt::NoClip && op == Qt::IntersectClip)
3237 op = Qt::ReplaceClip;
3239 d->state->clipPath = path;
3240 d->state->clipOperation = op;
3241 if (op == Qt::NoClip || op == Qt::ReplaceClip)
3242 d->state->clipInfo.clear();
3243 d->state->clipInfo << QPainterClipInfo(path, op, d->state->matrix);
3244 d->state->clipEnabled = true;
3245 d->state->dirtyFlags |= QPaintEngine::DirtyClipPath | QPaintEngine::DirtyClipEnabled;
3246 d->updateState(d->state);
3250 Draws the outline (strokes) the path \a path with the pen specified
3253 \sa fillPath(), {QPainter#Drawing}{Drawing}
3255 void QPainter::strokePath(const QPainterPath &path, const QPen &pen)
3260 qWarning("QPainter::strokePath: Painter not active");
3268 const QGradient *g = qpen_brush(pen).gradient();
3269 if (!g || g->coordinateMode() == QGradient::LogicalMode) {
3270 d->extended->stroke(qtVectorPathForPath(path), pen);
3275 QBrush oldBrush = d->state->brush;
3276 QPen oldPen = d->state->pen;
3279 setBrush(Qt::NoBrush);
3289 Fills the given \a path using the given \a brush. The outline is
3292 Alternatively, you can specify a QColor instead of a QBrush; the
3293 QBrush constructor (taking a QColor argument) will automatically
3294 create a solid pattern brush.
3298 void QPainter::fillPath(const QPainterPath &path, const QBrush &brush)
3303 qWarning("QPainter::fillPath: Painter not active");
3311 const QGradient *g = brush.gradient();
3312 if (!g || g->coordinateMode() == QGradient::LogicalMode) {
3313 d->extended->fill(qtVectorPathForPath(path), brush);
3318 QBrush oldBrush = d->state->brush;
3319 QPen oldPen = d->state->pen;
3332 Draws the given painter \a path using the current pen for outline
3333 and the current brush for filling.
3337 \li \inlineimage qpainter-path.png
3339 \snippet code/src_gui_painting_qpainter.cpp 5
3342 \sa {painting/painterpaths}{the Painter Paths
3343 example},{painting/deform}{the Vector Deformation example}
3345 void QPainter::drawPath(const QPainterPath &path)
3347 #ifdef QT_DEBUG_DRAW
3348 QRectF pathBounds = path.boundingRect();
3349 if (qt_show_painter_debug_output)
3350 printf("QPainter::drawPath(), size=%d, [%.2f,%.2f,%.2f,%.2f]\n",
3351 path.elementCount(),
3352 pathBounds.x(), pathBounds.y(), pathBounds.width(), pathBounds.height());
3358 qWarning("QPainter::drawPath: Painter not active");
3363 d->extended->drawPath(path);
3366 d->updateState(d->state);
3368 if (d->engine->hasFeature(QPaintEngine::PainterPaths) && d->state->emulationSpecifier == 0) {
3369 d->engine->drawPath(path);
3371 d->draw_helper(path);
3376 \fn void QPainter::drawLine(const QLineF &line)
3378 Draws a line defined by \a line.
3382 \li \inlineimage qpainter-line.png
3384 \snippet code/src_gui_painting_qpainter.cpp 6
3387 \sa drawLines(), drawPolyline(), {Coordinate System}
3391 \fn void QPainter::drawLine(const QLine &line)
3394 Draws a line defined by \a line.
3398 \fn void QPainter::drawLine(const QPoint &p1, const QPoint &p2)
3401 Draws a line from \a p1 to \a p2.
3405 \fn void QPainter::drawLine(const QPointF &p1, const QPointF &p2)
3408 Draws a line from \a p1 to \a p2.
3412 \fn void QPainter::drawLine(int x1, int y1, int x2, int y2)
3415 Draws a line from (\a x1, \a y1) to (\a x2, \a y2) and sets the
3416 current pen position to (\a x2, \a y2).
3420 \fn void QPainter::drawRect(const QRectF &rectangle)
3422 Draws the current \a rectangle with the current pen and brush.
3424 A filled rectangle has a size of \a{rectangle}.size(). A stroked
3425 rectangle has a size of \a{rectangle}.size() plus the pen width.
3429 \li \inlineimage qpainter-rectangle.png
3431 \snippet code/src_gui_painting_qpainter.cpp 7
3434 \sa drawRects(), drawPolygon(), {Coordinate System}
3438 \fn void QPainter::drawRect(const QRect &rectangle)
3442 Draws the current \a rectangle with the current pen and brush.
3446 \fn void QPainter::drawRect(int x, int y, int width, int height)
3450 Draws a rectangle with upper left corner at (\a{x}, \a{y}) and
3451 with the given \a width and \a height.
3455 \fn void QPainter::drawRects(const QRectF *rectangles, int rectCount)
3457 Draws the first \a rectCount of the given \a rectangles using the
3458 current pen and brush.
3462 void QPainter::drawRects(const QRectF *rects, int rectCount)
3464 #ifdef QT_DEBUG_DRAW
3465 if (qt_show_painter_debug_output)
3466 printf("QPainter::drawRects(), count=%d\n", rectCount);
3471 qWarning("QPainter::drawRects: Painter not active");
3479 d->extended->drawRects(rects, rectCount);
3483 d->updateState(d->state);
3485 if (!d->state->emulationSpecifier) {
3486 d->engine->drawRects(rects, rectCount);
3490 if (d->state->emulationSpecifier == QPaintEngine::PrimitiveTransform
3491 && d->state->matrix.type() == QTransform::TxTranslate) {
3492 for (int i=0; i<rectCount; ++i) {
3493 QRectF r(rects[i].x() + d->state->matrix.dx(),
3494 rects[i].y() + d->state->matrix.dy(),
3497 d->engine->drawRects(&r, 1);
3500 if (d->state->brushNeedsResolving() || d->state->penNeedsResolving()) {
3501 for (int i=0; i<rectCount; ++i) {
3502 QPainterPath rectPath;
3503 rectPath.addRect(rects[i]);
3504 d->draw_helper(rectPath, QPainterPrivate::StrokeAndFillDraw);
3507 QPainterPath rectPath;
3508 for (int i=0; i<rectCount; ++i)
3509 rectPath.addRect(rects[i]);
3510 d->draw_helper(rectPath, QPainterPrivate::StrokeAndFillDraw);
3516 \fn void QPainter::drawRects(const QRect *rectangles, int rectCount)
3519 Draws the first \a rectCount of the given \a rectangles using the
3520 current pen and brush.
3522 void QPainter::drawRects(const QRect *rects, int rectCount)
3524 #ifdef QT_DEBUG_DRAW
3525 if (qt_show_painter_debug_output)
3526 printf("QPainter::drawRects(), count=%d\n", rectCount);
3531 qWarning("QPainter::drawRects: Painter not active");
3539 d->extended->drawRects(rects, rectCount);
3543 d->updateState(d->state);
3545 if (!d->state->emulationSpecifier) {
3546 d->engine->drawRects(rects, rectCount);
3550 if (d->state->emulationSpecifier == QPaintEngine::PrimitiveTransform
3551 && d->state->matrix.type() == QTransform::TxTranslate) {
3552 for (int i=0; i<rectCount; ++i) {
3553 QRectF r(rects[i].x() + d->state->matrix.dx(),
3554 rects[i].y() + d->state->matrix.dy(),
3558 d->engine->drawRects(&r, 1);
3561 if (d->state->brushNeedsResolving() || d->state->penNeedsResolving()) {
3562 for (int i=0; i<rectCount; ++i) {
3563 QPainterPath rectPath;
3564 rectPath.addRect(rects[i]);
3565 d->draw_helper(rectPath, QPainterPrivate::StrokeAndFillDraw);
3568 QPainterPath rectPath;
3569 for (int i=0; i<rectCount; ++i)
3570 rectPath.addRect(rects[i]);
3572 d->draw_helper(rectPath, QPainterPrivate::StrokeAndFillDraw);
3578 \fn void QPainter::drawRects(const QVector<QRectF> &rectangles)
3581 Draws the given \a rectangles using the current pen and brush.
3585 \fn void QPainter::drawRects(const QVector<QRect> &rectangles)
3589 Draws the given \a rectangles using the current pen and brush.
3593 \fn void QPainter::drawPoint(const QPointF &position)
3595 Draws a single point at the given \a position using the current
3598 \sa {Coordinate System}
3602 \fn void QPainter::drawPoint(const QPoint &position)
3605 Draws a single point at the given \a position using the current
3609 /*! \fn void QPainter::drawPoint(int x, int y)
3613 Draws a single point at position (\a x, \a y).
3617 Draws the first \a pointCount points in the array \a points using
3618 the current pen's color.
3620 \sa {Coordinate System}
3622 void QPainter::drawPoints(const QPointF *points, int pointCount)
3624 #ifdef QT_DEBUG_DRAW
3625 if (qt_show_painter_debug_output)
3626 printf("QPainter::drawPoints(), count=%d\n", pointCount);
3631 qWarning("QPainter::drawPoints: Painter not active");
3635 if (pointCount <= 0)
3639 d->extended->drawPoints(points, pointCount);
3643 d->updateState(d->state);
3645 if (!d->state->emulationSpecifier) {
3646 d->engine->drawPoints(points, pointCount);
3650 if (d->state->emulationSpecifier == QPaintEngine::PrimitiveTransform
3651 && d->state->matrix.type() == QTransform::TxTranslate) {
3652 // ### use drawPoints function
3653 for (int i=0; i<pointCount; ++i) {
3654 QPointF pt(points[i].x() + d->state->matrix.dx(),
3655 points[i].y() + d->state->matrix.dy());
3656 d->engine->drawPoints(&pt, 1);
3659 QPen pen = d->state->pen;
3660 bool flat_pen = pen.capStyle() == Qt::FlatCap;
3663 pen.setCapStyle(Qt::SquareCap);
3667 for (int i=0; i<pointCount; ++i) {
3668 path.moveTo(points[i].x(), points[i].y());
3669 path.lineTo(points[i].x() + 0.0001, points[i].y());
3671 d->draw_helper(path, QPainterPrivate::StrokeDraw);
3680 Draws the first \a pointCount points in the array \a points using
3681 the current pen's color.
3684 void QPainter::drawPoints(const QPoint *points, int pointCount)
3686 #ifdef QT_DEBUG_DRAW
3687 if (qt_show_painter_debug_output)
3688 printf("QPainter::drawPoints(), count=%d\n", pointCount);
3693 qWarning("QPainter::drawPoints: Painter not active");
3697 if (pointCount <= 0)
3701 d->extended->drawPoints(points, pointCount);
3705 d->updateState(d->state);
3707 if (!d->state->emulationSpecifier) {
3708 d->engine->drawPoints(points, pointCount);
3712 if (d->state->emulationSpecifier == QPaintEngine::PrimitiveTransform
3713 && d->state->matrix.type() == QTransform::TxTranslate) {
3714 // ### use drawPoints function
3715 for (int i=0; i<pointCount; ++i) {
3716 QPointF pt(points[i].x() + d->state->matrix.dx(),
3717 points[i].y() + d->state->matrix.dy());
3718 d->engine->drawPoints(&pt, 1);
3721 QPen pen = d->state->pen;
3722 bool flat_pen = (pen.capStyle() == Qt::FlatCap);
3725 pen.setCapStyle(Qt::SquareCap);
3729 for (int i=0; i<pointCount; ++i) {
3730 path.moveTo(points[i].x(), points[i].y());
3731 path.lineTo(points[i].x() + 0.0001, points[i].y());
3733 d->draw_helper(path, QPainterPrivate::StrokeDraw);
3740 \fn void QPainter::drawPoints(const QPolygonF &points)
3744 Draws the points in the vector \a points.
3748 \fn void QPainter::drawPoints(const QPolygon &points)
3752 Draws the points in the vector \a points.
3756 Sets the background mode of the painter to the given \a mode
3758 Qt::TransparentMode (the default) draws stippled lines and text
3759 without setting the background pixels. Qt::OpaqueMode fills these
3760 space with the current background color.
3762 Note that in order to draw a bitmap or pixmap transparently, you
3763 must use QPixmap::setMask().
3765 \sa backgroundMode(), setBackground(),
3766 {QPainter#Settings}{Settings}
3769 void QPainter::setBackgroundMode(Qt::BGMode mode)
3771 #ifdef QT_DEBUG_DRAW
3772 if (qt_show_painter_debug_output)
3773 printf("QPainter::setBackgroundMode(), mode=%d\n", mode);
3778 qWarning("QPainter::setBackgroundMode: Painter not active");
3781 if (d->state->bgMode == mode)
3784 d->state->bgMode = mode;
3786 d->checkEmulation();
3788 d->state->dirtyFlags |= QPaintEngine::DirtyBackgroundMode;
3793 Returns the current background mode.
3795 \sa setBackgroundMode(), {QPainter#Settings}{Settings}
3797 Qt::BGMode QPainter::backgroundMode() const
3799 Q_D(const QPainter);
3801 qWarning("QPainter::backgroundMode: Painter not active");
3802 return Qt::TransparentMode;
3804 return d->state->bgMode;
3811 Sets the painter's pen to have style Qt::SolidLine, width 0 and the
3815 void QPainter::setPen(const QColor &color)
3817 #ifdef QT_DEBUG_DRAW
3818 if (qt_show_painter_debug_output)
3819 printf("QPainter::setPen(), color=%04x\n", color.rgb());
3823 qWarning("QPainter::setPen: Painter not active");
3827 if (d->state->pen.style() == Qt::SolidLine
3828 && d->state->pen.widthF() == 0
3829 && d->state->pen.isSolid()
3830 && d->state->pen.color() == color)
3833 QPen pen(color.isValid() ? color : QColor(Qt::black), 0, Qt::SolidLine);
3835 d->state->pen = pen;
3837 d->extended->penChanged();
3839 d->state->dirtyFlags |= QPaintEngine::DirtyPen;
3843 Sets the painter's pen to be the given \a pen.
3845 The \a pen defines how to draw lines and outlines, and it also
3846 defines the text color.
3848 \sa pen(), {QPainter#Settings}{Settings}
3851 void QPainter::setPen(const QPen &pen)
3854 #ifdef QT_DEBUG_DRAW
3855 if (qt_show_painter_debug_output)
3856 printf("QPainter::setPen(), color=%04x, (brushStyle=%d) style=%d, cap=%d, join=%d\n",
3857 pen.color().rgb(), pen.brush().style(), pen.style(), pen.capStyle(), pen.joinStyle());
3861 qWarning("QPainter::setPen: Painter not active");
3865 if (d->state->pen == pen)
3868 d->state->pen = pen;
3871 d->checkEmulation();
3872 d->extended->penChanged();
3876 d->state->dirtyFlags |= QPaintEngine::DirtyPen;
3882 Sets the painter's pen to have the given \a style, width 0 and
3886 void QPainter::setPen(Qt::PenStyle style)
3890 qWarning("QPainter::setPen: Painter not active");
3894 if (d->state->pen.style() == style
3895 && (style == Qt::NoPen || (d->state->pen.widthF() == 0
3896 && d->state->pen.isSolid()
3897 && d->state->pen.color() == QColor(Qt::black))))
3900 // QPen(Qt::NoPen) is to avoid creating QPenData, including its brush (from the color)
3901 // Note that this works well as long as QPen(Qt::NoPen) returns a black, zero-width pen
3902 d->state->pen = (style == Qt::NoPen) ? QPen(Qt::NoPen) : QPen(Qt::black, 0, style);
3905 d->extended->penChanged();
3907 d->state->dirtyFlags |= QPaintEngine::DirtyPen;
3912 Returns the painter's current pen.
3914 \sa setPen(), {QPainter#Settings}{Settings}
3917 const QPen &QPainter::pen() const
3919 Q_D(const QPainter);
3921 qWarning("QPainter::pen: Painter not active");
3922 return d->fakeState()->pen;
3924 return d->state->pen;
3929 Sets the painter's brush to the given \a brush.
3931 The painter's brush defines how shapes are filled.
3933 \sa brush(), {QPainter#Settings}{Settings}
3936 void QPainter::setBrush(const QBrush &brush)
3938 #ifdef QT_DEBUG_DRAW
3939 if (qt_show_painter_debug_output)
3940 printf("QPainter::setBrush(), color=%04x, style=%d\n", brush.color().rgb(), brush.style());
3944 qWarning("QPainter::setBrush: Painter not active");
3948 if (d->state->brush.d == brush.d)
3952 d->state->brush = brush;
3953 d->checkEmulation();
3954 d->extended->brushChanged();
3958 d->state->brush = brush;
3959 d->state->dirtyFlags |= QPaintEngine::DirtyBrush;
3966 Sets the painter's brush to black color and the specified \a
3970 void QPainter::setBrush(Qt::BrushStyle style)
3974 qWarning("QPainter::setBrush: Painter not active");
3977 if (d->state->brush.style() == style &&
3978 (style == Qt::NoBrush
3979 || (style == Qt::SolidPattern && d->state->brush.color() == QColor(0, 0, 0))))
3981 d->state->brush = QBrush(Qt::black, style);
3983 d->extended->brushChanged();
3985 d->state->dirtyFlags |= QPaintEngine::DirtyBrush;
3989 Returns the painter's current brush.
3991 \sa QPainter::setBrush(), {QPainter#Settings}{Settings}
3994 const QBrush &QPainter::brush() const
3996 Q_D(const QPainter);
3998 qWarning("QPainter::brush: Painter not active");
3999 return d->fakeState()->brush;
4001 return d->state->brush;
4005 \fn void QPainter::setBackground(const QBrush &brush)
4007 Sets the background brush of the painter to the given \a brush.
4009 The background brush is the brush that is filled in when drawing
4010 opaque text, stippled lines and bitmaps. The background brush has
4011 no effect in transparent background mode (which is the default).
4013 \sa background(), setBackgroundMode(),
4014 {QPainter#Settings}{Settings}
4017 void QPainter::setBackground(const QBrush &bg)
4019 #ifdef QT_DEBUG_DRAW
4020 if (qt_show_painter_debug_output)
4021 printf("QPainter::setBackground(), color=%04x, style=%d\n", bg.color().rgb(), bg.style());
4026 qWarning("QPainter::setBackground: Painter not active");
4029 d->state->bgBrush = bg;
4031 d->state->dirtyFlags |= QPaintEngine::DirtyBackground;
4035 Sets the painter's font to the given \a font.
4037 This font is used by subsequent drawText() functions. The text
4038 color is the same as the pen color.
4040 If you set a font that isn't available, Qt finds a close match.
4041 font() will return what you set using setFont() and fontInfo() returns the
4042 font actually being used (which may be the same).
4044 \sa font(), drawText(), {QPainter#Settings}{Settings}
4047 void QPainter::setFont(const QFont &font)
4051 #ifdef QT_DEBUG_DRAW
4052 if (qt_show_painter_debug_output)
4053 printf("QPainter::setFont(), family=%s, pointSize=%d\n", font.family().toLatin1().constData(), font.pointSize());
4057 qWarning("QPainter::setFont: Painter not active");
4061 d->state->font = QFont(font.resolve(d->state->deviceFont), device());
4063 d->state->dirtyFlags |= QPaintEngine::DirtyFont;
4067 Returns the currently set font used for drawing text.
4069 \sa setFont(), drawText(), {QPainter#Settings}{Settings}
4071 const QFont &QPainter::font() const
4073 Q_D(const QPainter);
4075 qWarning("QPainter::font: Painter not active");
4076 return d->fakeState()->font;
4078 return d->state->font;
4084 Draws the given rectangle \a rect with rounded corners.
4086 The \a xRadius and \a yRadius arguments specify the radii
4087 of the ellipses defining the corners of the rounded rectangle.
4088 When \a mode is Qt::RelativeSize, \a xRadius and
4089 \a yRadius are specified in percentage of half the rectangle's
4090 width and height respectively, and should be in the range
4093 A filled rectangle has a size of rect.size(). A stroked rectangle
4094 has a size of rect.size() plus the pen width.
4098 \li \inlineimage qpainter-roundrect.png
4100 \snippet code/src_gui_painting_qpainter.cpp 8
4103 \sa drawRect(), QPen
4105 void QPainter::drawRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius, Qt::SizeMode mode)
4107 #ifdef QT_DEBUG_DRAW
4108 if (qt_show_painter_debug_output)
4109 printf("QPainter::drawRoundedRect(), [%.2f,%.2f,%.2f,%.2f]\n", rect.x(), rect.y(), rect.width(), rect.height());
4116 if (xRadius <= 0 || yRadius <= 0) { // draw normal rectangle
4122 d->extended->drawRoundedRect(rect, xRadius, yRadius, mode);
4127 path.addRoundedRect(rect, xRadius, yRadius, mode);
4132 \fn void QPainter::drawRoundedRect(const QRect &rect, qreal xRadius, qreal yRadius,
4133 Qt::SizeMode mode = Qt::AbsoluteSize);
4137 Draws the given rectangle \a rect with rounded corners.
4141 \fn void QPainter::drawRoundedRect(int x, int y, int w, int h, qreal xRadius, qreal yRadius,
4142 Qt::SizeMode mode = Qt::AbsoluteSize);
4146 Draws the given rectangle \a x, \a y, \a w, \a h with rounded corners.
4152 Draws a rectangle \a r with rounded corners.
4154 The \a xRnd and \a yRnd arguments specify how rounded the corners
4155 should be. 0 is angled corners, 99 is maximum roundedness.
4157 A filled rectangle has a size of r.size(). A stroked rectangle
4158 has a size of r.size() plus the pen width.
4160 \sa drawRoundedRect()
4162 void QPainter::drawRoundRect(const QRectF &r, int xRnd, int yRnd)
4164 drawRoundedRect(r, xRnd, yRnd, Qt::RelativeSize);
4169 \fn void QPainter::drawRoundRect(const QRect &r, int xRnd = 25, int yRnd = 25)
4174 Draws the rectangle \a r with rounded corners.
4180 \fn QPainter::drawRoundRect(int x, int y, int w, int h, int xRnd, int yRnd)
4184 Draws the rectangle \a x, \a y, \a w, \a h with rounded corners.
4188 \fn void QPainter::drawEllipse(const QRectF &rectangle)
4190 Draws the ellipse defined by the given \a rectangle.
4192 A filled ellipse has a size of \a{rectangle}.\l
4193 {QRect::size()}{size()}. A stroked ellipse has a size of
4194 \a{rectangle}.\l {QRect::size()}{size()} plus the pen width.
4198 \li \inlineimage qpainter-ellipse.png
4200 \snippet code/src_gui_painting_qpainter.cpp 9
4203 \sa drawPie(), {Coordinate System}
4205 void QPainter::drawEllipse(const QRectF &r)
4207 #ifdef QT_DEBUG_DRAW
4208 if (qt_show_painter_debug_output)
4209 printf("QPainter::drawEllipse(), [%.2f,%.2f,%.2f,%.2f]\n", r.x(), r.y(), r.width(), r.height());
4216 QRectF rect(r.normalized());
4219 d->extended->drawEllipse(rect);
4223 d->updateState(d->state);
4224 if (d->state->emulationSpecifier) {
4225 if (d->state->emulationSpecifier == QPaintEngine::PrimitiveTransform
4226 && d->state->matrix.type() == QTransform::TxTranslate) {
4227 rect.translate(QPointF(d->state->matrix.dx(), d->state->matrix.dy()));
4230 path.addEllipse(rect);
4231 d->draw_helper(path, QPainterPrivate::StrokeAndFillDraw);
4236 d->engine->drawEllipse(rect);
4240 \fn QPainter::drawEllipse(const QRect &rectangle)
4244 Draws the ellipse defined by the given \a rectangle.
4246 void QPainter::drawEllipse(const QRect &r)
4248 #ifdef QT_DEBUG_DRAW
4249 if (qt_show_painter_debug_output)
4250 printf("QPainter::drawEllipse(), [%d,%d,%d,%d]\n", r.x(), r.y(), r.width(), r.height());
4257 QRect rect(r.normalized());
4260 d->extended->drawEllipse(rect);
4264 d->updateState(d->state);
4266 if (d->state->emulationSpecifier) {
4267 if (d->state->emulationSpecifier == QPaintEngine::PrimitiveTransform
4268 && d->state->matrix.type() == QTransform::TxTranslate) {
4269 rect.translate(QPoint(qRound(d->state->matrix.dx()), qRound(d->state->matrix.dy())));
4272 path.addEllipse(rect);
4273 d->draw_helper(path, QPainterPrivate::StrokeAndFillDraw);
4278 d->engine->drawEllipse(rect);
4282 \fn QPainter::drawEllipse(int x, int y, int width, int height)
4286 Draws the ellipse defined by the rectangle beginning at (\a{x},
4287 \a{y}) with the given \a width and \a height.
4293 \fn QPainter::drawEllipse(const QPointF ¢er, qreal rx, qreal ry)
4297 Draws the ellipse positioned at \a{center} with radii \a{rx} and \a{ry}.
4303 \fn QPainter::drawEllipse(const QPoint ¢er, int rx, int ry)
4307 Draws the ellipse positioned at \a{center} with radii \a{rx} and \a{ry}.
4311 \fn void QPainter::drawArc(const QRectF &rectangle, int startAngle, int spanAngle)
4313 Draws the arc defined by the given \a rectangle, \a startAngle and
4316 The \a startAngle and \a spanAngle must be specified in 1/16th of
4317 a degree, i.e. a full circle equals 5760 (16 * 360). Positive
4318 values for the angles mean counter-clockwise while negative values
4319 mean the clockwise direction. Zero degrees is at the 3 o'clock
4324 \li \inlineimage qpainter-arc.png
4326 \snippet code/src_gui_painting_qpainter.cpp 10
4329 \sa drawPie(), drawChord(), {Coordinate System}
4332 void QPainter::drawArc(const QRectF &r, int a, int alen)
4334 #ifdef QT_DEBUG_DRAW
4335 if (qt_show_painter_debug_output)
4336 printf("QPainter::drawArc(), [%.2f,%.2f,%.2f,%.2f], angle=%d, sweep=%d\n",
4337 r.x(), r.y(), r.width(), r.height(), a/16, alen/16);
4344 QRectF rect = r.normalized();
4347 path.arcMoveTo(rect, a/16.0);
4348 path.arcTo(rect, a/16.0, alen/16.0);
4349 strokePath(path, d->state->pen);
4352 /*! \fn void QPainter::drawArc(const QRect &rectangle, int startAngle,
4357 Draws the arc defined by the given \a rectangle, \a startAngle and
4362 \fn void QPainter::drawArc(int x, int y, int width, int height,
4363 int startAngle, int spanAngle)
4367 Draws the arc defined by the rectangle beginning at (\a x, \a y)
4368 with the specified \a width and \a height, and the given \a
4369 startAngle and \a spanAngle.
4373 \fn void QPainter::drawPie(const QRectF &rectangle, int startAngle, int spanAngle)
4375 Draws a pie defined by the given \a rectangle, \a startAngle and
4378 The pie is filled with the current brush().
4380 The startAngle and spanAngle must be specified in 1/16th of a
4381 degree, i.e. a full circle equals 5760 (16 * 360). Positive values
4382 for the angles mean counter-clockwise while negative values mean
4383 the clockwise direction. Zero degrees is at the 3 o'clock
4388 \li \inlineimage qpainter-pie.png
4390 \snippet code/src_gui_painting_qpainter.cpp 11
4393 \sa drawEllipse(), drawChord(), {Coordinate System}
4395 void QPainter::drawPie(const QRectF &r, int a, int alen)
4397 #ifdef QT_DEBUG_DRAW
4398 if (qt_show_painter_debug_output)
4399 printf("QPainter::drawPie(), [%.2f,%.2f,%.2f,%.2f], angle=%d, sweep=%d\n",
4400 r.x(), r.y(), r.width(), r.height(), a/16, alen/16);
4411 if (a < 0) a += (360*16);
4414 QRectF rect = r.normalized();
4417 path.moveTo(rect.center());
4418 path.arcTo(rect.x(), rect.y(), rect.width(), rect.height(), a/16.0, alen/16.0);
4419 path.closeSubpath();
4425 \fn void QPainter::drawPie(const QRect &rectangle, int startAngle, int spanAngle)
4428 Draws a pie defined by the given \a rectangle, \a startAngle and
4433 \fn void QPainter::drawPie(int x, int y, int width, int height, int
4434 startAngle, int spanAngle)
4438 Draws the pie defined by the rectangle beginning at (\a x, \a y) with
4439 the specified \a width and \a height, and the given \a startAngle and
4444 \fn void QPainter::drawChord(const QRectF &rectangle, int startAngle, int spanAngle)
4446 Draws the chord defined by the given \a rectangle, \a startAngle and
4447 \a spanAngle. The chord is filled with the current brush().
4449 The startAngle and spanAngle must be specified in 1/16th of a
4450 degree, i.e. a full circle equals 5760 (16 * 360). Positive values
4451 for the angles mean counter-clockwise while negative values mean
4452 the clockwise direction. Zero degrees is at the 3 o'clock
4457 \li \inlineimage qpainter-chord.png
4459 \snippet code/src_gui_painting_qpainter.cpp 12
4462 \sa drawArc(), drawPie(), {Coordinate System}
4464 void QPainter::drawChord(const QRectF &r, int a, int alen)
4466 #ifdef QT_DEBUG_DRAW
4467 if (qt_show_painter_debug_output)
4468 printf("QPainter::drawChord(), [%.2f,%.2f,%.2f,%.2f], angle=%d, sweep=%d\n",
4469 r.x(), r.y(), r.width(), r.height(), a/16, alen/16);
4476 QRectF rect = r.normalized();
4479 path.arcMoveTo(rect, a/16.0);
4480 path.arcTo(rect, a/16.0, alen/16.0);
4481 path.closeSubpath();
4485 \fn void QPainter::drawChord(const QRect &rectangle, int startAngle, int spanAngle)
4489 Draws the chord defined by the given \a rectangle, \a startAngle and
4494 \fn void QPainter::drawChord(int x, int y, int width, int height, int
4495 startAngle, int spanAngle)
4499 Draws the chord defined by the rectangle beginning at (\a x, \a y)
4500 with the specified \a width and \a height, and the given \a
4501 startAngle and \a spanAngle.
4506 Draws the first \a lineCount lines in the array \a lines
4507 using the current pen.
4509 \sa drawLine(), drawPolyline()
4511 void QPainter::drawLines(const QLineF *lines, int lineCount)
4513 #ifdef QT_DEBUG_DRAW
4514 if (qt_show_painter_debug_output)
4515 printf("QPainter::drawLines(), line count=%d\n", lineCount);
4520 if (!d->engine || lineCount < 1)
4524 d->extended->drawLines(lines, lineCount);
4528 d->updateState(d->state);
4530 uint lineEmulation = line_emulation(d->state->emulationSpecifier);
4532 if (lineEmulation) {
4533 if (lineEmulation == QPaintEngine::PrimitiveTransform
4534 && d->state->matrix.type() == QTransform::TxTranslate) {
4535 for (int i = 0; i < lineCount; ++i) {
4536 QLineF line = lines[i];
4537 line.translate(d->state->matrix.dx(), d->state->matrix.dy());
4538 d->engine->drawLines(&line, 1);
4541 QPainterPath linePath;
4542 for (int i = 0; i < lineCount; ++i) {
4543 linePath.moveTo(lines[i].p1());
4544 linePath.lineTo(lines[i].p2());
4546 d->draw_helper(linePath, QPainterPrivate::StrokeDraw);
4550 d->engine->drawLines(lines, lineCount);
4554 \fn void QPainter::drawLines(const QLine *lines, int lineCount)
4557 Draws the first \a lineCount lines in the array \a lines
4558 using the current pen.
4560 void QPainter::drawLines(const QLine *lines, int lineCount)
4562 #ifdef QT_DEBUG_DRAW
4563 if (qt_show_painter_debug_output)
4564 printf("QPainter::drawLine(), line count=%d\n", lineCount);
4569 if (!d->engine || lineCount < 1)
4573 d->extended->drawLines(lines, lineCount);
4577 d->updateState(d->state);
4579 uint lineEmulation = line_emulation(d->state->emulationSpecifier);
4581 if (lineEmulation) {
4582 if (lineEmulation == QPaintEngine::PrimitiveTransform
4583 && d->state->matrix.type() == QTransform::TxTranslate) {
4584 for (int i = 0; i < lineCount; ++i) {
4585 QLineF line = lines[i];
4586 line.translate(d->state->matrix.dx(), d->state->matrix.dy());
4587 d->engine->drawLines(&line, 1);
4590 QPainterPath linePath;
4591 for (int i = 0; i < lineCount; ++i) {
4592 linePath.moveTo(lines[i].p1());
4593 linePath.lineTo(lines[i].p2());
4595 d->draw_helper(linePath, QPainterPrivate::StrokeDraw);
4599 d->engine->drawLines(lines, lineCount);
4605 Draws the first \a lineCount lines in the array \a pointPairs
4606 using the current pen. The lines are specified as pairs of points
4607 so the number of entries in \a pointPairs must be at least \a
4610 void QPainter::drawLines(const QPointF *pointPairs, int lineCount)
4612 Q_ASSERT(sizeof(QLineF) == 2*sizeof(QPointF));
4614 drawLines((QLineF*)pointPairs, lineCount);
4620 Draws the first \a lineCount lines in the array \a pointPairs
4621 using the current pen.
4623 void QPainter::drawLines(const QPoint *pointPairs, int lineCount)
4625 Q_ASSERT(sizeof(QLine) == 2*sizeof(QPoint));
4627 drawLines((QLine*)pointPairs, lineCount);
4632 \fn void QPainter::drawLines(const QVector<QPointF> &pointPairs)
4635 Draws a line for each pair of points in the vector \a pointPairs
4636 using the current pen. If there is an odd number of points in the
4637 array, the last point will be ignored.
4641 \fn void QPainter::drawLines(const QVector<QPoint> &pointPairs)
4644 Draws a line for each pair of points in the vector \a pointPairs
4645 using the current pen.
4649 \fn void QPainter::drawLines(const QVector<QLineF> &lines)
4652 Draws the set of lines defined by the list \a lines using the
4653 current pen and brush.
4657 \fn void QPainter::drawLines(const QVector<QLine> &lines)
4660 Draws the set of lines defined by the list \a lines using the
4661 current pen and brush.
4665 Draws the polyline defined by the first \a pointCount points in \a
4666 points using the current pen.
4668 Note that unlike the drawPolygon() function the last point is \e
4669 not connected to the first, neither is the polyline filled.
4674 \snippet code/src_gui_painting_qpainter.cpp 13
4677 \sa drawLines(), drawPolygon(), {Coordinate System}
4679 void QPainter::drawPolyline(const QPointF *points, int pointCount)
4681 #ifdef QT_DEBUG_DRAW
4682 if (qt_show_painter_debug_output)
4683 printf("QPainter::drawPolyline(), count=%d\n", pointCount);
4687 if (!d->engine || pointCount < 2)
4691 d->extended->drawPolygon(points, pointCount, QPaintEngine::PolylineMode);
4695 d->updateState(d->state);
4697 uint lineEmulation = line_emulation(d->state->emulationSpecifier);
4699 if (lineEmulation) {
4701 // if (lineEmulation == QPaintEngine::PrimitiveTransform
4702 // && d->state->matrix.type() == QTransform::TxTranslate) {
4704 QPainterPath polylinePath(points[0]);
4705 for (int i=1; i<pointCount; ++i)
4706 polylinePath.lineTo(points[i]);
4707 d->draw_helper(polylinePath, QPainterPrivate::StrokeDraw);
4710 d->engine->drawPolygon(points, pointCount, QPaintEngine::PolylineMode);
4717 Draws the polyline defined by the first \a pointCount points in \a
4718 points using the current pen.
4720 void QPainter::drawPolyline(const QPoint *points, int pointCount)
4722 #ifdef QT_DEBUG_DRAW
4723 if (qt_show_painter_debug_output)
4724 printf("QPainter::drawPolyline(), count=%d\n", pointCount);
4728 if (!d->engine || pointCount < 2)
4732 d->extended->drawPolygon(points, pointCount, QPaintEngine::PolylineMode);
4736 d->updateState(d->state);
4738 uint lineEmulation = line_emulation(d->state->emulationSpecifier);
4740 if (lineEmulation) {
4742 // if (lineEmulation == QPaintEngine::PrimitiveTransform
4743 // && d->state->matrix.type() == QTransform::TxTranslate) {
4745 QPainterPath polylinePath(points[0]);
4746 for (int i=1; i<pointCount; ++i)
4747 polylinePath.lineTo(points[i]);
4748 d->draw_helper(polylinePath, QPainterPrivate::StrokeDraw);
4751 d->engine->drawPolygon(points, pointCount, QPaintEngine::PolylineMode);
4756 \fn void QPainter::drawPolyline(const QPolygonF &points)
4760 Draws the polyline defined by the given \a points using the
4765 \fn void QPainter::drawPolyline(const QPolygon &points)
4769 Draws the polyline defined by the given \a points using the
4774 Draws the polygon defined by the first \a pointCount points in the
4775 array \a points using the current pen and brush.
4779 \li \inlineimage qpainter-polygon.png
4781 \snippet code/src_gui_painting_qpainter.cpp 14
4784 The first point is implicitly connected to the last point, and the
4785 polygon is filled with the current brush().
4787 If \a fillRule is Qt::WindingFill, the polygon is filled using the
4788 winding fill algorithm. If \a fillRule is Qt::OddEvenFill, the
4789 polygon is filled using the odd-even fill algorithm. See
4790 \l{Qt::FillRule} for a more detailed description of these fill
4793 \sa drawConvexPolygon(), drawPolyline(), {Coordinate System}
4795 void QPainter::drawPolygon(const QPointF *points, int pointCount, Qt::FillRule fillRule)
4797 #ifdef QT_DEBUG_DRAW
4798 if (qt_show_painter_debug_output)
4799 printf("QPainter::drawPolygon(), count=%d\n", pointCount);
4804 if (!d->engine || pointCount < 2)
4808 d->extended->drawPolygon(points, pointCount, QPaintEngine::PolygonDrawMode(fillRule));
4812 d->updateState(d->state);
4814 uint emulationSpecifier = d->state->emulationSpecifier;
4816 if (emulationSpecifier) {
4817 QPainterPath polygonPath(points[0]);
4818 for (int i=1; i<pointCount; ++i)
4819 polygonPath.lineTo(points[i]);
4820 polygonPath.closeSubpath();
4821 polygonPath.setFillRule(fillRule);
4822 d->draw_helper(polygonPath);
4826 d->engine->drawPolygon(points, pointCount, QPaintEngine::PolygonDrawMode(fillRule));
4831 Draws the polygon defined by the first \a pointCount points in the
4834 void QPainter::drawPolygon(const QPoint *points, int pointCount, Qt::FillRule fillRule)
4836 #ifdef QT_DEBUG_DRAW
4837 if (qt_show_painter_debug_output)
4838 printf("QPainter::drawPolygon(), count=%d\n", pointCount);
4843 if (!d->engine || pointCount < 2)
4847 d->extended->drawPolygon(points, pointCount, QPaintEngine::PolygonDrawMode(fillRule));
4851 d->updateState(d->state);
4853 uint emulationSpecifier = d->state->emulationSpecifier;
4855 if (emulationSpecifier) {
4856 QPainterPath polygonPath(points[0]);
4857 for (int i=1; i<pointCount; ++i)
4858 polygonPath.lineTo(points[i]);
4859 polygonPath.closeSubpath();
4860 polygonPath.setFillRule(fillRule);
4861 d->draw_helper(polygonPath);
4865 d->engine->drawPolygon(points, pointCount, QPaintEngine::PolygonDrawMode(fillRule));
4868 /*! \fn void QPainter::drawPolygon(const QPolygonF &points, Qt::FillRule fillRule)
4872 Draws the polygon defined by the given \a points using the fill
4876 /*! \fn void QPainter::drawPolygon(const QPolygon &points, Qt::FillRule fillRule)
4880 Draws the polygon defined by the given \a points using the fill
4885 \fn void QPainter::drawConvexPolygon(const QPointF *points, int pointCount)
4887 Draws the convex polygon defined by the first \a pointCount points
4888 in the array \a points using the current pen.
4892 \li \inlineimage qpainter-polygon.png
4894 \snippet code/src_gui_painting_qpainter.cpp 15
4897 The first point is implicitly connected to the last point, and the
4898 polygon is filled with the current brush(). If the supplied
4899 polygon is not convex, i.e. it contains at least one angle larger
4900 than 180 degrees, the results are undefined.
4902 On some platforms (e.g. X11), the drawConvexPolygon() function can
4903 be faster than the drawPolygon() function.
4905 \sa drawPolygon(), drawPolyline(), {Coordinate System}
4909 \fn void QPainter::drawConvexPolygon(const QPoint *points, int pointCount)
4912 Draws the convex polygon defined by the first \a pointCount points
4913 in the array \a points using the current pen.
4917 \fn void QPainter::drawConvexPolygon(const QPolygonF &polygon)
4921 Draws the convex polygon defined by \a polygon using the current
4926 \fn void QPainter::drawConvexPolygon(const QPolygon &polygon)
4929 Draws the convex polygon defined by \a polygon using the current
4933 void QPainter::drawConvexPolygon(const QPoint *points, int pointCount)
4935 #ifdef QT_DEBUG_DRAW
4936 if (qt_show_painter_debug_output)
4937 printf("QPainter::drawConvexPolygon(), count=%d\n", pointCount);
4942 if (!d->engine || pointCount < 2)
4946 d->extended->drawPolygon(points, pointCount, QPaintEngine::ConvexMode);
4950 d->updateState(d->state);
4952 uint emulationSpecifier = d->state->emulationSpecifier;
4954 if (emulationSpecifier) {
4955 QPainterPath polygonPath(points[0]);
4956 for (int i=1; i<pointCount; ++i)
4957 polygonPath.lineTo(points[i]);
4958 polygonPath.closeSubpath();
4959 polygonPath.setFillRule(Qt::WindingFill);
4960 d->draw_helper(polygonPath);
4964 d->engine->drawPolygon(points, pointCount, QPaintEngine::ConvexMode);
4967 void QPainter::drawConvexPolygon(const QPointF *points, int pointCount)
4969 #ifdef QT_DEBUG_DRAW
4970 if (qt_show_painter_debug_output)
4971 printf("QPainter::drawConvexPolygon(), count=%d\n", pointCount);
4976 if (!d->engine || pointCount < 2)
4980 d->extended->drawPolygon(points, pointCount, QPaintEngine::ConvexMode);
4984 d->updateState(d->state);
4986 uint emulationSpecifier = d->state->emulationSpecifier;
4988 if (emulationSpecifier) {
4989 QPainterPath polygonPath(points[0]);
4990 for (int i=1; i<pointCount; ++i)
4991 polygonPath.lineTo(points[i]);
4992 polygonPath.closeSubpath();
4993 polygonPath.setFillRule(Qt::WindingFill);
4994 d->draw_helper(polygonPath);
4998 d->engine->drawPolygon(points, pointCount, QPaintEngine::ConvexMode);
5001 static inline QPointF roundInDeviceCoordinates(const QPointF &p, const QTransform &m)
5003 return m.inverted().map(QPointF(m.map(p).toPoint()));
5007 \fn void QPainter::drawPixmap(const QRectF &target, const QPixmap &pixmap, const QRectF &source)
5009 Draws the rectangular portion \a source of the given \a pixmap
5010 into the given \a target in the paint device.
5012 \note The pixmap is scaled to fit the rectangle, if both the pixmap and rectangle size disagree.
5017 \snippet code/src_gui_painting_qpainter.cpp 16
5020 If \a pixmap is a QBitmap it is drawn with the bits that are "set"
5021 using the pens color. If backgroundMode is Qt::OpaqueMode, the
5022 "unset" bits are drawn using the color of the background brush; if
5023 backgroundMode is Qt::TransparentMode, the "unset" bits are
5024 transparent. Drawing bitmaps with gradient or texture colors is
5029 void QPainter::drawPixmap(const QPointF &p, const QPixmap &pm)
5031 #if defined QT_DEBUG_DRAW
5032 if (qt_show_painter_debug_output)
5033 printf("QPainter::drawPixmap(), p=[%.2f,%.2f], pix=[%d,%d]\n",
5035 pm.width(), pm.height());
5040 if (!d->engine || pm.isNull())
5044 qt_painter_thread_test(d->device->devType(), "drawPixmap()", true);
5048 d->extended->drawPixmap(p, pm);
5056 int h = pm.height();
5061 // Emulate opaque background for bitmaps
5062 if (d->state->bgMode == Qt::OpaqueMode && pm.isQBitmap()) {
5063 fillRect(QRectF(x, y, w, h), d->state->bgBrush.color());
5066 d->updateState(d->state);
5068 if ((d->state->matrix.type() > QTransform::TxTranslate
5069 && !d->engine->hasFeature(QPaintEngine::PixmapTransform))
5070 || (!d->state->matrix.isAffine() && !d->engine->hasFeature(QPaintEngine::PerspectiveTransform))
5071 || (d->state->opacity != 1.0 && !d->engine->hasFeature(QPaintEngine::ConstantOpacity)))
5074 // If there is no rotation involved we have to make sure we use the
5075 // antialiased and not the aliased coordinate system by rounding the coordinates.
5076 if (d->state->matrix.type() <= QTransform::TxScale) {
5077 const QPointF p = roundInDeviceCoordinates(QPointF(x, y), d->state->matrix);
5082 setBackgroundMode(Qt::TransparentMode);
5083 setRenderHint(Antialiasing, renderHints() & SmoothPixmapTransform);
5084 QBrush brush(d->state->pen.color(), pm);
5087 setBrushOrigin(QPointF(0, 0));
5089 drawRect(pm.rect());
5092 if (!d->engine->hasFeature(QPaintEngine::PixmapTransform)) {
5093 x += d->state->matrix.dx();
5094 y += d->state->matrix.dy();
5096 d->engine->drawPixmap(QRectF(x, y, w, h), pm, QRectF(0, 0, w, h));
5100 void QPainter::drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr)
5102 #if defined QT_DEBUG_DRAW
5103 if (qt_show_painter_debug_output)
5104 printf("QPainter::drawPixmap(), target=[%.2f,%.2f,%.2f,%.2f], pix=[%d,%d], source=[%.2f,%.2f,%.2f,%.2f]\n",
5105 r.x(), r.y(), r.width(), r.height(),
5106 pm.width(), pm.height(),
5107 sr.x(), sr.y(), sr.width(), sr.height());
5111 if (!d->engine || pm.isNull())
5114 qt_painter_thread_test(d->device->devType(), "drawPixmap()", true);
5119 qreal w = r.width();
5120 qreal h = r.height();
5123 qreal sw = sr.width();
5124 qreal sh = sr.height();
5126 // Sanity-check clipping
5128 sw = pm.width() - sx;
5131 sh = pm.height() - sy;
5139 qreal w_ratio = sx * w/sw;
5147 qreal h_ratio = sy * h/sh;
5154 if (sw + sx > pm.width()) {
5155 qreal delta = sw - (pm.width() - sx);
5156 qreal w_ratio = delta * w/sw;
5161 if (sh + sy > pm.height()) {
5162 qreal delta = sh - (pm.height() - sy);
5163 qreal h_ratio = delta * h/sh;
5168 if (w == 0 || h == 0 || sw <= 0 || sh <= 0)
5172 d->extended->drawPixmap(QRectF(x, y, w, h), pm, QRectF(sx, sy, sw, sh));
5176 // Emulate opaque background for bitmaps
5177 if (d->state->bgMode == Qt::OpaqueMode && pm.isQBitmap())
5178 fillRect(QRectF(x, y, w, h), d->state->bgBrush.color());
5180 d->updateState(d->state);
5182 if ((d->state->matrix.type() > QTransform::TxTranslate
5183 && !d->engine->hasFeature(QPaintEngine::PixmapTransform))
5184 || (!d->state->matrix.isAffine() && !d->engine->hasFeature(QPaintEngine::PerspectiveTransform))
5185 || (d->state->opacity != 1.0 && !d->engine->hasFeature(QPaintEngine::ConstantOpacity))
5186 || ((sw != w || sh != h) && !d->engine->hasFeature(QPaintEngine::PixmapTransform)))
5189 // If there is no rotation involved we have to make sure we use the
5190 // antialiased and not the aliased coordinate system by rounding the coordinates.
5191 if (d->state->matrix.type() <= QTransform::TxScale) {
5192 const QPointF p = roundInDeviceCoordinates(QPointF(x, y), d->state->matrix);
5197 if (d->state->matrix.type() <= QTransform::TxTranslate && sw == w && sh == h) {
5205 scale(w / sw, h / sh);
5206 setBackgroundMode(Qt::TransparentMode);
5207 setRenderHint(Antialiasing, renderHints() & SmoothPixmapTransform);
5210 if (sw == pm.width() && sh == pm.height())
5211 brush = QBrush(d->state->pen.color(), pm);
5213 brush = QBrush(d->state->pen.color(), pm.copy(sx, sy, sw, sh));
5218 drawRect(QRectF(0, 0, sw, sh));
5221 if (!d->engine->hasFeature(QPaintEngine::PixmapTransform)) {
5222 x += d->state->matrix.dx();
5223 y += d->state->matrix.dy();
5225 d->engine->drawPixmap(QRectF(x, y, w, h), pm, QRectF(sx, sy, sw, sh));
5231 \fn void QPainter::drawPixmap(const QRect &target, const QPixmap &pixmap,
5232 const QRect &source)
5235 Draws the rectangular portion \a source of the given \a pixmap
5236 into the given \a target in the paint device.
5238 \note The pixmap is scaled to fit the rectangle, if both the pixmap and rectangle size disagree.
5242 \fn void QPainter::drawPixmap(const QPointF &point, const QPixmap &pixmap,
5243 const QRectF &source)
5246 Draws the rectangular portion \a source of the given \a pixmap
5247 with its origin at the given \a point.
5251 \fn void QPainter::drawPixmap(const QPoint &point, const QPixmap &pixmap,
5252 const QRect &source)
5256 Draws the rectangular portion \a source of the given \a pixmap
5257 with its origin at the given \a point.
5261 \fn void QPainter::drawPixmap(const QPointF &point, const QPixmap &pixmap)
5264 Draws the given \a pixmap with its origin at the given \a point.
5268 \fn void QPainter::drawPixmap(const QPoint &point, const QPixmap &pixmap)
5271 Draws the given \a pixmap with its origin at the given \a point.
5275 \fn void QPainter::drawPixmap(int x, int y, const QPixmap &pixmap)
5279 Draws the given \a pixmap at position (\a{x}, \a{y}).
5283 \fn void QPainter::drawPixmap(const QRect &rectangle, const QPixmap &pixmap)
5286 Draws the given \a pixmap into the given \a rectangle.
5288 \note The pixmap is scaled to fit the rectangle, if both the pixmap and rectangle size disagree.
5292 \fn void QPainter::drawPixmap(int x, int y, int width, int height,
5293 const QPixmap &pixmap)
5297 Draws the \a pixmap into the rectangle at position (\a{x}, \a{y})
5298 with the given \a width and \a height.
5302 \fn void QPainter::drawPixmap(int x, int y, int w, int h, const QPixmap &pixmap,
5303 int sx, int sy, int sw, int sh)
5307 Draws the rectangular portion with the origin (\a{sx}, \a{sy}),
5308 width \a sw and height \a sh, of the given \a pixmap , at the
5309 point (\a{x}, \a{y}), with a width of \a w and a height of \a h.
5310 If sw or sh are equal to zero the width/height of the pixmap
5311 is used and adjusted by the offset sx/sy;
5315 \fn void QPainter::drawPixmap(int x, int y, const QPixmap &pixmap,
5316 int sx, int sy, int sw, int sh)
5320 Draws a pixmap at (\a{x}, \a{y}) by copying a part of the given \a
5321 pixmap into the paint device.
5323 (\a{x}, \a{y}) specifies the top-left point in the paint device that is
5324 to be drawn onto. (\a{sx}, \a{sy}) specifies the top-left point in \a
5325 pixmap that is to be drawn. The default is (0, 0).
5327 (\a{sw}, \a{sh}) specifies the size of the pixmap that is to be drawn.
5328 The default, (0, 0) (and negative) means all the way to the
5329 bottom-right of the pixmap.
5332 void QPainter::drawImage(const QPointF &p, const QImage &image)
5336 if (!d->engine || image.isNull())
5340 d->extended->drawImage(p, image);
5347 int w = image.width();
5348 int h = image.height();
5350 d->updateState(d->state);
5352 if (((d->state->matrix.type() > QTransform::TxTranslate)
5353 && !d->engine->hasFeature(QPaintEngine::PixmapTransform))
5354 || (!d->state->matrix.isAffine() && !d->engine->hasFeature(QPaintEngine::PerspectiveTransform))
5355 || (d->state->opacity != 1.0 && !d->engine->hasFeature(QPaintEngine::ConstantOpacity)))
5358 // If there is no rotation involved we have to make sure we use the
5359 // antialiased and not the aliased coordinate system by rounding the coordinates.
5360 if (d->state->matrix.type() <= QTransform::TxScale) {
5361 const QPointF p = roundInDeviceCoordinates(QPointF(x, y), d->state->matrix);
5366 setBackgroundMode(Qt::TransparentMode);
5367 setRenderHint(Antialiasing, renderHints() & SmoothPixmapTransform);
5368 QBrush brush(image);
5371 setBrushOrigin(QPointF(0, 0));
5373 drawRect(image.rect());
5378 if (d->state->matrix.type() == QTransform::TxTranslate
5379 && !d->engine->hasFeature(QPaintEngine::PixmapTransform)) {
5380 x += d->state->matrix.dx();
5381 y += d->state->matrix.dy();
5384 d->engine->drawImage(QRectF(x, y, w, h), image, QRectF(0, 0, w, h), Qt::AutoColor);
5387 void QPainter::drawImage(const QRectF &targetRect, const QImage &image, const QRectF &sourceRect,
5388 Qt::ImageConversionFlags flags)
5392 if (!d->engine || image.isNull())
5395 qreal x = targetRect.x();
5396 qreal y = targetRect.y();
5397 qreal w = targetRect.width();
5398 qreal h = targetRect.height();
5399 qreal sx = sourceRect.x();
5400 qreal sy = sourceRect.y();
5401 qreal sw = sourceRect.width();
5402 qreal sh = sourceRect.height();
5404 // Sanity-check clipping
5406 sw = image.width() - sx;
5409 sh = image.height() - sy;
5417 qreal w_ratio = sx * w/sw;
5425 qreal h_ratio = sy * h/sh;
5432 if (sw + sx > image.width()) {
5433 qreal delta = sw - (image.width() - sx);
5434 qreal w_ratio = delta * w/sw;
5439 if (sh + sy > image.height()) {
5440 qreal delta = sh - (image.height() - sy);
5441 qreal h_ratio = delta * h/sh;
5446 if (w == 0 || h == 0 || sw <= 0 || sh <= 0)
5450 d->extended->drawImage(QRectF(x, y, w, h), image, QRectF(sx, sy, sw, sh), flags);
5454 d->updateState(d->state);
5456 if (((d->state->matrix.type() > QTransform::TxTranslate || (sw != w || sh != h))
5457 && !d->engine->hasFeature(QPaintEngine::PixmapTransform))
5458 || (!d->state->matrix.isAffine() && !d->engine->hasFeature(QPaintEngine::PerspectiveTransform))
5459 || (d->state->opacity != 1.0 && !d->engine->hasFeature(QPaintEngine::ConstantOpacity)))
5462 // If there is no rotation involved we have to make sure we use the
5463 // antialiased and not the aliased coordinate system by rounding the coordinates.
5464 if (d->state->matrix.type() <= QTransform::TxScale) {
5465 const QPointF p = roundInDeviceCoordinates(QPointF(x, y), d->state->matrix);
5470 if (d->state->matrix.type() <= QTransform::TxTranslate && sw == w && sh == h) {
5477 scale(w / sw, h / sh);
5478 setBackgroundMode(Qt::TransparentMode);
5479 setRenderHint(Antialiasing, renderHints() & SmoothPixmapTransform);
5480 QBrush brush(image);
5483 setBrushOrigin(QPointF(-sx, -sy));
5485 drawRect(QRectF(0, 0, sw, sh));
5490 if (d->state->matrix.type() == QTransform::TxTranslate
5491 && !d->engine->hasFeature(QPaintEngine::PixmapTransform)) {
5492 x += d->state->matrix.dx();
5493 y += d->state->matrix.dy();
5496 d->engine->drawImage(QRectF(x, y, w, h), image, QRectF(sx, sy, sw, sh), flags);
5500 \fn void QPainter::drawGlyphRun(const QPointF &position, const QGlyphRun &glyphs)
5502 Draws the glyphs represented by \a glyphs at \a position. The \a position gives the
5503 edge of the baseline for the string of glyphs. The glyphs will be retrieved from the font
5504 selected on \a glyphs and at offsets given by the positions in \a glyphs.
5508 \sa QGlyphRun::setRawFont(), QGlyphRun::setPositions(), QGlyphRun::setGlyphIndexes()
5510 #if !defined(QT_NO_RAWFONT)
5511 void QPainter::drawGlyphRun(const QPointF &position, const QGlyphRun &glyphRun)
5515 QRawFont font = glyphRun.rawFont();
5516 if (!font.isValid())
5519 QGlyphRunPrivate *glyphRun_d = QGlyphRunPrivate::get(glyphRun);
5521 const quint32 *glyphIndexes = glyphRun_d->glyphIndexData;
5522 const QPointF *glyphPositions = glyphRun_d->glyphPositionData;
5524 int count = qMin(glyphRun_d->glyphIndexDataSize, glyphRun_d->glyphPositionDataSize);
5525 QVarLengthArray<QFixedPoint, 128> fixedPointPositions(count);
5527 QRawFontPrivate *fontD = QRawFontPrivate::get(font);
5528 bool supportsTransformations = d->extended
5529 ? d->extended->supportsTransformations(fontD->fontEngine, d->state->matrix)
5530 : d->engine->type() == QPaintEngine::CoreGraphics || d->state->matrix.isAffine();
5532 for (int i=0; i<count; ++i) {
5533 QPointF processedPosition = position + glyphPositions[i];
5534 if (!supportsTransformations)
5535 processedPosition = d->state->transform().map(processedPosition);
5536 fixedPointPositions[i] = QFixedPoint::fromPointF(processedPosition);
5539 d->drawGlyphs(glyphIndexes, fixedPointPositions.data(), count, font, glyphRun.overline(),
5540 glyphRun.underline(), glyphRun.strikeOut());
5543 void QPainterPrivate::drawGlyphs(const quint32 *glyphArray, QFixedPoint *positions,
5545 const QRawFont &font, bool overline, bool underline,
5552 QRawFontPrivate *fontD = QRawFontPrivate::get(font);
5553 QFontEngine *fontEngine = fontD->fontEngine;
5558 for (int i=0; i<glyphCount; ++i) {
5559 glyph_metrics_t gm = fontEngine->boundingBox(glyphArray[i]);
5560 if (i == 0 || leftMost > positions[i].x)
5561 leftMost = positions[i].x;
5563 // We don't support glyphs that do not share a common baseline. If this turns out to
5564 // be a relevant use case, then we need to find clusters of glyphs that share a baseline
5565 // and do a drawTextItemDecorations call per cluster.
5566 if (i == 0 || baseLine < positions[i].y)
5567 baseLine = positions[i].y;
5569 // We use the advance rather than the actual bounds to match the algorithm in drawText()
5570 if (i == 0 || rightMost < positions[i].x + gm.xoff)
5571 rightMost = positions[i].x + gm.xoff;
5574 QFixed width = rightMost - leftMost;
5576 if (extended != 0 && state->matrix.isAffine()) {
5577 QStaticTextItem staticTextItem;
5578 staticTextItem.color = state->pen.color();
5579 staticTextItem.font = state->font;
5580 staticTextItem.setFontEngine(fontEngine);
5581 staticTextItem.numGlyphs = glyphCount;
5582 staticTextItem.glyphs = reinterpret_cast<glyph_t *>(const_cast<glyph_t *>(glyphArray));
5583 staticTextItem.glyphPositions = positions;
5585 extended->drawStaticTextItem(&staticTextItem);
5587 QTextItemInt textItem;
5588 textItem.fontEngine = fontEngine;
5590 QVarLengthArray<QFixed, 128> advances(glyphCount);
5591 QVarLengthArray<QGlyphJustification, 128> glyphJustifications(glyphCount);
5592 QVarLengthArray<HB_GlyphAttributes, 128> glyphAttributes(glyphCount);
5593 memset(glyphAttributes.data(), 0, glyphAttributes.size() * sizeof(HB_GlyphAttributes));
5594 memset(advances.data(), 0, advances.size() * sizeof(QFixed));
5595 memset(glyphJustifications.data(), 0, glyphJustifications.size() * sizeof(QGlyphJustification));
5597 textItem.glyphs.numGlyphs = glyphCount;
5598 textItem.glyphs.glyphs = reinterpret_cast<HB_Glyph *>(const_cast<quint32 *>(glyphArray));
5599 textItem.glyphs.offsets = positions;
5600 textItem.glyphs.advances_x = advances.data();
5601 textItem.glyphs.advances_y = advances.data();
5602 textItem.glyphs.justifications = glyphJustifications.data();
5603 textItem.glyphs.attributes = glyphAttributes.data();
5605 engine->drawTextItem(QPointF(0, 0), textItem);
5608 QTextItemInt::RenderFlags flags;
5610 flags |= QTextItemInt::Underline;
5612 flags |= QTextItemInt::Overline;
5614 flags |= QTextItemInt::StrikeOut;
5616 drawTextItemDecoration(q, QPointF(leftMost.toReal(), baseLine.toReal()),
5620 ? QTextCharFormat::SingleUnderline
5621 : QTextCharFormat::NoUnderline),
5622 flags, width.toReal(), QTextCharFormat());
5624 #endif // QT_NO_RAWFONT
5628 \fn void QPainter::drawStaticText(const QPoint &topLeftPosition, const QStaticText &staticText)
5632 Draws the \a staticText at the \a topLeftPosition.
5634 \note The y-position is used as the top of the font.
5639 \fn void QPainter::drawStaticText(int left, int top, const QStaticText &staticText)
5643 Draws the \a staticText at coordinates \a left and \a top.
5645 \note The y-position is used as the top of the font.
5649 \fn void QPainter::drawText(const QPointF &position, const QString &text)
5651 Draws the given \a text with the currently defined text direction,
5652 beginning at the given \a position.
5654 This function does not handle the newline character (\n), as it cannot
5655 break text into multiple lines, and it cannot display the newline character.
5656 Use the QPainter::drawText() overload that takes a rectangle instead
5657 if you want to draw multiple lines of text with the newline character, or
5658 if you want the text to be wrapped.
5660 By default, QPainter draws text anti-aliased.
5662 \note The y-position is used as the baseline of the font.
5665 void QPainter::drawText(const QPointF &p, const QString &str)
5667 drawText(p, str, 0, 0);
5673 Draws the given \a staticText at the given \a topLeftPosition.
5675 The text will be drawn using the font and the transformation set on the painter. If the
5676 font and/or transformation set on the painter are different from the ones used to initialize
5677 the layout of the QStaticText, then the layout will have to be recalculated. Use
5678 QStaticText::prepare() to initialize \a staticText with the font and transformation with which
5679 it will later be drawn.
5681 If \a topLeftPosition is not the same as when \a staticText was initialized, or when it was
5682 last drawn, then there will be a slight overhead when translating the text to its new position.
5684 \note If the painter's transformation is not affine, then \a staticText will be drawn using
5685 regular calls to drawText(), losing any potential for performance improvement.
5687 \note The y-position is used as the top of the font.
5691 void QPainter::drawStaticText(const QPointF &topLeftPosition, const QStaticText &staticText)
5694 if (!d->engine || staticText.text().isEmpty() || pen().style() == Qt::NoPen)
5697 QStaticTextPrivate *staticText_d =
5698 const_cast<QStaticTextPrivate *>(QStaticTextPrivate::get(&staticText));
5700 if (font() != staticText_d->font) {
5701 staticText_d->font = font();
5702 staticText_d->needsRelayout = true;
5705 // If we don't have an extended paint engine, or if the painter is projected,
5706 // we go through standard code path
5707 if (d->extended == 0 || !d->state->matrix.isAffine()) {
5708 staticText_d->paintText(topLeftPosition, this);
5712 QFontEngine *fe = staticText_d->font.d->engineForScript(QUnicodeTables::Common);
5713 if (fe->type() == QFontEngine::Multi)
5714 fe = static_cast<QFontEngineMulti *>(fe)->engine(0);
5715 bool supportsTransformations = d->extended->supportsTransformations(fe,
5717 if (supportsTransformations && !staticText_d->untransformedCoordinates) {
5718 staticText_d->untransformedCoordinates = true;
5719 staticText_d->needsRelayout = true;
5720 } else if (!supportsTransformations && staticText_d->untransformedCoordinates) {
5721 staticText_d->untransformedCoordinates = false;
5722 staticText_d->needsRelayout = true;
5725 // Don't recalculate entire layout because of translation, rather add the dx and dy
5726 // into the position to move each text item the correct distance.
5727 QPointF transformedPosition = topLeftPosition;
5728 if (!staticText_d->untransformedCoordinates)
5729 transformedPosition = transformedPosition * d->state->matrix;
5730 QTransform oldMatrix;
5732 // The translation has been applied to transformedPosition. Remove translation
5733 // component from matrix.
5734 if (d->state->matrix.isTranslating() && !staticText_d->untransformedCoordinates) {
5735 qreal m11 = d->state->matrix.m11();
5736 qreal m12 = d->state->matrix.m12();
5737 qreal m13 = d->state->matrix.m13();
5738 qreal m21 = d->state->matrix.m21();
5739 qreal m22 = d->state->matrix.m22();
5740 qreal m23 = d->state->matrix.m23();
5741 qreal m33 = d->state->matrix.m33();
5743 oldMatrix = d->state->matrix;
5744 d->state->matrix.setMatrix(m11, m12, m13,
5749 // If the transform is not identical to the text transform,
5750 // we have to relayout the text (for other transformations than plain translation)
5751 bool staticTextNeedsReinit = staticText_d->needsRelayout;
5752 if (!staticText_d->untransformedCoordinates && staticText_d->matrix != d->state->matrix) {
5753 staticText_d->matrix = d->state->matrix;
5754 staticTextNeedsReinit = true;
5757 // Recreate the layout of the static text because the matrix or font has changed
5758 if (staticTextNeedsReinit)
5759 staticText_d->init();
5761 if (transformedPosition != staticText_d->position) { // Translate to actual position
5762 QFixed fx = QFixed::fromReal(transformedPosition.x());
5763 QFixed fy = QFixed::fromReal(transformedPosition.y());
5764 QFixed oldX = QFixed::fromReal(staticText_d->position.x());
5765 QFixed oldY = QFixed::fromReal(staticText_d->position.y());
5766 for (int item=0; item<staticText_d->itemCount;++item) {
5767 QStaticTextItem *textItem = staticText_d->items + item;
5768 for (int i=0; i<textItem->numGlyphs; ++i) {
5769 textItem->glyphPositions[i].x += fx - oldX;
5770 textItem->glyphPositions[i].y += fy - oldY;
5772 textItem->userDataNeedsUpdate = true;
5775 staticText_d->position = transformedPosition;
5778 QPen oldPen = d->state->pen;
5779 QColor currentColor = oldPen.color();
5780 for (int i=0; i<staticText_d->itemCount; ++i) {
5781 QStaticTextItem *item = staticText_d->items + i;
5782 if (item->color.isValid() && currentColor != item->color) {
5783 setPen(item->color);
5784 currentColor = item->color;
5786 d->extended->drawStaticTextItem(item);
5788 qt_draw_decoration_for_glyphs(this, item->glyphs, item->glyphPositions,
5789 item->numGlyphs, item->fontEngine(), staticText_d->font,
5792 if (currentColor != oldPen.color())
5795 if (!staticText_d->untransformedCoordinates && oldMatrix.isTranslating())
5796 d->state->matrix = oldMatrix;
5802 void QPainter::drawText(const QPointF &p, const QString &str, int tf, int justificationPadding)
5804 #ifdef QT_DEBUG_DRAW
5805 if (qt_show_painter_debug_output)
5806 printf("QPainter::drawText(), pos=[%.2f,%.2f], str='%s'\n", p.x(), p.y(), str.toLatin1().constData());
5811 if (!d->engine || str.isEmpty() || pen().style() == Qt::NoPen)
5814 if (tf & Qt::TextBypassShaping) {
5815 // Skip harfbuzz complex shaping, shape using glyph advances only
5816 int len = str.length();
5817 int numGlyphs = len;
5818 QVarLengthGlyphLayoutArray glyphs(len);
5819 QFontEngine *fontEngine = d->state->font.d->engineForScript(QUnicodeTables::Common);
5820 if (!fontEngine->stringToCMap(str.data(), len, &glyphs, &numGlyphs, 0)) {
5821 glyphs.resize(numGlyphs);
5822 if (!fontEngine->stringToCMap(str.data(), len, &glyphs, &numGlyphs, 0))
5823 Q_ASSERT_X(false, Q_FUNC_INFO, "stringToCMap shouldn't fail twice");
5826 QTextItemInt gf(glyphs, &d->state->font, str.data(), len, fontEngine);
5827 drawTextItem(p, gf);
5831 QStackTextEngine engine(str, d->state->font);
5832 engine.option.setTextDirection(d->state->layoutDirection);
5833 if (tf & (Qt::TextForceLeftToRight|Qt::TextForceRightToLeft)) {
5834 engine.ignoreBidi = true;
5835 engine.option.setTextDirection((tf & Qt::TextForceLeftToRight) ? Qt::LeftToRight : Qt::RightToLeft);
5839 line.length = str.length();
5840 engine.shapeLine(line);
5842 int nItems = engine.layoutData->items.size();
5843 QVarLengthArray<int> visualOrder(nItems);
5844 QVarLengthArray<uchar> levels(nItems);
5845 for (int i = 0; i < nItems; ++i)
5846 levels[i] = engine.layoutData->items[i].analysis.bidiLevel;
5847 QTextEngine::bidiReorder(nItems, levels.data(), visualOrder.data());
5849 if (justificationPadding > 0) {
5850 engine.option.setAlignment(Qt::AlignJustify);
5851 engine.forceJustification = true;
5852 // this works because justify() is only interested in the difference between width and textWidth
5853 line.width = justificationPadding;
5854 engine.justify(line);
5856 QFixed x = QFixed::fromReal(p.x());
5858 for (int i = 0; i < nItems; ++i) {
5859 int item = visualOrder[i];
5860 const QScriptItem &si = engine.layoutData->items.at(item);
5861 if (si.analysis.flags >= QScriptAnalysis::TabOrObject) {
5865 QFont f = engine.font(si);
5866 QTextItemInt gf(si, &f);
5867 gf.glyphs = engine.shapedGlyphs(&si);
5868 gf.chars = engine.layoutData->string.unicode() + si.position;
5869 gf.num_chars = engine.length(item);
5870 if (engine.forceJustification) {
5871 for (int j=0; j<gf.glyphs.numGlyphs; ++j)
5872 gf.width += gf.glyphs.effectiveAdvance(j);
5874 gf.width = si.width;
5876 gf.logClusters = engine.logClusters(&si);
5878 drawTextItem(QPointF(x.toReal(), p.y()), gf);
5884 void QPainter::drawText(const QRect &r, int flags, const QString &str, QRect *br)
5886 #ifdef QT_DEBUG_DRAW
5887 if (qt_show_painter_debug_output)
5888 printf("QPainter::drawText(), r=[%d,%d,%d,%d], flags=%d, str='%s'\n",
5889 r.x(), r.y(), r.width(), r.height(), flags, str.toLatin1().constData());
5894 if (!d->engine || str.length() == 0 || pen().style() == Qt::NoPen)
5898 d->updateState(d->state);
5901 qt_format_text(d->state->font, r, flags, 0, str, br ? &bounds : 0, 0, 0, 0, this);
5903 *br = bounds.toAlignedRect();
5907 \fn void QPainter::drawText(const QPoint &position, const QString &text)
5911 Draws the given \a text with the currently defined text direction,
5912 beginning at the given \a position.
5914 By default, QPainter draws text anti-aliased.
5916 \note The y-position is used as the baseline of the font.
5921 \fn void QPainter::drawText(const QRectF &rectangle, int flags, const QString &text, QRectF *boundingRect)
5924 Draws the given \a text within the provided \a rectangle.
5928 \li \inlineimage qpainter-text.png
5930 \snippet code/src_gui_painting_qpainter.cpp 17
5933 The \a boundingRect (if not null) is set to the what the bounding rectangle
5934 should be in order to enclose the whole text. The \a flags argument is a bitwise
5935 OR of the following flags:
5940 \li Qt::AlignHCenter
5941 \li Qt::AlignJustify
5944 \li Qt::AlignVCenter
5946 \li Qt::TextDontClip
5947 \li Qt::TextSingleLine
5948 \li Qt::TextExpandTabs
5949 \li Qt::TextShowMnemonic
5950 \li Qt::TextWordWrap
5951 \li Qt::TextIncludeTrailingSpaces
5954 \sa Qt::AlignmentFlag, Qt::TextFlag, boundingRect(), layoutDirection()
5956 By default, QPainter draws text anti-aliased.
5958 \note The y-coordinate of \a rectangle is used as the top of the font.
5960 void QPainter::drawText(const QRectF &r, int flags, const QString &str, QRectF *br)
5962 #ifdef QT_DEBUG_DRAW
5963 if (qt_show_painter_debug_output)
5964 printf("QPainter::drawText(), r=[%.2f,%.2f,%.2f,%.2f], flags=%d, str='%s'\n",
5965 r.x(), r.y(), r.width(), r.height(), flags, str.toLatin1().constData());
5970 if (!d->engine || str.length() == 0 || pen().style() == Qt::NoPen)
5974 d->updateState(d->state);
5976 qt_format_text(d->state->font, r, flags, 0, str, br, 0, 0, 0, this);
5980 \fn void QPainter::drawText(const QRect &rectangle, int flags, const QString &text, QRect *boundingRect)
5983 Draws the given \a text within the provided \a rectangle according
5984 to the specified \a flags. The \a boundingRect (if not null) is set to
5985 the what the bounding rectangle should be in order to enclose the whole text.
5987 By default, QPainter draws text anti-aliased.
5989 \note The y-coordinate of \a rectangle is used as the top of the font.
5993 \fn void QPainter::drawText(int x, int y, const QString &text)
5997 Draws the given \a text at position (\a{x}, \a{y}), using the painter's
5998 currently defined text direction.
6000 By default, QPainter draws text anti-aliased.
6002 \note The y-position is used as the baseline of the font.
6007 \fn void QPainter::drawText(int x, int y, int width, int height, int flags,
6008 const QString &text, QRect *boundingRect)
6012 Draws the given \a text within the rectangle with origin (\a{x},
6013 \a{y}), \a width and \a height.
6015 The \a boundingRect (if not null) is set to the actual bounding
6016 rectangle of the output. The \a flags argument is a bitwise OR of
6017 the following flags:
6022 \li Qt::AlignHCenter
6023 \li Qt::AlignJustify
6026 \li Qt::AlignVCenter
6028 \li Qt::TextSingleLine
6029 \li Qt::TextExpandTabs
6030 \li Qt::TextShowMnemonic
6031 \li Qt::TextWordWrap
6034 By default, QPainter draws text anti-aliased.
6036 \note The y-position is used as the top of the font.
6038 \sa Qt::AlignmentFlag, Qt::TextFlag
6042 \fn void QPainter::drawText(const QRectF &rectangle, const QString &text,
6043 const QTextOption &option)
6046 Draws the given \a text in the \a rectangle specified using the \a option
6047 to control its positioning and orientation.
6049 By default, QPainter draws text anti-aliased.
6051 \note The y-coordinate of \a rectangle is used as the top of the font.
6053 void QPainter::drawText(const QRectF &r, const QString &text, const QTextOption &o)
6055 #ifdef QT_DEBUG_DRAW
6056 if (qt_show_painter_debug_output)
6057 printf("QPainter::drawText(), r=[%.2f,%.2f,%.2f,%.2f], str='%s'\n",
6058 r.x(), r.y(), r.width(), r.height(), text.toLatin1().constData());
6063 if (!d->engine || text.length() == 0 || pen().style() == Qt::NoPen)
6067 d->updateState(d->state);
6069 qt_format_text(d->state->font, r, 0, &o, text, 0, 0, 0, 0, this);
6073 \fn void QPainter::drawTextItem(int x, int y, const QTextItem &ti)
6080 \fn void QPainter::drawTextItem(const QPoint &p, const QTextItem &ti)
6085 Draws the text item \a ti at position \a p.
6089 \fn void QPainter::drawTextItem(const QPointF &p, const QTextItem &ti)
6094 Draws the text item \a ti at position \a p.
6096 This method ignores the painters background mode and
6097 color. drawText and qt_format_text have to do it themselves, as
6098 only they know the extents of the complete string.
6100 It ignores the font set on the painter as the text item has one of its own.
6102 The underline and strikeout parameters of the text items font are
6103 ignored aswell. You'll need to pass in the correct flags to get
6104 underlining and strikeout.
6107 static QPixmap generateWavyPixmap(qreal maxRadius, const QPen &pen)
6109 const qreal radiusBase = qMax(qreal(1), maxRadius);
6111 QString key = QLatin1String("WaveUnderline-")
6112 % pen.color().name()
6113 % HexString<qreal>(radiusBase);
6116 if (QPixmapCache::find(key, pixmap))
6119 const qreal halfPeriod = qMax(qreal(2), qreal(radiusBase * 1.61803399)); // the golden ratio
6120 const int width = qCeil(100 / (2 * halfPeriod)) * (2 * halfPeriod);
6121 const int radius = qFloor(radiusBase);
6128 while (xs < width) {
6131 path.quadTo(xs - halfPeriod / 2, ys, xs, 0);
6134 pixmap = QPixmap(width, radius * 2);
6135 pixmap.fill(Qt::transparent);
6138 wavePen.setCapStyle(Qt::SquareCap);
6140 // This is to protect against making the line too fat, as happens on Mac OS X
6141 // due to it having a rather thick width for the regular underline.
6142 const qreal maxPenWidth = .8 * radius;
6143 if (wavePen.widthF() > maxPenWidth)
6144 wavePen.setWidth(maxPenWidth);
6146 QPainter imgPainter(&pixmap);
6147 imgPainter.setPen(wavePen);
6148 imgPainter.setRenderHint(QPainter::Antialiasing);
6149 imgPainter.translate(0, radius);
6150 imgPainter.drawPath(path);
6153 QPixmapCache::insert(key, pixmap);
6158 static void drawTextItemDecoration(QPainter *painter, const QPointF &pos, const QFontEngine *fe, QTextEngine *textEngine,
6159 QTextCharFormat::UnderlineStyle underlineStyle,
6160 QTextItem::RenderFlags flags, qreal width,
6161 const QTextCharFormat &charFormat)
6163 if (underlineStyle == QTextCharFormat::NoUnderline
6164 && !(flags & (QTextItem::StrikeOut | QTextItem::Overline)))
6167 const QPen oldPen = painter->pen();
6168 const QBrush oldBrush = painter->brush();
6169 painter->setBrush(Qt::NoBrush);
6171 pen.setStyle(Qt::SolidLine);
6172 pen.setWidthF(fe->lineThickness().toReal());
6173 pen.setCapStyle(Qt::FlatCap);
6175 QLineF line(pos.x(), pos.y(), pos.x() + qFloor(width), pos.y());
6177 const qreal underlineOffset = fe->underlinePosition().toReal();
6178 // deliberately ceil the offset to avoid the underline coming too close to
6179 // the text above it.
6180 const qreal aliasedCoordinateDelta = 0.5 - 0.015625;
6181 const qreal underlinePos = pos.y() + qCeil(underlineOffset) - aliasedCoordinateDelta;
6183 if (underlineStyle == QTextCharFormat::SpellCheckUnderline) {
6184 underlineStyle = QTextCharFormat::SpellCheckUnderline; // ### Qt5 QTextCharFormat::UnderlineStyle(QApplication::style()->styleHint(QStyle::SH_SpellCheckUnderlineStyle));
6187 if (underlineStyle == QTextCharFormat::WaveUnderline) {
6189 painter->translate(0, pos.y() + 1);
6191 QColor uc = charFormat.underlineColor();
6195 // Adapt wave to underlineOffset or pen width, whatever is larger, to make it work on all platforms
6196 const QPixmap wave = generateWavyPixmap(qMax(underlineOffset, pen.widthF()), pen);
6197 const int descent = (int) fe->descent().toReal();
6199 painter->setBrushOrigin(painter->brushOrigin().x(), 0);
6200 painter->fillRect(pos.x(), 0, qCeil(width), qMin(wave.height(), descent), wave);
6202 } else if (underlineStyle != QTextCharFormat::NoUnderline) {
6203 QColor uc = charFormat.underlineColor();
6207 pen.setStyle((Qt::PenStyle)(underlineStyle));
6208 painter->setPen(pen);
6209 QLineF underline(line.x1(), underlinePos, line.x2(), underlinePos);
6211 textEngine->addUnderline(painter, underline);
6213 painter->drawLine(underline);
6216 pen.setStyle(Qt::SolidLine);
6217 pen.setColor(oldPen.color());
6219 if (flags & QTextItem::StrikeOut) {
6220 QLineF strikeOutLine = line;
6221 strikeOutLine.translate(0., - fe->ascent().toReal() / 3.);
6222 painter->setPen(pen);
6224 textEngine->addStrikeOut(painter, strikeOutLine);
6226 painter->drawLine(strikeOutLine);
6229 if (flags & QTextItem::Overline) {
6230 QLineF overline = line;
6231 overline.translate(0., - fe->ascent().toReal());
6232 painter->setPen(pen);
6234 textEngine->addOverline(painter, overline);
6236 painter->drawLine(overline);
6239 painter->setPen(oldPen);
6240 painter->setBrush(oldBrush);
6243 Q_GUI_EXPORT void qt_draw_decoration_for_glyphs(QPainter *painter, const glyph_t *glyphArray,
6244 const QFixedPoint *positions, int glyphCount,
6245 QFontEngine *fontEngine, const QFont &font,
6246 const QTextCharFormat &charFormat)
6248 if (!(font.underline() || font.strikeOut() || font.overline()))
6254 for (int i=0; i<glyphCount; ++i) {
6255 glyph_metrics_t gm = fontEngine->boundingBox(glyphArray[i]);
6256 if (i == 0 || leftMost > positions[i].x)
6257 leftMost = positions[i].x;
6259 // We don't support glyphs that do not share a common baseline. If this turns out to
6260 // be a relevant use case, then we need to find clusters of glyphs that share a baseline
6261 // and do a drawTextItemDecoration call per cluster.
6262 if (i == 0 || baseLine < positions[i].y)
6263 baseLine = positions[i].y;
6265 // We use the advance rather than the actual bounds to match the algorithm in drawText()
6266 if (i == 0 || rightMost < positions[i].x + gm.xoff)
6267 rightMost = positions[i].x + gm.xoff;
6270 QFixed width = rightMost - leftMost;
6271 QTextItem::RenderFlags flags = 0;
6273 if (font.underline())
6274 flags |= QTextItem::Underline;
6275 if (font.overline())
6276 flags |= QTextItem::Overline;
6277 if (font.strikeOut())
6278 flags |= QTextItem::StrikeOut;
6280 drawTextItemDecoration(painter, QPointF(leftMost.toReal(), baseLine.toReal()),
6283 font.underline() ? QTextCharFormat::SingleUnderline
6284 : QTextCharFormat::NoUnderline, flags,
6285 width.toReal(), charFormat);
6288 void QPainter::drawTextItem(const QPointF &p, const QTextItem &ti)
6292 d->drawTextItem(p, ti, static_cast<QTextEngine *>(0));
6295 void QPainterPrivate::drawTextItem(const QPointF &p, const QTextItem &_ti, QTextEngine *textEngine)
6297 #ifdef QT_DEBUG_DRAW
6298 if (qt_show_painter_debug_output)
6299 printf("QPainter::drawTextItem(), pos=[%.f,%.f], str='%s'\n",
6300 p.x(), p.y(), qPrintable(_ti.text()));
6309 qt_painter_thread_test(device->devType(),
6311 QFontDatabase::supportsThreadedFontRendering());
6314 QTextItemInt &ti = const_cast<QTextItemInt &>(static_cast<const QTextItemInt &>(_ti));
6316 if (!extended && state->bgMode == Qt::OpaqueMode) {
6317 QRectF rect(p.x(), p.y() - ti.ascent.toReal(), ti.width.toReal(), (ti.ascent + ti.descent + 1).toReal());
6318 q->fillRect(rect, state->bgBrush);
6321 if (q->pen().style() == Qt::NoPen)
6324 const QPainter::RenderHints oldRenderHints = state->renderHints;
6325 if (!state->renderHints & QPainter::Antialiasing && state->matrix.type() >= QTransform::TxScale) {
6326 // draw antialias decoration (underline/overline/strikeout) with
6330 const QTransform &m = state->matrix;
6331 if (state->matrix.type() < QTransform::TxShear) {
6332 bool isPlain90DegreeRotation =
6333 (qFuzzyIsNull(m.m11())
6334 && qFuzzyIsNull(m.m12() - qreal(1))
6335 && qFuzzyIsNull(m.m21() + qreal(1))
6336 && qFuzzyIsNull(m.m22())
6339 (qFuzzyIsNull(m.m11() + qreal(1))
6340 && qFuzzyIsNull(m.m12())
6341 && qFuzzyIsNull(m.m21())
6342 && qFuzzyIsNull(m.m22() + qreal(1))
6345 (qFuzzyIsNull(m.m11())
6346 && qFuzzyIsNull(m.m12() + qreal(1))
6347 && qFuzzyIsNull(m.m21() - qreal(1))
6348 && qFuzzyIsNull(m.m22())
6351 aa = !isPlain90DegreeRotation;
6354 q->setRenderHint(QPainter::Antialiasing, true);
6360 if (!ti.glyphs.numGlyphs) {
6362 } else if (ti.fontEngine->type() == QFontEngine::Multi) {
6363 QFontEngineMulti *multi = static_cast<QFontEngineMulti *>(ti.fontEngine);
6365 const QGlyphLayout &glyphs = ti.glyphs;
6366 int which = glyphs.glyphs[0] >> 24;
6371 bool rtl = ti.flags & QTextItem::RightToLeft;
6373 x += ti.width.toReal();
6377 for (end = 0; end < ti.glyphs.numGlyphs; ++end) {
6378 const int e = glyphs.glyphs[end] >> 24;
6383 QTextItemInt ti2 = ti.midItem(multi->engine(which), start, end - start);
6385 // set the high byte to zero and calc the width
6386 for (i = start; i < end; ++i) {
6387 glyphs.glyphs[i] = glyphs.glyphs[i] & 0xffffff;
6388 ti2.width += ti.glyphs.effectiveAdvance(i);
6392 x -= ti2.width.toReal();
6394 engine->drawTextItem(QPointF(x, y), ti2);
6397 x += ti2.width.toReal();
6399 // reset the high byte for all glyphs and advance to the next sub-string
6400 const int hi = which << 24;
6401 for (i = start; i < end; ++i) {
6402 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
6410 QTextItemInt ti2 = ti.midItem(multi->engine(which), start, end - start);
6412 // set the high byte to zero and calc the width
6413 for (i = start; i < end; ++i) {
6414 glyphs.glyphs[i] = glyphs.glyphs[i] & 0xffffff;
6415 ti2.width += ti.glyphs.effectiveAdvance(i);
6419 x -= ti2.width.toReal();
6422 extended->drawTextItem(QPointF(x, y), ti2);
6424 engine->drawTextItem(QPointF(x,y), ti2);
6426 // reset the high byte for all glyphs
6427 const int hi = which << 24;
6428 for (i = start; i < end; ++i)
6429 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
6433 extended->drawTextItem(p, ti);
6435 engine->drawTextItem(p, ti);
6437 drawTextItemDecoration(q, p, ti.fontEngine, textEngine, ti.underlineStyle,
6438 ti.flags, ti.width.toReal(), ti.charFormat);
6440 if (state->renderHints != oldRenderHints) {
6441 state->renderHints = oldRenderHints;
6443 extended->renderHintsChanged();
6445 state->dirtyFlags |= QPaintEngine::DirtyHints;
6450 \fn QRectF QPainter::boundingRect(const QRectF &rectangle, int flags, const QString &text)
6452 Returns the bounding rectangle of the \a text as it will appear
6453 when drawn inside the given \a rectangle with the specified \a
6454 flags using the currently set font(); i.e the function tells you
6455 where the drawText() function will draw when given the same
6458 If the \a text does not fit within the given \a rectangle using
6459 the specified \a flags, the function returns the required
6462 The \a flags argument is a bitwise OR of the following flags:
6466 \li Qt::AlignHCenter
6469 \li Qt::AlignVCenter
6471 \li Qt::TextSingleLine
6472 \li Qt::TextExpandTabs
6473 \li Qt::TextShowMnemonic
6474 \li Qt::TextWordWrap
6475 \li Qt::TextIncludeTrailingSpaces
6477 If several of the horizontal or several of the vertical alignment
6478 flags are set, the resulting alignment is undefined.
6480 \sa drawText(), Qt::Alignment, Qt::TextFlag
6484 \fn QRect QPainter::boundingRect(const QRect &rectangle, int flags,
6485 const QString &text)
6489 Returns the bounding rectangle of the \a text as it will appear
6490 when drawn inside the given \a rectangle with the specified \a
6491 flags using the currently set font().
6495 \fn QRect QPainter::boundingRect(int x, int y, int w, int h, int flags,
6496 const QString &text);
6500 Returns the bounding rectangle of the given \a text as it will
6501 appear when drawn inside the rectangle beginning at the point
6502 (\a{x}, \a{y}) with width \a w and height \a h.
6504 QRect QPainter::boundingRect(const QRect &rect, int flags, const QString &str)
6507 return QRect(rect.x(),rect.y(), 0,0);
6509 drawText(rect, flags | Qt::TextDontPrint, str, &brect);
6515 QRectF QPainter::boundingRect(const QRectF &rect, int flags, const QString &str)
6518 return QRectF(rect.x(),rect.y(), 0,0);
6520 drawText(rect, flags | Qt::TextDontPrint, str, &brect);
6525 \fn QRectF QPainter::boundingRect(const QRectF &rectangle,
6526 const QString &text, const QTextOption &option)
6530 Instead of specifying flags as a bitwise OR of the
6531 Qt::AlignmentFlag and Qt::TextFlag, this overloaded function takes
6532 an \a option argument. The QTextOption class provides a
6533 description of general rich text properties.
6537 QRectF QPainter::boundingRect(const QRectF &r, const QString &text, const QTextOption &o)
6541 if (!d->engine || text.length() == 0)
6542 return QRectF(r.x(),r.y(), 0,0);
6545 qt_format_text(d->state->font, r, Qt::TextDontPrint, &o, text, &br, 0, 0, 0, this);
6550 \fn void QPainter::drawTiledPixmap(const QRectF &rectangle, const QPixmap &pixmap, const QPointF &position)
6552 Draws a tiled \a pixmap, inside the given \a rectangle with its
6553 origin at the given \a position.
6555 Calling drawTiledPixmap() is similar to calling drawPixmap()
6556 several times to fill (tile) an area with a pixmap, but is
6557 potentially much more efficient depending on the underlying window
6562 void QPainter::drawTiledPixmap(const QRectF &r, const QPixmap &pixmap, const QPointF &sp)
6564 #ifdef QT_DEBUG_DRAW
6565 if (qt_show_painter_debug_output)
6566 printf("QPainter::drawTiledPixmap(), target=[%.2f,%.2f,%.2f,%.2f], pix=[%d,%d], offset=[%.2f,%.2f]\n",
6567 r.x(), r.y(), r.width(), r.height(),
6568 pixmap.width(), pixmap.height(),
6573 if (!d->engine || pixmap.isNull() || r.isEmpty())
6577 qt_painter_thread_test(d->device->devType(), "drawTiledPixmap()", true);
6580 qreal sw = pixmap.width();
6581 qreal sh = pixmap.height();
6585 sx = qRound(sw) - qRound(-sx) % qRound(sw);
6587 sx = qRound(sx) % qRound(sw);
6589 sy = qRound(sh) - -qRound(sy) % qRound(sh);
6591 sy = qRound(sy) % qRound(sh);
6595 d->extended->drawTiledPixmap(r, pixmap, QPointF(sx, sy));
6599 if (d->state->bgMode == Qt::OpaqueMode && pixmap.isQBitmap())
6600 fillRect(r, d->state->bgBrush);
6602 d->updateState(d->state);
6603 if ((d->state->matrix.type() > QTransform::TxTranslate
6604 && !d->engine->hasFeature(QPaintEngine::PixmapTransform))
6605 || (d->state->opacity != 1.0 && !d->engine->hasFeature(QPaintEngine::ConstantOpacity)))
6608 setBackgroundMode(Qt::TransparentMode);
6609 setRenderHint(Antialiasing, renderHints() & SmoothPixmapTransform);
6610 setBrush(QBrush(d->state->pen.color(), pixmap));
6613 // If there is no rotation involved we have to make sure we use the
6614 // antialiased and not the aliased coordinate system by rounding the coordinates.
6615 if (d->state->matrix.type() <= QTransform::TxScale) {
6616 const QPointF p = roundInDeviceCoordinates(r.topLeft(), d->state->matrix);
6618 if (d->state->matrix.type() <= QTransform::TxTranslate) {
6623 setBrushOrigin(QPointF(r.x()-sx, r.y()-sy));
6624 drawRect(QRectF(p, r.size()));
6626 setBrushOrigin(QPointF(r.x()-sx, r.y()-sy));
6635 if (d->state->matrix.type() == QTransform::TxTranslate
6636 && !d->engine->hasFeature(QPaintEngine::PixmapTransform)) {
6637 x += d->state->matrix.dx();
6638 y += d->state->matrix.dy();
6641 d->engine->drawTiledPixmap(QRectF(x, y, r.width(), r.height()), pixmap, QPointF(sx, sy));
6645 \fn QPainter::drawTiledPixmap(const QRect &rectangle, const QPixmap &pixmap,
6646 const QPoint &position = QPoint())
6649 Draws a tiled \a pixmap, inside the given \a rectangle with its
6650 origin at the given \a position.
6654 \fn void QPainter::drawTiledPixmap(int x, int y, int width, int height, const
6655 QPixmap &pixmap, int sx, int sy);
6658 Draws a tiled \a pixmap in the specified rectangle.
6660 (\a{x}, \a{y}) specifies the top-left point in the paint device
6661 that is to be drawn onto; with the given \a width and \a
6662 height. (\a{sx}, \a{sy}) specifies the top-left point in the \a
6663 pixmap that is to be drawn; this defaults to (0, 0).
6666 #ifndef QT_NO_PICTURE
6669 \fn void QPainter::drawPicture(const QPointF &point, const QPicture &picture)
6671 Replays the given \a picture at the given \a point.
6673 The QPicture class is a paint device that records and replays
6674 QPainter commands. A picture serializes the painter commands to an
6675 IO device in a platform-independent format. Everything that can be
6676 painted on a widget or pixmap can also be stored in a picture.
6678 This function does exactly the same as QPicture::play() when
6679 called with \a point = QPoint(0, 0).
6684 \snippet code/src_gui_painting_qpainter.cpp 18
6687 \sa QPicture::play()
6690 void QPainter::drawPicture(const QPointF &p, const QPicture &picture)
6698 d->updateState(d->state);
6702 const_cast<QPicture *>(&picture)->play(this);
6707 \fn void QPainter::drawPicture(const QPoint &point, const QPicture &picture)
6710 Replays the given \a picture at the given \a point.
6714 \fn void QPainter::drawPicture(int x, int y, const QPicture &picture)
6717 Draws the given \a picture at point (\a x, \a y).
6720 #endif // QT_NO_PICTURE
6723 \fn void QPainter::eraseRect(const QRectF &rectangle)
6725 Erases the area inside the given \a rectangle. Equivalent to
6727 \snippet code/src_gui_painting_qpainter.cpp 19
6731 void QPainter::eraseRect(const QRectF &r)
6735 fillRect(r, d->state->bgBrush);
6738 static inline bool needsResolving(const QBrush &brush)
6740 Qt::BrushStyle s = brush.style();
6741 return ((s == Qt::LinearGradientPattern || s == Qt::RadialGradientPattern ||
6742 s == Qt::ConicalGradientPattern) &&
6743 brush.gradient()->coordinateMode() == QGradient::ObjectBoundingMode);
6747 \fn void QPainter::eraseRect(const QRect &rectangle)
6750 Erases the area inside the given \a rectangle.
6754 \fn void QPainter::eraseRect(int x, int y, int width, int height)
6757 Erases the area inside the rectangle beginning at (\a x, \a y)
6758 with the given \a width and \a height.
6763 \fn void QPainter::fillRect(int x, int y, int width, int height, Qt::BrushStyle style)
6766 Fills the rectangle beginning at (\a{x}, \a{y}) with the given \a
6767 width and \a height, using the brush \a style specified.
6773 \fn void QPainter::fillRect(const QRect &rectangle, Qt::BrushStyle style)
6776 Fills the given \a rectangle with the brush \a style specified.
6782 \fn void QPainter::fillRect(const QRectF &rectangle, Qt::BrushStyle style)
6785 Fills the given \a rectangle with the brush \a style specified.
6791 \fn void QPainter::fillRect(const QRectF &rectangle, const QBrush &brush)
6793 Fills the given \a rectangle with the \a brush specified.
6795 Alternatively, you can specify a QColor instead of a QBrush; the
6796 QBrush constructor (taking a QColor argument) will automatically
6797 create a solid pattern brush.
6801 void QPainter::fillRect(const QRectF &r, const QBrush &brush)
6809 const QGradient *g = brush.gradient();
6810 if (!g || g->coordinateMode() == QGradient::LogicalMode) {
6811 d->extended->fillRect(r, brush);
6816 QPen oldPen = pen();
6817 QBrush oldBrush = this->brush();
6819 if (brush.style() == Qt::SolidPattern) {
6820 d->colorBrush.setStyle(Qt::SolidPattern);
6821 d->colorBrush.setColor(brush.color());
6822 setBrush(d->colorBrush);
6833 \fn void QPainter::fillRect(const QRect &rectangle, const QBrush &brush)
6836 Fills the given \a rectangle with the specified \a brush.
6839 void QPainter::fillRect(const QRect &r, const QBrush &brush)
6847 const QGradient *g = brush.gradient();
6848 if (!g || g->coordinateMode() == QGradient::LogicalMode) {
6849 d->extended->fillRect(r, brush);
6854 QPen oldPen = pen();
6855 QBrush oldBrush = this->brush();
6857 if (brush.style() == Qt::SolidPattern) {
6858 d->colorBrush.setStyle(Qt::SolidPattern);
6859 d->colorBrush.setColor(brush.color());
6860 setBrush(d->colorBrush);
6873 \fn void QPainter::fillRect(const QRect &rectangle, const QColor &color)
6876 Fills the given \a rectangle with the \a color specified.
6880 void QPainter::fillRect(const QRect &r, const QColor &color)
6888 d->extended->fillRect(r, color);
6892 fillRect(r, QBrush(color));
6897 \fn void QPainter::fillRect(const QRectF &rectangle, const QColor &color)
6900 Fills the given \a rectangle with the \a color specified.
6904 void QPainter::fillRect(const QRectF &r, const QColor &color)
6912 d->extended->fillRect(r, color);
6916 fillRect(r, QBrush(color));
6920 \fn void QPainter::fillRect(int x, int y, int width, int height, const QBrush &brush)
6924 Fills the rectangle beginning at (\a{x}, \a{y}) with the given \a
6925 width and \a height, using the given \a brush.
6929 \fn void QPainter::fillRect(int x, int y, int width, int height, const QColor &color)
6933 Fills the rectangle beginning at (\a{x}, \a{y}) with the given \a
6934 width and \a height, using the given \a color.
6940 \fn void QPainter::fillRect(int x, int y, int width, int height, Qt::GlobalColor color)
6944 Fills the rectangle beginning at (\a{x}, \a{y}) with the given \a
6945 width and \a height, using the given \a color.
6951 \fn void QPainter::fillRect(const QRect &rectangle, Qt::GlobalColor color);
6955 Fills the given \a rectangle with the specified \a color.
6961 \fn void QPainter::fillRect(const QRectF &rectangle, Qt::GlobalColor color);
6965 Fills the given \a rectangle with the specified \a color.
6971 Sets the given render \a hint on the painter if \a on is true;
6972 otherwise clears the render hint.
6974 \sa setRenderHints(), renderHints(), {QPainter#Rendering
6975 Quality}{Rendering Quality}
6977 void QPainter::setRenderHint(RenderHint hint, bool on)
6979 #ifdef QT_DEBUG_DRAW
6980 if (qt_show_painter_debug_output)
6981 printf("QPainter::setRenderHint: hint=%x, %s\n", hint, on ? "on" : "off");
6985 static const bool antialiasingDisabled = qgetenv("QT_NO_ANTIALIASING").toInt();
6986 if (hint == QPainter::Antialiasing && antialiasingDisabled)
6990 setRenderHints(hint, on);
6996 Sets the given render \a hints on the painter if \a on is true;
6997 otherwise clears the render hints.
6999 \sa setRenderHint(), renderHints(), {QPainter#Rendering
7000 Quality}{Rendering Quality}
7003 void QPainter::setRenderHints(RenderHints hints, bool on)
7008 qWarning("QPainter::setRenderHint: Painter must be active to set rendering hints");
7013 d->state->renderHints |= hints;
7015 d->state->renderHints &= ~hints;
7018 d->extended->renderHintsChanged();
7020 d->state->dirtyFlags |= QPaintEngine::DirtyHints;
7024 Returns a flag that specifies the rendering hints that are set for
7027 \sa testRenderHint(), {QPainter#Rendering Quality}{Rendering Quality}
7029 QPainter::RenderHints QPainter::renderHints() const
7031 Q_D(const QPainter);
7036 return d->state->renderHints;
7040 \fn bool QPainter::testRenderHint(RenderHint hint) const
7043 Returns true if \a hint is set; otherwise returns false.
7045 \sa renderHints(), setRenderHint()
7049 Returns true if view transformation is enabled; otherwise returns
7052 \sa setViewTransformEnabled(), worldTransform()
7055 bool QPainter::viewTransformEnabled() const
7057 Q_D(const QPainter);
7059 qWarning("QPainter::viewTransformEnabled: Painter not active");
7062 return d->state->VxF;
7066 \fn void QPainter::setWindow(const QRect &rectangle)
7068 Sets the painter's window to the given \a rectangle, and enables
7069 view transformations.
7071 The window rectangle is part of the view transformation. The
7072 window specifies the logical coordinate system. Its sister, the
7073 viewport(), specifies the device coordinate system.
7075 The default window rectangle is the same as the device's
7078 \sa window(), viewTransformEnabled(), {Coordinate
7079 System#Window-Viewport Conversion}{Window-Viewport Conversion}
7083 \fn void QPainter::setWindow(int x, int y, int width, int height)
7086 Sets the painter's window to the rectangle beginning at (\a x, \a
7087 y) and the given \a width and \a height.
7090 void QPainter::setWindow(const QRect &r)
7092 #ifdef QT_DEBUG_DRAW
7093 if (qt_show_painter_debug_output)
7094 printf("QPainter::setWindow(), [%d,%d,%d,%d]\n", r.x(), r.y(), r.width(), r.height());
7100 qWarning("QPainter::setWindow: Painter not active");
7104 d->state->wx = r.x();
7105 d->state->wy = r.y();
7106 d->state->ww = r.width();
7107 d->state->wh = r.height();
7109 d->state->VxF = true;
7114 Returns the window rectangle.
7116 \sa setWindow(), setViewTransformEnabled()
7119 QRect QPainter::window() const
7121 Q_D(const QPainter);
7123 qWarning("QPainter::window: Painter not active");
7126 return QRect(d->state->wx, d->state->wy, d->state->ww, d->state->wh);
7130 \fn void QPainter::setViewport(const QRect &rectangle)
7132 Sets the painter's viewport rectangle to the given \a rectangle,
7133 and enables view transformations.
7135 The viewport rectangle is part of the view transformation. The
7136 viewport specifies the device coordinate system. Its sister, the
7137 window(), specifies the logical coordinate system.
7139 The default viewport rectangle is the same as the device's
7142 \sa viewport(), viewTransformEnabled(), {Coordinate
7143 System#Window-Viewport Conversion}{Window-Viewport Conversion}
7147 \fn void QPainter::setViewport(int x, int y, int width, int height)
7150 Sets the painter's viewport rectangle to be the rectangle
7151 beginning at (\a x, \a y) with the given \a width and \a height.
7154 void QPainter::setViewport(const QRect &r)
7156 #ifdef QT_DEBUG_DRAW
7157 if (qt_show_painter_debug_output)
7158 printf("QPainter::setViewport(), [%d,%d,%d,%d]\n", r.x(), r.y(), r.width(), r.height());
7164 qWarning("QPainter::setViewport: Painter not active");
7168 d->state->vx = r.x();
7169 d->state->vy = r.y();
7170 d->state->vw = r.width();
7171 d->state->vh = r.height();
7173 d->state->VxF = true;
7178 Returns the viewport rectangle.
7180 \sa setViewport(), setViewTransformEnabled()
7183 QRect QPainter::viewport() const
7185 Q_D(const QPainter);
7187 qWarning("QPainter::viewport: Painter not active");
7190 return QRect(d->state->vx, d->state->vy, d->state->vw, d->state->vh);
7194 Enables view transformations if \a enable is true, or disables
7195 view transformations if \a enable is false.
7197 \sa viewTransformEnabled(), {Coordinate System#Window-Viewport
7198 Conversion}{Window-Viewport Conversion}
7201 void QPainter::setViewTransformEnabled(bool enable)
7203 #ifdef QT_DEBUG_DRAW
7204 if (qt_show_painter_debug_output)
7205 printf("QPainter::setViewTransformEnabled(), enable=%d\n", enable);
7211 qWarning("QPainter::setViewTransformEnabled: Painter not active");
7215 if (enable == d->state->VxF)
7218 d->state->VxF = enable;
7227 Please use QWidget::render() instead.
7229 Redirects all paint commands for the given paint \a device, to the
7230 \a replacement device. The optional point \a offset defines an
7231 offset within the source device.
7233 The redirection will not be effective until the begin() function
7234 has been called; make sure to call end() for the given \a
7235 device's painter (if any) before redirecting. Call
7236 restoreRedirected() to restore the previous redirection.
7238 \warning Making use of redirections in the QPainter API implies
7239 that QPainter::begin() and QPaintDevice destructors need to hold
7240 a mutex for a short period. This can impact performance. Use of
7241 QWidget::render is strongly encouraged.
7243 \sa redirected(), restoreRedirected()
7245 void QPainter::setRedirected(const QPaintDevice *device,
7246 QPaintDevice *replacement,
7247 const QPoint &offset)
7249 Q_ASSERT(device != 0);
7251 Q_UNUSED(replacement)
7253 qWarning("QPainter::setRedirected(): ignoring call to deprecated function, use QWidget::render() instead");
7261 Using QWidget::render() obsoletes the use of this function.
7263 Restores the previous redirection for the given \a device after a
7264 call to setRedirected().
7266 \warning Making use of redirections in the QPainter API implies
7267 that QPainter::begin() and QPaintDevice destructors need to hold
7268 a mutex for a short period. This can impact performance. Use of
7269 QWidget::render is strongly encouraged.
7273 void QPainter::restoreRedirected(const QPaintDevice *device)
7276 qWarning("QPainter::restoreRedirected(): ignoring call to deprecated function, use QWidget::render() instead");
7284 Using QWidget::render() obsoletes the use of this function.
7286 Returns the replacement for given \a device. The optional out
7287 parameter \a offset returns the offset within the replaced device.
7289 \warning Making use of redirections in the QPainter API implies
7290 that QPainter::begin() and QPaintDevice destructors need to hold
7291 a mutex for a short period. This can impact performance. Use of
7292 QWidget::render is strongly encouraged.
7294 \sa setRedirected(), restoreRedirected()
7296 QPaintDevice *QPainter::redirected(const QPaintDevice *device, QPoint *offset)
7303 void qt_format_text(const QFont &fnt, const QRectF &_r,
7304 int tf, const QString& str, QRectF *brect,
7305 int tabstops, int *ta, int tabarraylen,
7308 qt_format_text(fnt, _r,
7310 tabstops, ta, tabarraylen,
7313 void qt_format_text(const QFont &fnt, const QRectF &_r,
7314 int tf, const QTextOption *option, const QString& str, QRectF *brect,
7315 int tabstops, int *ta, int tabarraylen,
7319 Q_ASSERT( !((tf & ~Qt::TextDontPrint)!=0 && option!=0) ); // we either have an option or flags
7322 tf |= option->alignment();
7323 if (option->wrapMode() != QTextOption::NoWrap)
7324 tf |= Qt::TextWordWrap;
7326 if (option->flags() & QTextOption::IncludeTrailingSpaces)
7327 tf |= Qt::TextIncludeTrailingSpaces;
7329 if (option->tabStop() >= 0 || !option->tabArray().isEmpty())
7330 tf |= Qt::TextExpandTabs;
7333 // we need to copy r here to protect against the case (&r == brect).
7336 bool dontclip = (tf & Qt::TextDontClip);
7337 bool wordwrap = (tf & Qt::TextWordWrap) || (tf & Qt::TextWrapAnywhere);
7338 bool singleline = (tf & Qt::TextSingleLine);
7339 bool showmnemonic = (tf & Qt::TextShowMnemonic);
7340 bool hidemnmemonic = (tf & Qt::TextHideMnemonic);
7342 Qt::LayoutDirection layout_direction;
7343 if (tf & Qt::TextForceLeftToRight)
7344 layout_direction = Qt::LeftToRight;
7345 else if (tf & Qt::TextForceRightToLeft)
7346 layout_direction = Qt::RightToLeft;
7348 layout_direction = option->textDirection();
7350 layout_direction = painter->layoutDirection();
7352 layout_direction = Qt::LeftToRight;
7354 tf = QGuiApplicationPrivate::visualAlignment(layout_direction, QFlag(tf));
7356 bool isRightToLeft = layout_direction == Qt::RightToLeft;
7357 bool expandtabs = ((tf & Qt::TextExpandTabs) &&
7358 (((tf & Qt::AlignLeft) && !isRightToLeft) ||
7359 ((tf & Qt::AlignRight) && isRightToLeft)));
7362 tf |= Qt::TextDontPrint;
7364 uint maxUnderlines = 0;
7365 int numUnderlines = 0;
7366 QVarLengthArray<int, 32> underlinePositions(1);
7368 QFontMetricsF fm(fnt);
7371 start_lengthVariant:
7372 bool hasMoreLengthVariants = false;
7373 // compatible behaviour to the old implementation. Replace
7375 int old_offset = offset;
7376 for (; offset < text.length(); offset++) {
7377 QChar chr = text.at(offset);
7378 if (chr == QLatin1Char('\r') || (singleline && chr == QLatin1Char('\n'))) {
7379 text[offset] = QLatin1Char(' ');
7380 } else if (chr == QLatin1Char('\n')) {
7381 text[offset] = QChar::LineSeparator;
7382 } else if (chr == QLatin1Char('&')) {
7384 } else if (chr == QLatin1Char('\t')) {
7386 text[offset] = QLatin1Char(' ');
7387 } else if (!tabarraylen && !tabstops) {
7388 tabstops = qRound(fm.width(QLatin1Char('x'))*8);
7390 } else if (chr == QChar(ushort(0x9c))) {
7391 // string with multiple length variants
7392 hasMoreLengthVariants = true;
7397 int length = offset - old_offset;
7398 if ((hidemnmemonic || showmnemonic) && maxUnderlines > 0) {
7399 underlinePositions.resize(maxUnderlines + 1);
7401 QChar *cout = text.data() + old_offset;
7405 if (*cin == QLatin1Char('&')) {
7411 if (*cin != QLatin1Char('&') && !hidemnmemonic)
7412 underlinePositions[numUnderlines++] = cout - text.data() - old_offset;
7421 // no need to do extra work for underlines if we don't paint
7422 if (tf & Qt::TextDontPrint)
7425 underlinePositions[numUnderlines] = -1;
7429 QString finalText = text.mid(old_offset, length);
7430 QStackTextEngine engine(finalText, fnt);
7432 engine.option = *option;
7435 if (engine.option.tabStop() < 0 && tabstops > 0)
7436 engine.option.setTabStop(tabstops);
7438 if (engine.option.tabs().isEmpty() && ta) {
7440 for (int i = 0; i < tabarraylen; i++)
7441 tabs.append(qreal(ta[i]));
7442 engine.option.setTabArray(tabs);
7445 engine.option.setTextDirection(layout_direction);
7446 if (tf & Qt::AlignJustify)
7447 engine.option.setAlignment(Qt::AlignJustify);
7449 engine.option.setAlignment(Qt::AlignLeft); // do not do alignment twice
7451 if (!option && (tf & Qt::TextWrapAnywhere))
7452 engine.option.setWrapMode(QTextOption::WrapAnywhere);
7454 if (tf & Qt::TextJustificationForced)
7455 engine.forceJustification = true;
7456 QTextLayout textLayout(&engine);
7457 textLayout.setCacheEnabled(true);
7458 textLayout.engine()->underlinePositions = underlinePositions.data();
7460 if (finalText.isEmpty()) {
7461 height = fm.height();
7463 tf |= Qt::TextDontPrint;
7465 qreal lineWidth = 0x01000000;
7466 if (wordwrap || (tf & Qt::TextJustificationForced))
7467 lineWidth = qMax<qreal>(0, r.width());
7469 tf |= Qt::TextIncludeTrailingSpaces;
7470 textLayout.engine()->ignoreBidi = bool(tf & Qt::TextDontPrint);
7471 textLayout.beginLayout();
7473 qreal leading = fm.leading();
7477 QTextLine l = textLayout.createLine();
7481 l.setLineWidth(lineWidth);
7483 l.setPosition(QPointF(0., height));
7484 height += l.height();
7485 width = qMax(width, l.naturalTextWidth());
7486 if (!dontclip && !brect && height >= r.height())
7489 textLayout.endLayout();
7494 if (tf & Qt::AlignBottom) {
7495 yoff = r.height() - height;
7496 } else if (tf & Qt::AlignVCenter) {
7497 yoff = (r.height() - height)/2;
7499 QTransform::TransformationType type = painter->transform().type();
7500 if (type <= QTransform::TxScale) {
7501 // do the rounding manually to work around inconsistencies
7502 // in the paint engines when drawing on floating point offsets
7503 const qreal scale = painter->transform().m22();
7505 yoff = -qRound(-yoff * scale) / scale;
7509 if (tf & Qt::AlignRight) {
7510 xoff = r.width() - width;
7511 } else if (tf & Qt::AlignHCenter) {
7512 xoff = (r.width() - width)/2;
7514 QTransform::TransformationType type = painter->transform().type();
7515 if (type <= QTransform::TxScale) {
7516 // do the rounding manually to work around inconsistencies
7517 // in the paint engines when drawing on floating point offsets
7518 const qreal scale = painter->transform().m11();
7520 xoff = qRound(xoff * scale) / scale;
7524 QRectF bounds = QRectF(r.x() + xoff, r.y() + yoff, width, height);
7526 if (hasMoreLengthVariants && !(tf & Qt::TextLongestVariant) && !r.contains(bounds)) {
7528 goto start_lengthVariant;
7533 if (!(tf & Qt::TextDontPrint)) {
7534 bool restore = false;
7535 if (!dontclip && !r.contains(bounds)) {
7538 painter->setClipRect(r, Qt::IntersectClip);
7541 for (int i = 0; i < textLayout.lineCount(); i++) {
7542 QTextLine line = textLayout.lineAt(i);
7543 QTextEngine *eng = textLayout.engine();
7544 eng->enableDelayDecorations();
7546 qreal advance = line.horizontalAdvance();
7548 if (tf & Qt::AlignRight) {
7549 xoff = r.width() - advance -
7550 eng->leadingSpaceWidth(eng->lines[line.lineNumber()]).toReal();
7552 else if (tf & Qt::AlignHCenter)
7553 xoff = (r.width() - advance) / 2;
7555 line.draw(painter, QPointF(r.x() + xoff, r.y() + yoff));
7556 eng->drawDecorations(painter);
7566 Sets the layout direction used by the painter when drawing text,
7567 to the specified \a direction.
7569 The default is Qt::LayoutDirectionAuto, which will implicitly determine the
7570 direction from the text drawn.
7572 \sa QTextOption::setTextDirection(), layoutDirection(), drawText(), {QPainter#Settings}{Settings}
7574 void QPainter::setLayoutDirection(Qt::LayoutDirection direction)
7578 d->state->layoutDirection = direction;
7582 Returns the layout direction used by the painter when drawing text.
7584 \sa QTextOption::textDirection(), setLayoutDirection(), drawText(), {QPainter#Settings}{Settings}
7586 Qt::LayoutDirection QPainter::layoutDirection() const
7588 Q_D(const QPainter);
7589 return d->state ? d->state->layoutDirection : Qt::LayoutDirectionAuto;
7592 QPainterState::QPainterState(const QPainterState *s)
7593 : brushOrigin(s->brushOrigin), font(s->font), deviceFont(s->deviceFont),
7594 pen(s->pen), brush(s->brush), bgBrush(s->bgBrush),
7595 clipRegion(s->clipRegion), clipPath(s->clipPath),
7596 clipOperation(s->clipOperation),
7597 renderHints(s->renderHints), clipInfo(s->clipInfo),
7598 worldMatrix(s->worldMatrix), matrix(s->matrix), redirectionMatrix(s->redirectionMatrix),
7599 wx(s->wx), wy(s->wy), ww(s->ww), wh(s->wh),
7600 vx(s->vx), vy(s->vy), vw(s->vw), vh(s->vh),
7601 opacity(s->opacity), WxF(s->WxF), VxF(s->VxF),
7602 clipEnabled(s->clipEnabled), bgMode(s->bgMode), painter(s->painter),
7603 layoutDirection(s->layoutDirection),
7604 composition_mode(s->composition_mode),
7605 emulationSpecifier(s->emulationSpecifier), changeFlags(0)
7607 dirtyFlags = s->dirtyFlags;
7610 QPainterState::QPainterState()
7611 : brushOrigin(0, 0), bgBrush(Qt::white), clipOperation(Qt::NoClip),
7613 wx(0), wy(0), ww(0), wh(0), vx(0), vy(0), vw(0), vh(0),
7614 opacity(1), WxF(false), VxF(false), clipEnabled(true),
7615 bgMode(Qt::TransparentMode), painter(0),
7616 layoutDirection(QGuiApplication::layoutDirection()),
7617 composition_mode(QPainter::CompositionMode_SourceOver),
7618 emulationSpecifier(0), changeFlags(0)
7623 QPainterState::~QPainterState()
7627 void QPainterState::init(QPainter *p) {
7628 bgBrush = Qt::white;
7629 bgMode = Qt::TransparentMode;
7633 wx = wy = ww = wh = 0;
7634 vx = vy = vw = vh = 0;
7637 brushOrigin = QPointF(0, 0);
7639 font = deviceFont = QFont();
7640 clipRegion = QRegion();
7641 clipPath = QPainterPath();
7642 clipOperation = Qt::NoClip;
7644 worldMatrix.reset();
7646 layoutDirection = QGuiApplication::layoutDirection();
7647 composition_mode = QPainter::CompositionMode_SourceOver;
7648 emulationSpecifier = 0;
7656 \fn void QPainter::drawImage(const QRectF &target, const QImage &image, const QRectF &source,
7657 Qt::ImageConversionFlags flags)
7659 Draws the rectangular portion \a source of the given \a image
7660 into the \a target rectangle in the paint device.
7662 \note The image is scaled to fit the rectangle, if both the image and rectangle size disagree.
7664 If the image needs to be modified to fit in a lower-resolution
7665 result (e.g. converting from 32-bit to 8-bit), use the \a flags to
7666 specify how you would prefer this to happen.
7671 \snippet code/src_gui_painting_qpainter.cpp 20
7678 \fn void QPainter::drawImage(const QRect &target, const QImage &image, const QRect &source,
7679 Qt::ImageConversionFlags flags)
7682 Draws the rectangular portion \a source of the given \a image
7683 into the \a target rectangle in the paint device.
7685 \note The image is scaled to fit the rectangle, if both the image and rectangle size disagree.
7689 \fn void QPainter::drawImage(const QPointF &point, const QImage &image)
7693 Draws the given \a image at the given \a point.
7697 \fn void QPainter::drawImage(const QPoint &point, const QImage &image)
7701 Draws the given \a image at the given \a point.
7705 \fn void QPainter::drawImage(const QPointF &point, const QImage &image, const QRectF &source,
7706 Qt::ImageConversionFlags flags = 0)
7710 Draws the rectangular portion \a source of the given \a image with
7711 its origin at the given \a point.
7715 \fn void QPainter::drawImage(const QPoint &point, const QImage &image, const QRect &source,
7716 Qt::ImageConversionFlags flags = 0)
7719 Draws the rectangular portion \a source of the given \a image with
7720 its origin at the given \a point.
7724 \fn void QPainter::drawImage(const QRectF &rectangle, const QImage &image)
7728 Draws the given \a image into the given \a rectangle.
7730 \note The image is scaled to fit the rectangle, if both the image and rectangle size disagree.
7734 \fn void QPainter::drawImage(const QRect &rectangle, const QImage &image)
7738 Draws the given \a image into the given \a rectangle.
7740 \note The image is scaled to fit the rectangle, if both the image and rectangle size disagree.
7744 \fn void QPainter::drawImage(int x, int y, const QImage &image,
7745 int sx, int sy, int sw, int sh,
7746 Qt::ImageConversionFlags flags)
7749 Draws an image at (\a{x}, \a{y}) by copying a part of \a image into
7752 (\a{x}, \a{y}) specifies the top-left point in the paint device that is
7753 to be drawn onto. (\a{sx}, \a{sy}) specifies the top-left point in \a
7754 image that is to be drawn. The default is (0, 0).
7756 (\a{sw}, \a{sh}) specifies the size of the image that is to be drawn.
7757 The default, (0, 0) (and negative) means all the way to the
7758 bottom-right of the image.
7762 \class QPaintEngineState
7765 \brief The QPaintEngineState class provides information about the
7766 active paint engine's current state.
7769 QPaintEngineState records which properties that have changed since
7770 the last time the paint engine was updated, as well as their
7773 Which properties that have changed can at any time be retrieved
7774 using the state() function. This function returns an instance of
7775 the QPaintEngine::DirtyFlags type which stores an OR combination
7776 of QPaintEngine::DirtyFlag values. The QPaintEngine::DirtyFlag
7777 enum defines whether a property has changed since the last update
7780 If a property is marked with a dirty flag, its current value can
7781 be retrieved using the corresponding get function:
7786 \header \li Property Flag \li Current Property Value
7787 \row \li QPaintEngine::DirtyBackground \li backgroundBrush()
7788 \row \li QPaintEngine::DirtyBackgroundMode \li backgroundMode()
7789 \row \li QPaintEngine::DirtyBrush \li brush()
7790 \row \li QPaintEngine::DirtyBrushOrigin \li brushOrigin()
7791 \row \li QPaintEngine::DirtyClipRegion \e or QPaintEngine::DirtyClipPath
7793 \row \li QPaintEngine::DirtyClipPath \li clipPath()
7794 \row \li QPaintEngine::DirtyClipRegion \li clipRegion()
7795 \row \li QPaintEngine::DirtyCompositionMode \li compositionMode()
7796 \row \li QPaintEngine::DirtyFont \li font()
7797 \row \li QPaintEngine::DirtyTransform \li transform()
7798 \row \li QPaintEngine::DirtyClipEnabled \li isClipEnabled()
7799 \row \li QPaintEngine::DirtyPen \li pen()
7800 \row \li QPaintEngine::DirtyHints \li renderHints()
7803 The QPaintEngineState class also provide the painter() function
7804 which returns a pointer to the painter that is currently updating
7807 An instance of this class, representing the current state of the
7808 active paint engine, is passed as argument to the
7809 QPaintEngine::updateState() function. The only situation in which
7810 you will have to use this class directly is when implementing your
7818 \fn QPaintEngine::DirtyFlags QPaintEngineState::state() const
7820 Returns a combination of flags identifying the set of properties
7821 that need to be updated when updating the paint engine's state
7822 (i.e. during a call to the QPaintEngine::updateState() function).
7824 \sa QPaintEngine::updateState()
7829 Returns the pen in the current paint engine state.
7831 This variable should only be used when the state() returns a
7832 combination which includes the QPaintEngine::DirtyPen flag.
7834 \sa state(), QPaintEngine::updateState()
7837 QPen QPaintEngineState::pen() const
7839 return static_cast<const QPainterState *>(this)->pen;
7843 Returns the brush in the current paint engine state.
7845 This variable should only be used when the state() returns a
7846 combination which includes the QPaintEngine::DirtyBrush flag.
7848 \sa state(), QPaintEngine::updateState()
7851 QBrush QPaintEngineState::brush() const
7853 return static_cast<const QPainterState *>(this)->brush;
7857 Returns the brush origin in the current paint engine state.
7859 This variable should only be used when the state() returns a
7860 combination which includes the QPaintEngine::DirtyBrushOrigin flag.
7862 \sa state(), QPaintEngine::updateState()
7865 QPointF QPaintEngineState::brushOrigin() const
7867 return static_cast<const QPainterState *>(this)->brushOrigin;
7871 Returns the background brush in the current paint engine state.
7873 This variable should only be used when the state() returns a
7874 combination which includes the QPaintEngine::DirtyBackground flag.
7876 \sa state(), QPaintEngine::updateState()
7879 QBrush QPaintEngineState::backgroundBrush() const
7881 return static_cast<const QPainterState *>(this)->bgBrush;
7885 Returns the background mode in the current paint engine
7888 This variable should only be used when the state() returns a
7889 combination which includes the QPaintEngine::DirtyBackgroundMode flag.
7891 \sa state(), QPaintEngine::updateState()
7894 Qt::BGMode QPaintEngineState::backgroundMode() const
7896 return static_cast<const QPainterState *>(this)->bgMode;
7900 Returns the font in the current paint engine
7903 This variable should only be used when the state() returns a
7904 combination which includes the QPaintEngine::DirtyFont flag.
7906 \sa state(), QPaintEngine::updateState()
7909 QFont QPaintEngineState::font() const
7911 return static_cast<const QPainterState *>(this)->font;
7918 Returns the matrix in the current paint engine
7921 \note It is advisable to use transform() instead of this function to
7922 preserve the properties of perspective transformations.
7924 This variable should only be used when the state() returns a
7925 combination which includes the QPaintEngine::DirtyTransform flag.
7927 \sa state(), QPaintEngine::updateState()
7930 QMatrix QPaintEngineState::matrix() const
7932 const QPainterState *st = static_cast<const QPainterState *>(this);
7934 return st->matrix.toAffine();
7940 Returns the matrix in the current paint engine state.
7942 This variable should only be used when the state() returns a
7943 combination which includes the QPaintEngine::DirtyTransform flag.
7945 \sa state(), QPaintEngine::updateState()
7949 QTransform QPaintEngineState::transform() const
7951 const QPainterState *st = static_cast<const QPainterState *>(this);
7958 Returns the clip operation in the current paint engine
7961 This variable should only be used when the state() returns a
7962 combination which includes either the QPaintEngine::DirtyClipPath
7963 or the QPaintEngine::DirtyClipRegion flag.
7965 \sa state(), QPaintEngine::updateState()
7968 Qt::ClipOperation QPaintEngineState::clipOperation() const
7970 return static_cast<const QPainterState *>(this)->clipOperation;
7976 Returns whether the coordinate of the fill have been specified
7977 as bounded by the current rendering operation and have to be
7978 resolved (about the currently rendered primitive).
7980 bool QPaintEngineState::brushNeedsResolving() const
7982 const QBrush &brush = static_cast<const QPainterState *>(this)->brush;
7983 return needsResolving(brush);
7990 Returns whether the coordinate of the stroke have been specified
7991 as bounded by the current rendering operation and have to be
7992 resolved (about the currently rendered primitive).
7994 bool QPaintEngineState::penNeedsResolving() const
7996 const QPen &pen = static_cast<const QPainterState *>(this)->pen;
7997 return needsResolving(pen.brush());
8001 Returns the clip region in the current paint engine state.
8003 This variable should only be used when the state() returns a
8004 combination which includes the QPaintEngine::DirtyClipRegion flag.
8006 \sa state(), QPaintEngine::updateState()
8009 QRegion QPaintEngineState::clipRegion() const
8011 return static_cast<const QPainterState *>(this)->clipRegion;
8015 Returns the clip path in the current paint engine state.
8017 This variable should only be used when the state() returns a
8018 combination which includes the QPaintEngine::DirtyClipPath flag.
8020 \sa state(), QPaintEngine::updateState()
8023 QPainterPath QPaintEngineState::clipPath() const
8025 return static_cast<const QPainterState *>(this)->clipPath;
8029 Returns whether clipping is enabled or not in the current paint
8032 This variable should only be used when the state() returns a
8033 combination which includes the QPaintEngine::DirtyClipEnabled
8036 \sa state(), QPaintEngine::updateState()
8039 bool QPaintEngineState::isClipEnabled() const
8041 return static_cast<const QPainterState *>(this)->clipEnabled;
8045 Returns the render hints in the current paint engine state.
8047 This variable should only be used when the state() returns a
8048 combination which includes the QPaintEngine::DirtyHints
8051 \sa state(), QPaintEngine::updateState()
8054 QPainter::RenderHints QPaintEngineState::renderHints() const
8056 return static_cast<const QPainterState *>(this)->renderHints;
8060 Returns the composition mode in the current paint engine state.
8062 This variable should only be used when the state() returns a
8063 combination which includes the QPaintEngine::DirtyCompositionMode
8066 \sa state(), QPaintEngine::updateState()
8069 QPainter::CompositionMode QPaintEngineState::compositionMode() const
8071 return static_cast<const QPainterState *>(this)->composition_mode;
8076 Returns a pointer to the painter currently updating the paint
8080 QPainter *QPaintEngineState::painter() const
8082 return static_cast<const QPainterState *>(this)->painter;
8089 Returns the opacity in the current paint engine state.
8092 qreal QPaintEngineState::opacity() const
8094 return static_cast<const QPainterState *>(this)->opacity;
8100 Sets the world transformation matrix.
8101 If \a combine is true, the specified \a transform is combined with
8102 the current matrix; otherwise it replaces the current matrix.
8104 \sa transform(), setWorldTransform()
8107 void QPainter::setTransform(const QTransform &transform, bool combine )
8109 setWorldTransform(transform, combine);
8113 Returns the world transformation matrix.
8115 \sa worldTransform()
8118 const QTransform & QPainter::transform() const
8120 return worldTransform();
8125 Returns the matrix that transforms from logical coordinates to
8126 device coordinates of the platform dependent paint device.
8128 This function is \e only needed when using platform painting
8129 commands on the platform dependent handle (Qt::HANDLE), and the
8130 platform does not do transformations nativly.
8132 The QPaintEngine::PaintEngineFeature enum can be queried to
8133 determine whether the platform performs the transformations or
8136 \sa worldTransform(), QPaintEngine::hasFeature(),
8139 const QTransform & QPainter::deviceTransform() const
8141 Q_D(const QPainter);
8143 qWarning("QPainter::deviceTransform: Painter not active");
8144 return d->fakeState()->transform;
8146 return d->state->matrix;
8151 Resets any transformations that were made using translate(),
8152 scale(), shear(), rotate(), setWorldTransform(), setViewport()
8155 \sa {Coordinate Transformations}
8158 void QPainter::resetTransform()
8161 #ifdef QT_DEBUG_DRAW
8162 if (qt_show_painter_debug_output)
8163 printf("QPainter::resetMatrix()\n");
8166 qWarning("QPainter::resetMatrix: Painter not active");
8170 d->state->wx = d->state->wy = d->state->vx = d->state->vy = 0; // default view origins
8171 d->state->ww = d->state->vw = d->device->metric(QPaintDevice::PdmWidth);
8172 d->state->wh = d->state->vh = d->device->metric(QPaintDevice::PdmHeight);
8173 d->state->worldMatrix = QTransform();
8174 setMatrixEnabled(false);
8175 setViewTransformEnabled(false);
8177 d->extended->transformChanged();
8179 d->state->dirtyFlags |= QPaintEngine::DirtyTransform;
8183 Sets the world transformation matrix.
8184 If \a combine is true, the specified \a matrix is combined with the current matrix;
8185 otherwise it replaces the current matrix.
8187 \sa transform(), setTransform()
8190 void QPainter::setWorldTransform(const QTransform &matrix, bool combine )
8195 qWarning("QPainter::setWorldTransform: Painter not active");
8200 d->state->worldMatrix = matrix * d->state->worldMatrix; // combines
8202 d->state->worldMatrix = matrix; // set new matrix
8204 d->state->WxF = true;
8209 Returns the world transformation matrix.
8212 const QTransform & QPainter::worldTransform() const
8214 Q_D(const QPainter);
8216 qWarning("QPainter::worldTransform: Painter not active");
8217 return d->fakeState()->transform;
8219 return d->state->worldMatrix;
8223 Returns the transformation matrix combining the current
8224 window/viewport and world transformation.
8226 \sa setWorldTransform(), setWindow(), setViewport()
8229 QTransform QPainter::combinedTransform() const
8231 Q_D(const QPainter);
8233 qWarning("QPainter::combinedTransform: Painter not active");
8234 return QTransform();
8236 return d->state->worldMatrix * d->viewTransform();
8242 This function is used to draw \a pixmap, or a sub-rectangle of \a pixmap,
8243 at multiple positions with different scale, rotation and opacity. \a
8244 fragments is an array of \a fragmentCount elements specifying the
8245 parameters used to draw each pixmap fragment. The \a hints
8246 parameter can be used to pass in drawing hints.
8248 This function is potentially faster than multiple calls to drawPixmap(),
8249 since the backend can optimize state changes.
8251 \sa QPainter::PixmapFragment, QPainter::PixmapFragmentHint
8254 void QPainter::drawPixmapFragments(const PixmapFragment *fragments, int fragmentCount,
8255 const QPixmap &pixmap, PixmapFragmentHints hints)
8259 if (!d->engine || pixmap.isNull())
8263 for (int i = 0; i < fragmentCount; ++i) {
8264 QRectF sourceRect(fragments[i].sourceLeft, fragments[i].sourceTop,
8265 fragments[i].width, fragments[i].height);
8266 if (!(QRectF(pixmap.rect()).contains(sourceRect)))
8267 qWarning("QPainter::drawPixmapFragments - the source rect is not contained by the pixmap's rectangle");
8271 if (d->engine->isExtended()) {
8272 d->extended->drawPixmapFragments(fragments, fragmentCount, pixmap, hints);
8274 qreal oldOpacity = opacity();
8275 QTransform oldTransform = transform();
8277 for (int i = 0; i < fragmentCount; ++i) {
8278 QTransform transform = oldTransform;
8281 if (fragments[i].rotation == 0) {
8282 xOffset = fragments[i].x;
8283 yOffset = fragments[i].y;
8285 transform.translate(fragments[i].x, fragments[i].y);
8286 transform.rotate(fragments[i].rotation);
8288 setOpacity(oldOpacity * fragments[i].opacity);
8289 setTransform(transform);
8291 qreal w = fragments[i].scaleX * fragments[i].width;
8292 qreal h = fragments[i].scaleY * fragments[i].height;
8293 QRectF sourceRect(fragments[i].sourceLeft, fragments[i].sourceTop,
8294 fragments[i].width, fragments[i].height);
8295 drawPixmap(QRectF(-0.5 * w + xOffset, -0.5 * h + yOffset, w, h), pixmap, sourceRect);
8298 setOpacity(oldOpacity);
8299 setTransform(oldTransform);
8305 \class QPainter::PixmapFragment
8307 \brief This class is used in conjunction with the
8308 QPainter::drawPixmapFragments() function to specify how a pixmap, or
8309 sub-rect of a pixmap, is drawn.
8311 The \a sourceLeft, \a sourceTop, \a width and \a height variables are used
8312 as a source rectangle within the pixmap passed into the
8313 QPainter::drawPixmapFragments() function. The variables \a x, \a y, \a
8314 width and \a height are used to calculate the target rectangle that is
8315 drawn. \a x and \a y denotes the center of the target rectangle. The \a
8316 width and \a height in the target rectangle is scaled by the \a scaleX and
8317 \a scaleY values. The resulting target rectangle is then rotated \a
8318 rotation degrees around the \a x, \a y center point.
8320 \sa QPainter::drawPixmapFragments()
8326 This is a convenience function that returns a QPainter::PixmapFragment that is
8327 initialized with the \a pos, \a sourceRect, \a scaleX, \a scaleY, \a
8328 rotation, \a opacity parameters.
8331 QPainter::PixmapFragment QPainter::PixmapFragment::create(const QPointF &pos, const QRectF &sourceRect,
8332 qreal scaleX, qreal scaleY, qreal rotation,
8335 PixmapFragment fragment = {pos.x(), pos.y(), sourceRect.x(), sourceRect.y(), sourceRect.width(),
8336 sourceRect.height(), scaleX, scaleY, rotation, opacity};
8341 \variable QPainter::PixmapFragment::x
8342 \brief the x coordinate of center point in the target rectangle.
8346 \variable QPainter::PixmapFragment::y
8347 \brief the y coordinate of the center point in the target rectangle.
8351 \variable QPainter::PixmapFragment::sourceLeft
8352 \brief the left coordinate of the source rectangle.
8356 \variable QPainter::PixmapFragment::sourceTop
8357 \brief the top coordinate of the source rectangle.
8361 \variable QPainter::PixmapFragment::width
8363 \brief the width of the source rectangle and is used to calculate the width
8364 of the target rectangle.
8368 \variable QPainter::PixmapFragment::height
8370 \brief the height of the source rectangle and is used to calculate the
8371 height of the target rectangle.
8375 \variable QPainter::PixmapFragment::scaleX
8376 \brief the horizontal scale of the target rectangle.
8380 \variable QPainter::PixmapFragment::scaleY
8381 \brief the vertical scale of the target rectangle.
8385 \variable QPainter::PixmapFragment::rotation
8387 \brief the rotation of the target rectangle in degrees. The target
8388 rectangle is rotated after it has been scaled.
8392 \variable QPainter::PixmapFragment::opacity
8394 \brief the opacity of the target rectangle, where 0.0 is fully transparent
8395 and 1.0 is fully opaque.
8401 \enum QPainter::PixmapFragmentHint
8403 \value OpaqueHint Indicates that the pixmap fragments to be drawn are
8404 opaque. Opaque fragments are potentially faster to draw.
8406 \sa QPainter::drawPixmapFragments(), QPainter::PixmapFragment
8409 void qt_draw_helper(QPainterPrivate *p, const QPainterPath &path, QPainterPrivate::DrawOperation operation)
8411 p->draw_helper(path, operation);