1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtGui module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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 <qpa/qplatformtheme.h>
66 #include <private/qfontengine_p.h>
67 #include <private/qpaintengine_p.h>
68 #include <private/qemulationpaintengine_p.h>
69 #include <private/qpainterpath_p.h>
70 #include <private/qtextengine_p.h>
71 #include <private/qpaintengine_raster_p.h>
72 #include <private/qmath_p.h>
73 #include <private/qstatictext_p.h>
74 #include <private/qglyphrun_p.h>
75 #include <private/qhexstring_p.h>
76 #include <private/qguiapplication_p.h>
77 #include <private/qrawfont_p.h>
81 #define QGradient_StretchToDevice 0x10000000
82 #define QPaintEngine_OpaqueBackground 0x40000000
84 // #define QT_DEBUG_DRAW
86 bool qt_show_painter_debug_output = true;
89 extern QPixmap qt_pixmapForBrush(int style, bool invert);
91 void qt_format_text(const QFont &font,
92 const QRectF &_r, int tf, const QTextOption *option, const QString& str, QRectF *brect,
93 int tabstops, int* tabarray, int tabarraylen,
95 static void drawTextItemDecoration(QPainter *painter, const QPointF &pos, const QFontEngine *fe, QTextEngine *textEngine,
96 QTextCharFormat::UnderlineStyle underlineStyle,
97 QTextItem::RenderFlags flags, qreal width,
98 const QTextCharFormat &charFormat);
99 // Helper function to calculate left most position, width and flags for decoration drawing
100 Q_GUI_EXPORT void qt_draw_decoration_for_glyphs(QPainter *painter, const glyph_t *glyphArray,
101 const QFixedPoint *positions, int glyphCount,
102 QFontEngine *fontEngine, const QFont &font,
103 const QTextCharFormat &charFormat);
105 static inline QGradient::CoordinateMode coordinateMode(const QBrush &brush)
107 switch (brush.style()) {
108 case Qt::LinearGradientPattern:
109 case Qt::RadialGradientPattern:
110 case Qt::ConicalGradientPattern:
111 return brush.gradient()->coordinateMode();
115 return QGradient::LogicalMode;
118 /* Returns true if the gradient requires stretch to device...*/
119 static inline bool check_gradient(const QBrush &brush)
121 return coordinateMode(brush) == QGradient::StretchToDeviceMode;
124 extern bool qHasPixmapTexture(const QBrush &);
126 static inline bool is_brush_transparent(const QBrush &brush) {
127 Qt::BrushStyle s = brush.style();
128 bool brushBitmap = qHasPixmapTexture(brush)
129 ? brush.texture().isQBitmap()
130 : (brush.textureImage().depth() == 1);
131 return ((s >= Qt::Dense1Pattern && s <= Qt::DiagCrossPattern)
132 || (s == Qt::TexturePattern && brushBitmap));
135 static inline bool is_pen_transparent(const QPen &pen) {
136 return pen.style() > Qt::SolidLine || is_brush_transparent(pen.brush());
139 /* Discards the emulation flags that are not relevant for line drawing
140 and returns the result
142 static inline uint line_emulation(uint emulation)
144 return emulation & (QPaintEngine::PrimitiveTransform
145 | QPaintEngine::AlphaBlend
146 | QPaintEngine::Antialiasing
147 | QPaintEngine::BrushStroke
148 | QPaintEngine::ConstantOpacity
149 | QGradient_StretchToDevice
150 | QPaintEngine::ObjectBoundingModeGradients
151 | QPaintEngine_OpaqueBackground);
155 static bool qt_painter_thread_test(int devType, const char *what, bool extraCondition = false)
158 case QInternal::Image:
159 case QInternal::Printer:
160 case QInternal::Picture:
161 // can be drawn onto these devices safely from any thread
165 if (!extraCondition && QThread::currentThread() != qApp->thread()) {
166 qWarning("QPainter: It is not safe to use %s outside the GUI thread", what);
175 void QPainterPrivate::checkEmulation()
178 if (extended->flags() & QPaintEngineEx::DoNotEmulate)
181 bool doEmulation = false;
182 if (state->bgMode == Qt::OpaqueMode)
185 const QGradient *bg = state->brush.gradient();
186 if (bg && bg->coordinateMode() > QGradient::LogicalMode)
189 const QGradient *pg = qpen_brush(state->pen).gradient();
190 if (pg && pg->coordinateMode() > QGradient::LogicalMode)
194 if (extended != emulationEngine) {
195 if (!emulationEngine)
196 emulationEngine = new QEmulationPaintEngine(extended);
197 extended = emulationEngine;
198 extended->setState(state);
200 } else if (emulationEngine == extended) {
201 extended = emulationEngine->real_engine;
206 QPainterPrivate::~QPainterPrivate()
208 delete emulationEngine;
209 for (int i=0; i<states.size(); ++i)
217 QTransform QPainterPrivate::viewTransform() const
220 qreal scaleW = qreal(state->vw)/qreal(state->ww);
221 qreal scaleH = qreal(state->vh)/qreal(state->wh);
222 return QTransform(scaleW, 0, 0, scaleH,
223 state->vx - state->wx*scaleW, state->vy - state->wy*scaleH);
231 Returns true if using a shared painter; otherwise false.
233 bool QPainterPrivate::attachPainterPrivate(QPainter *q, QPaintDevice *pdev)
238 QPainter *sp = pdev->sharedPainter();
242 // Save the current state of the shared painter and assign
243 // the current d_ptr to the shared painter's d_ptr.
245 if (!sp->d_ptr->d_ptrs) {
246 // Allocate space for 4 d-pointers (enough for up to 4 sub-sequent
247 // redirections within the same paintEvent(), which should be enough
248 // in 99% of all cases). E.g: A renders B which renders C which renders D.
249 sp->d_ptr->d_ptrs_size = 4;
250 sp->d_ptr->d_ptrs = (QPainterPrivate **)malloc(4 * sizeof(QPainterPrivate *));
251 Q_CHECK_PTR(sp->d_ptr->d_ptrs);
252 } else if (sp->d_ptr->refcount - 1 == sp->d_ptr->d_ptrs_size) {
253 // However, to support corner cases we grow the array dynamically if needed.
254 sp->d_ptr->d_ptrs_size <<= 1;
255 const int newSize = sp->d_ptr->d_ptrs_size * sizeof(QPainterPrivate *);
256 sp->d_ptr->d_ptrs = q_check_ptr((QPainterPrivate **)realloc(sp->d_ptr->d_ptrs, newSize));
258 sp->d_ptr->d_ptrs[++sp->d_ptr->refcount - 2] = q->d_ptr.data();
260 q->d_ptr.reset(sp->d_ptr.data());
262 Q_ASSERT(q->d_ptr->state);
264 // Now initialize the painter with correct widget properties.
267 pdev->redirected(&offset);
268 offset += q->d_ptr->engine->coordinateOffset();
270 // Update system rect.
271 q->d_ptr->state->ww = q->d_ptr->state->vw = pdev->width();
272 q->d_ptr->state->wh = q->d_ptr->state->vh = pdev->height();
275 if (q->d_ptr->state->WxF) {
276 q->d_ptr->state->redirectionMatrix = q->d_ptr->state->matrix;
277 q->d_ptr->state->redirectionMatrix.translate(-offset.x(), -offset.y());
278 q->d_ptr->state->worldMatrix = QTransform();
279 q->d_ptr->state->WxF = false;
281 q->d_ptr->state->redirectionMatrix = QTransform::fromTranslate(-offset.x(), -offset.y());
283 q->d_ptr->updateMatrix();
285 QPaintEnginePrivate *enginePrivate = q->d_ptr->engine->d_func();
286 if (enginePrivate->currentClipDevice == pdev) {
287 enginePrivate->systemStateChanged();
291 // Update system transform and clip.
292 enginePrivate->currentClipDevice = pdev;
293 enginePrivate->setSystemTransform(q->d_ptr->state->matrix);
297 void QPainterPrivate::detachPainterPrivate(QPainter *q)
299 Q_ASSERT(refcount > 1);
302 QPainterPrivate *original = d_ptrs[--refcount - 1];
304 inDestructor = false;
306 original->inDestructor = true;
307 } else if (!original) {
308 original = new QPainterPrivate(q);
311 d_ptrs[refcount - 1] = 0;
314 q->d_ptr.reset(original);
316 if (emulationEngine) {
317 extended = emulationEngine->real_engine;
318 delete emulationEngine;
324 void QPainterPrivate::draw_helper(const QPainterPath &originalPath, DrawOperation op)
327 if (qt_show_painter_debug_output) {
328 printf("QPainter::drawHelper\n");
332 if (originalPath.isEmpty())
335 QPaintEngine::PaintEngineFeatures gradientStretch =
336 QPaintEngine::PaintEngineFeatures(QGradient_StretchToDevice
337 | QPaintEngine::ObjectBoundingModeGradients);
339 const bool mustEmulateObjectBoundingModeGradients = extended
340 || ((state->emulationSpecifier & QPaintEngine::ObjectBoundingModeGradients)
341 && !engine->hasFeature(QPaintEngine::PatternTransform));
343 if (!(state->emulationSpecifier & ~gradientStretch)
344 && !mustEmulateObjectBoundingModeGradients) {
345 drawStretchedGradient(originalPath, op);
347 } else if (state->emulationSpecifier & QPaintEngine_OpaqueBackground) {
348 drawOpaqueBackground(originalPath, op);
354 qreal strokeOffsetX = 0, strokeOffsetY = 0;
356 QPainterPath path = originalPath * state->matrix;
357 QRectF pathBounds = path.boundingRect();
359 bool doStroke = (op & StrokeDraw) && (state->pen.style() != Qt::NoPen);
361 qreal penWidth = state->pen.widthF();
366 // In case of complex xform
367 if (state->matrix.type() > QTransform::TxScale) {
368 QPainterPathStroker stroker;
369 stroker.setWidth(penWidth);
370 stroker.setJoinStyle(state->pen.joinStyle());
371 stroker.setCapStyle(state->pen.capStyle());
372 QPainterPath stroke = stroker.createStroke(originalPath);
373 strokeBounds = (stroke * state->matrix).boundingRect();
375 strokeOffsetX = qAbs(penWidth * state->matrix.m11() / 2.0);
376 strokeOffsetY = qAbs(penWidth * state->matrix.m22() / 2.0);
382 if (!strokeBounds.isEmpty()) {
383 absPathRect = strokeBounds.intersected(QRectF(0, 0, device->width(), device->height())).toAlignedRect();
385 absPathRect = pathBounds.adjusted(-strokeOffsetX, -strokeOffsetY, strokeOffsetX, strokeOffsetY)
386 .intersected(QRectF(0, 0, device->width(), device->height())).toAlignedRect();
389 if (q->hasClipping()) {
390 bool hasPerspectiveTransform = false;
391 for (int i = 0; i < state->clipInfo.size(); ++i) {
392 const QPainterClipInfo &info = state->clipInfo.at(i);
393 if (info.matrix.type() == QTransform::TxProject) {
394 hasPerspectiveTransform = true;
398 // avoid mapping QRegions with perspective transforms
399 if (!hasPerspectiveTransform) {
400 // The trick with txinv and invMatrix is done in order to
401 // avoid transforming the clip to logical coordinates, and
402 // then back to device coordinates. This is a problem with
403 // QRegion/QRect based clips, since they use integer
404 // coordinates and converting to/from logical coordinates will
406 bool old_txinv = txinv;
407 QTransform old_invMatrix = invMatrix;
409 invMatrix = QTransform();
410 QPainterPath clipPath = q->clipPath();
411 QRectF r = clipPath.boundingRect().intersected(absPathRect);
412 absPathRect = r.toAlignedRect();
414 invMatrix = old_invMatrix;
418 // qDebug("\nQPainterPrivate::draw_helper(), x=%d, y=%d, w=%d, h=%d",
419 // devMinX, devMinY, device->width(), device->height());
420 // qDebug() << " - matrix" << state->matrix;
421 // qDebug() << " - originalPath.bounds" << originalPath.boundingRect();
422 // qDebug() << " - path.bounds" << path.boundingRect();
424 if (absPathRect.width() <= 0 || absPathRect.height() <= 0)
427 QImage image(absPathRect.width(), absPathRect.height(), QImage::Format_ARGB32_Premultiplied);
432 p.d_ptr->helper_device = helper_device;
434 p.setOpacity(state->opacity);
435 p.translate(-absPathRect.x(), -absPathRect.y());
436 p.setTransform(state->matrix, true);
437 p.setPen(doStroke ? state->pen : QPen(Qt::NoPen));
438 p.setBrush((op & FillDraw) ? state->brush : QBrush(Qt::NoBrush));
439 p.setBackground(state->bgBrush);
440 p.setBackgroundMode(state->bgMode);
441 p.setBrushOrigin(state->brushOrigin);
443 p.setRenderHint(QPainter::Antialiasing, state->renderHints & QPainter::Antialiasing);
444 p.setRenderHint(QPainter::SmoothPixmapTransform,
445 state->renderHints & QPainter::SmoothPixmapTransform);
447 p.drawPath(originalPath);
450 static bool do_fallback_overlay = qgetenv("QT_PAINT_FALLBACK_OVERLAY").size() > 0;
451 if (do_fallback_overlay) {
452 QImage block(8, 8, QImage::Format_ARGB32_Premultiplied);
454 pt.fillRect(0, 0, 8, 8, QColor(196, 0, 196));
455 pt.drawLine(0, 0, 8, 8);
458 p.setCompositionMode(QPainter::CompositionMode_SourceAtop);
460 p.fillRect(0, 0, image.width(), image.height(), QBrush(block));
467 state->matrix = QTransform();
469 extended->transformChanged();
471 state->dirtyFlags |= QPaintEngine::DirtyTransform;
474 engine->drawImage(absPathRect,
476 QRectF(0, 0, absPathRect.width(), absPathRect.height()),
477 Qt::OrderedDither | Qt::OrderedAlphaDither);
481 void QPainterPrivate::drawOpaqueBackground(const QPainterPath &path, DrawOperation op)
485 q->setBackgroundMode(Qt::TransparentMode);
487 if (op & FillDraw && state->brush.style() != Qt::NoBrush) {
488 q->fillPath(path, state->bgBrush.color());
489 q->fillPath(path, state->brush);
492 if (op & StrokeDraw && state->pen.style() != Qt::NoPen) {
493 q->strokePath(path, QPen(state->bgBrush.color(), state->pen.width()));
494 q->strokePath(path, state->pen);
497 q->setBackgroundMode(Qt::OpaqueMode);
500 static inline QBrush stretchGradientToUserSpace(const QBrush &brush, const QRectF &boundingRect)
502 Q_ASSERT(brush.style() >= Qt::LinearGradientPattern
503 && brush.style() <= Qt::ConicalGradientPattern);
505 QTransform gradientToUser(boundingRect.width(), 0, 0, boundingRect.height(),
506 boundingRect.x(), boundingRect.y());
508 QGradient g = *brush.gradient();
509 g.setCoordinateMode(QGradient::LogicalMode);
512 b.setTransform(gradientToUser * b.transform());
516 void QPainterPrivate::drawStretchedGradient(const QPainterPath &path, DrawOperation op)
520 const qreal sw = helper_device->width();
521 const qreal sh = helper_device->height();
523 bool changedPen = false;
524 bool changedBrush = false;
525 bool needsFill = false;
527 const QPen pen = state->pen;
528 const QBrush brush = state->brush;
530 const QGradient::CoordinateMode penMode = coordinateMode(pen.brush());
531 const QGradient::CoordinateMode brushMode = coordinateMode(brush);
535 // Draw the xformed fill if the brush is a stretch gradient.
536 if ((op & FillDraw) && brush.style() != Qt::NoBrush) {
537 if (brushMode == QGradient::StretchToDeviceMode) {
538 q->setPen(Qt::NoPen);
539 changedPen = pen.style() != Qt::NoPen;
543 const qreal isw = 1.0 / sw;
544 const qreal ish = 1.0 / sh;
545 QTransform inv(isw, 0, 0, ish, 0, 0);
546 engine->drawPath(path * inv);
551 if (brushMode == QGradient::ObjectBoundingMode) {
552 Q_ASSERT(engine->hasFeature(QPaintEngine::PatternTransform));
553 boundingRect = path.boundingRect();
554 q->setBrush(stretchGradientToUserSpace(brush, boundingRect));
560 if ((op & StrokeDraw) && pen.style() != Qt::NoPen) {
561 // Draw the xformed outline if the pen is a stretch gradient.
562 if (penMode == QGradient::StretchToDeviceMode) {
563 q->setPen(Qt::NoPen);
568 engine->drawPath(path);
572 q->setBrush(pen.brush());
576 QPainterPathStroker stroker;
577 stroker.setDashPattern(pen.style());
578 stroker.setWidth(pen.widthF());
579 stroker.setJoinStyle(pen.joinStyle());
580 stroker.setCapStyle(pen.capStyle());
581 stroker.setMiterLimit(pen.miterLimit());
582 QPainterPath stroke = stroker.createStroke(path);
584 const qreal isw = 1.0 / sw;
585 const qreal ish = 1.0 / sh;
586 QTransform inv(isw, 0, 0, ish, 0, 0);
587 engine->drawPath(stroke * inv);
590 if (!needsFill && brush.style() != Qt::NoBrush) {
591 q->setBrush(Qt::NoBrush);
595 if (penMode == QGradient::ObjectBoundingMode) {
596 Q_ASSERT(engine->hasFeature(QPaintEngine::PatternTransform));
598 // avoid computing the bounding rect twice
599 if (!needsFill || brushMode != QGradient::ObjectBoundingMode)
600 boundingRect = path.boundingRect();
603 p.setBrush(stretchGradientToUserSpace(pen.brush(), boundingRect));
606 } else if (changedPen) {
612 engine->drawPath(path);
614 } else if (needsFill) {
615 if (pen.style() != Qt::NoPen) {
616 q->setPen(Qt::NoPen);
621 engine->drawPath(path);
631 void QPainterPrivate::updateMatrix()
633 state->matrix = state->WxF ? state->worldMatrix : QTransform();
635 state->matrix *= viewTransform();
637 txinv = false; // no inverted matrix
638 state->matrix *= state->redirectionMatrix;
640 extended->transformChanged();
642 state->dirtyFlags |= QPaintEngine::DirtyTransform;
644 // printf("VxF=%d, WxF=%d\n", state->VxF, state->WxF);
645 // qDebug() << " --- using matrix" << state->matrix << redirection_offset;
649 void QPainterPrivate::updateInvMatrix()
651 Q_ASSERT(txinv == false);
652 txinv = true; // creating inverted matrix
653 invMatrix = state->matrix.inverted();
656 extern bool qt_isExtendedRadialGradient(const QBrush &brush);
658 void QPainterPrivate::updateEmulationSpecifier(QPainterState *s)
661 bool linearGradient = false;
662 bool radialGradient = false;
663 bool extendedRadialGradient = false;
664 bool conicalGradient = false;
665 bool patternBrush = false;
667 bool complexXform = false;
671 // Pen and brush properties (we have to check both if one changes because the
672 // one that's unchanged can still be in a state which requires emulation)
673 if (s->state() & (QPaintEngine::DirtyPen | QPaintEngine::DirtyBrush | QPaintEngine::DirtyHints)) {
674 // Check Brush stroke emulation
675 if (!s->pen.isSolid() && !engine->hasFeature(QPaintEngine::BrushStroke))
676 s->emulationSpecifier |= QPaintEngine::BrushStroke;
678 s->emulationSpecifier &= ~QPaintEngine::BrushStroke;
682 QBrush penBrush = (qpen_style(s->pen) == Qt::NoPen) ? QBrush(Qt::NoBrush) : qpen_brush(s->pen);
683 Qt::BrushStyle brushStyle = qbrush_style(s->brush);
684 Qt::BrushStyle penBrushStyle = qbrush_style(penBrush);
685 alpha = (penBrushStyle != Qt::NoBrush
686 && (penBrushStyle < Qt::LinearGradientPattern && penBrush.color().alpha() != 255)
687 && !penBrush.isOpaque())
688 || (brushStyle != Qt::NoBrush
689 && (brushStyle < Qt::LinearGradientPattern && s->brush.color().alpha() != 255)
690 && !s->brush.isOpaque());
691 linearGradient = ((penBrushStyle == Qt::LinearGradientPattern) ||
692 (brushStyle == Qt::LinearGradientPattern));
693 radialGradient = ((penBrushStyle == Qt::RadialGradientPattern) ||
694 (brushStyle == Qt::RadialGradientPattern));
695 extendedRadialGradient = radialGradient && (qt_isExtendedRadialGradient(penBrush) || qt_isExtendedRadialGradient(s->brush));
696 conicalGradient = ((penBrushStyle == Qt::ConicalGradientPattern) ||
697 (brushStyle == Qt::ConicalGradientPattern));
698 patternBrush = (((penBrushStyle > Qt::SolidPattern
699 && penBrushStyle < Qt::LinearGradientPattern)
700 || penBrushStyle == Qt::TexturePattern) ||
701 ((brushStyle > Qt::SolidPattern
702 && brushStyle < Qt::LinearGradientPattern)
703 || brushStyle == Qt::TexturePattern));
705 bool penTextureAlpha = false;
706 if (penBrush.style() == Qt::TexturePattern)
707 penTextureAlpha = qHasPixmapTexture(penBrush)
708 ? (penBrush.texture().depth() > 1) && penBrush.texture().hasAlpha()
709 : penBrush.textureImage().hasAlphaChannel();
710 bool brushTextureAlpha = false;
711 if (s->brush.style() == Qt::TexturePattern) {
712 brushTextureAlpha = qHasPixmapTexture(s->brush)
713 ? (s->brush.texture().depth() > 1) && s->brush.texture().hasAlpha()
714 : s->brush.textureImage().hasAlphaChannel();
716 if (((penBrush.style() == Qt::TexturePattern && penTextureAlpha)
717 || (s->brush.style() == Qt::TexturePattern && brushTextureAlpha))
718 && !engine->hasFeature(QPaintEngine::MaskedBrush))
719 s->emulationSpecifier |= QPaintEngine::MaskedBrush;
721 s->emulationSpecifier &= ~QPaintEngine::MaskedBrush;
724 if (s->state() & (QPaintEngine::DirtyHints
725 | QPaintEngine::DirtyOpacity
726 | QPaintEngine::DirtyBackgroundMode)) {
734 qDebug("QPainterPrivate::updateEmulationSpecifier, state=%p\n"
736 " - linearGradient: %d\n"
737 " - radialGradient: %d\n"
738 " - conicalGradient: %d\n"
739 " - patternBrush: %d\n"
748 uint(s->renderHints),
753 if (s->state() & QPaintEngine::DirtyTransform) {
754 xform = !s->matrix.isIdentity();
755 complexXform = !s->matrix.isAffine();
756 } else if (s->matrix.type() >= QTransform::TxTranslate) {
758 complexXform = !s->matrix.isAffine();
761 const bool brushXform = (!s->brush.transform().type() == QTransform::TxNone);
762 const bool penXform = (!s->pen.brush().transform().type() == QTransform::TxNone);
764 const bool patternXform = patternBrush && (xform || brushXform || penXform);
766 // Check alphablending
767 if (alpha && !engine->hasFeature(QPaintEngine::AlphaBlend))
768 s->emulationSpecifier |= QPaintEngine::AlphaBlend;
770 s->emulationSpecifier &= ~QPaintEngine::AlphaBlend;
772 // Linear gradient emulation
773 if (linearGradient && !engine->hasFeature(QPaintEngine::LinearGradientFill))
774 s->emulationSpecifier |= QPaintEngine::LinearGradientFill;
776 s->emulationSpecifier &= ~QPaintEngine::LinearGradientFill;
778 // Radial gradient emulation
779 if (extendedRadialGradient || (radialGradient && !engine->hasFeature(QPaintEngine::RadialGradientFill)))
780 s->emulationSpecifier |= QPaintEngine::RadialGradientFill;
782 s->emulationSpecifier &= ~QPaintEngine::RadialGradientFill;
784 // Conical gradient emulation
785 if (conicalGradient && !engine->hasFeature(QPaintEngine::ConicalGradientFill))
786 s->emulationSpecifier |= QPaintEngine::ConicalGradientFill;
788 s->emulationSpecifier &= ~QPaintEngine::ConicalGradientFill;
791 if (patternBrush && !engine->hasFeature(QPaintEngine::PatternBrush))
792 s->emulationSpecifier |= QPaintEngine::PatternBrush;
794 s->emulationSpecifier &= ~QPaintEngine::PatternBrush;
797 if (patternXform && !engine->hasFeature(QPaintEngine::PatternTransform))
798 s->emulationSpecifier |= QPaintEngine::PatternTransform;
800 s->emulationSpecifier &= ~QPaintEngine::PatternTransform;
803 if (xform && !engine->hasFeature(QPaintEngine::PrimitiveTransform))
804 s->emulationSpecifier |= QPaintEngine::PrimitiveTransform;
806 s->emulationSpecifier &= ~QPaintEngine::PrimitiveTransform;
808 // Perspective XForms
809 if (complexXform && !engine->hasFeature(QPaintEngine::PerspectiveTransform))
810 s->emulationSpecifier |= QPaintEngine::PerspectiveTransform;
812 s->emulationSpecifier &= ~QPaintEngine::PerspectiveTransform;
815 if (state->opacity != 1 && !engine->hasFeature(QPaintEngine::ConstantOpacity))
816 s->emulationSpecifier |= QPaintEngine::ConstantOpacity;
818 s->emulationSpecifier &= ~QPaintEngine::ConstantOpacity;
820 bool gradientStretch = false;
821 bool objectBoundingMode = false;
822 if (linearGradient || conicalGradient || radialGradient) {
823 QGradient::CoordinateMode brushMode = coordinateMode(s->brush);
824 QGradient::CoordinateMode penMode = coordinateMode(s->pen.brush());
826 gradientStretch |= (brushMode == QGradient::StretchToDeviceMode);
827 gradientStretch |= (penMode == QGradient::StretchToDeviceMode);
829 objectBoundingMode |= (brushMode == QGradient::ObjectBoundingMode);
830 objectBoundingMode |= (penMode == QGradient::ObjectBoundingMode);
833 s->emulationSpecifier |= QGradient_StretchToDevice;
835 s->emulationSpecifier &= ~QGradient_StretchToDevice;
837 if (objectBoundingMode && !engine->hasFeature(QPaintEngine::ObjectBoundingModeGradients))
838 s->emulationSpecifier |= QPaintEngine::ObjectBoundingModeGradients;
840 s->emulationSpecifier &= ~QPaintEngine::ObjectBoundingModeGradients;
842 // Opaque backgrounds...
843 if (s->bgMode == Qt::OpaqueMode &&
844 (is_pen_transparent(s->pen) || is_brush_transparent(s->brush)))
845 s->emulationSpecifier |= QPaintEngine_OpaqueBackground;
847 s->emulationSpecifier &= ~QPaintEngine_OpaqueBackground;
850 //won't be correct either way because the device can already have
851 // something rendered to it in which case subsequent emulation
852 // on a fully transparent qimage and then blitting the results
853 // won't produce correct results
855 if (state->composition_mode > QPainter::CompositionMode_Xor &&
856 !engine->hasFeature(QPaintEngine::BlendModes))
857 s->emulationSpecifier |= QPaintEngine::BlendModes;
859 s->emulationSpecifier &= ~QPaintEngine::BlendModes;
863 void QPainterPrivate::updateStateImpl(QPainterState *newState)
865 // ### we might have to call QPainter::begin() here...
866 if (!engine->state) {
867 engine->state = newState;
868 engine->setDirty(QPaintEngine::AllDirty);
871 if (engine->state->painter() != newState->painter)
872 // ### this could break with clip regions vs paths.
873 engine->setDirty(QPaintEngine::AllDirty);
875 // Upon restore, revert all changes since last save
876 else if (engine->state != newState)
877 newState->dirtyFlags |= QPaintEngine::DirtyFlags(static_cast<QPainterState *>(engine->state)->changeFlags);
879 // We need to store all changes made so that restore can deal with them
881 newState->changeFlags |= newState->dirtyFlags;
883 updateEmulationSpecifier(newState);
885 // Unset potential dirty background mode
886 newState->dirtyFlags &= ~(QPaintEngine::DirtyBackgroundMode
887 | QPaintEngine::DirtyBackground);
889 engine->state = newState;
890 engine->updateState(*newState);
891 engine->clearDirty(QPaintEngine::AllDirty);
895 void QPainterPrivate::updateState(QPainterState *newState)
899 engine->state = newState;
901 } else if (newState->state() || engine->state!=newState) {
902 bool setNonCosmeticPen = (newState->renderHints & QPainter::NonCosmeticDefaultPen)
903 && newState->pen.widthF() == 0;
904 if (setNonCosmeticPen) {
905 // Override the default pen's cosmetic state if the
906 // NonCosmeticDefaultPen render hint is used.
907 QPen oldPen = newState->pen;
908 newState->pen.setWidth(1);
909 newState->pen.setCosmetic(false);
910 newState->dirtyFlags |= QPaintEngine::DirtyPen;
912 updateStateImpl(newState);
914 // Restore the state pen back to its default to preserve visible
916 newState->pen = oldPen;
918 updateStateImpl(newState);
926 \brief The QPainter class performs low-level painting on widgets and
934 QPainter provides highly optimized functions to do most of the
935 drawing GUI programs require. It can draw everything from simple
936 lines to complex shapes like pies and chords. It can also draw
937 aligned text and pixmaps. Normally, it draws in a "natural"
938 coordinate system, but it can also do view and world
939 transformation. QPainter can operate on any object that inherits
940 the QPaintDevice class.
942 The common use of QPainter is inside a widget's paint event:
943 Construct and customize (e.g. set the pen or the brush) the
944 painter. Then draw. Remember to destroy the QPainter object after
945 drawing. For example:
947 \snippet code/src_gui_painting_qpainter.cpp 0
949 The core functionality of QPainter is drawing, but the class also
950 provide several functions that allows you to customize QPainter's
951 settings and its rendering quality, and others that enable
952 clipping. In addition you can control how different shapes are
953 merged together by specifying the painter's composition mode.
955 The isActive() function indicates whether the painter is active. A
956 painter is activated by the begin() function and the constructor
957 that takes a QPaintDevice argument. The end() function, and the
958 destructor, deactivates it.
960 Together with the QPaintDevice and QPaintEngine classes, QPainter
961 form the basis for Qt's paint system. QPainter is the class used
962 to perform drawing operations. QPaintDevice represents a device
963 that can be painted on using a QPainter. QPaintEngine provides the
964 interface that the painter uses to draw onto different types of
965 devices. If the painter is active, device() returns the paint
966 device on which the painter paints, and paintEngine() returns the
967 paint engine that the painter is currently operating on. For more
968 information, see the \l {Paint System}.
970 Sometimes it is desirable to make someone else paint on an unusual
971 QPaintDevice. QPainter supports a static function to do this,
974 \warning When the paintdevice is a widget, QPainter can only be
975 used inside a paintEvent() function or in a function called by
982 There are several settings that you can customize to make QPainter
983 draw according to your preferences:
987 \li font() is the font used for drawing text. If the painter
988 isActive(), you can retrieve information about the currently set
989 font, and its metrics, using the fontInfo() and fontMetrics()
990 functions respectively.
992 \li brush() defines the color or pattern that is used for filling
995 \li pen() defines the color or stipple that is used for drawing
998 \li backgroundMode() defines whether there is a background() or
999 not, i.e it is either Qt::OpaqueMode or Qt::TransparentMode.
1001 \li background() only applies when backgroundMode() is \l
1002 Qt::OpaqueMode and pen() is a stipple. In that case, it
1003 describes the color of the background pixels in the stipple.
1005 \li brushOrigin() defines the origin of the tiled brushes, normally
1006 the origin of widget's background.
1008 \li viewport(), window(), worldTransform() make up the painter's coordinate
1009 transformation system. For more information, see the \l
1010 {Coordinate Transformations} section and the \l {Coordinate
1011 System} documentation.
1013 \li hasClipping() tells whether the painter clips at all. (The paint
1014 device clips, too.) If the painter clips, it clips to clipRegion().
1016 \li layoutDirection() defines the layout direction used by the
1017 painter when drawing text.
1019 \li worldMatrixEnabled() tells whether world transformation is enabled.
1021 \li viewTransformEnabled() tells whether view transformation is
1026 Note that some of these settings mirror settings in some paint
1027 devices, e.g. QWidget::font(). The QPainter::begin() function (or
1028 equivalently the QPainter constructor) copies these attributes
1029 from the paint device.
1031 You can at any time save the QPainter's state by calling the
1032 save() function which saves all the available settings on an
1033 internal stack. The restore() function pops them back.
1037 QPainter provides functions to draw most primitives: drawPoint(),
1038 drawPoints(), drawLine(), drawRect(), drawRoundedRect(),
1039 drawEllipse(), drawArc(), drawPie(), drawChord(), drawPolyline(),
1040 drawPolygon(), drawConvexPolygon() and drawCubicBezier(). The two
1041 convenience functions, drawRects() and drawLines(), draw the given
1042 number of rectangles or lines in the given array of \l
1043 {QRect}{QRects} or \l {QLine}{QLines} using the current pen and
1046 The QPainter class also provides the fillRect() function which
1047 fills the given QRect, with the given QBrush, and the eraseRect()
1048 function that erases the area inside the given rectangle.
1050 All of these functions have both integer and floating point
1055 \li \inlineimage qpainter-basicdrawing.png
1057 \b {Basic Drawing Example}
1059 The \l {painting/basicdrawing}{Basic Drawing} example shows how to
1060 display basic graphics primitives in a variety of styles using the
1065 If you need to draw a complex shape, especially if you need to do
1066 so repeatedly, consider creating a QPainterPath and drawing it
1072 \b {Painter Paths example}
1074 The QPainterPath class provides a container for painting
1075 operations, enabling graphical shapes to be constructed and
1078 The \l {painting/painterpaths}{Painter Paths} example shows how
1079 painter paths can be used to build complex shapes for rendering.
1081 \li \inlineimage qpainter-painterpaths.png
1084 QPainter also provides the fillPath() function which fills the
1085 given QPainterPath with the given QBrush, and the strokePath()
1086 function that draws the outline of the given path (i.e. strokes
1089 See also the \l {painting/deform}{Vector Deformation} example which
1090 shows how to use advanced vector techniques to draw text using a
1091 QPainterPath, the \l {painting/gradients}{Gradients} example which shows
1092 the different types of gradients that are available in Qt, and the \l
1093 {painting/pathstroke}{Path Stroking} example which shows Qt's built-in
1094 dash patterns and shows how custom patterns can be used to extend
1095 the range of available patterns.
1099 \li \l {painting/deform}{Vector Deformation}
1100 \li \l {painting/gradients}{Gradients}
1101 \li \l {painting/pathstroke}{Path Stroking}
1103 \li \inlineimage qpainter-vectordeformation.png
1104 \li \inlineimage qpainter-gradients.png
1105 \li \inlineimage qpainter-pathstroking.png
1109 There are functions to draw pixmaps/images, namely drawPixmap(),
1110 drawImage() and drawTiledPixmap(). Both drawPixmap() and drawImage()
1111 produce the same result, except that drawPixmap() is faster
1112 on-screen while drawImage() may be faster on a QPrinter or other
1115 Text drawing is done using drawText(). When you need
1116 fine-grained positioning, boundingRect() tells you where a given
1117 drawText() command will draw.
1119 There is a drawPicture() function that draws the contents of an
1120 entire QPicture. The drawPicture() function is the only function
1121 that disregards all the painter's settings as QPicture has its own
1124 \section1 Rendering Quality
1126 To get the optimal rendering result using QPainter, you should use
1127 the platform independent QImage as paint device; i.e. using QImage
1128 will ensure that the result has an identical pixel representation
1131 The QPainter class also provides a means of controlling the
1132 rendering quality through its RenderHint enum and the support for
1133 floating point precision: All the functions for drawing primitives
1134 has a floating point version. These are often used in combination
1135 with the \l {RenderHint}{QPainter::Antialiasing} render hint.
1139 \li \inlineimage qpainter-concentriccircles.png
1141 \b {Concentric Circles Example}
1143 The \l {painting/concentriccircles}{Concentric Circles} example
1144 shows the improved rendering quality that can be obtained using
1145 floating point precision and anti-aliasing when drawing custom
1148 The application's main window displays several widgets which are
1149 drawn using the various combinations of precision and
1154 The RenderHint enum specifies flags to QPainter that may or may
1155 not be respected by any given engine. \l
1156 {RenderHint}{QPainter::Antialiasing} indicates that the engine
1157 should antialias edges of primitives if possible, \l
1158 {RenderHint}{QPainter::TextAntialiasing} indicates that the engine
1159 should antialias text if possible, and the \l
1160 {RenderHint}{QPainter::SmoothPixmapTransform} indicates that the
1161 engine should use a smooth pixmap transformation algorithm.
1162 \l {RenderHint}{HighQualityAntialiasing} is an OpenGL-specific rendering hint
1163 indicating that the engine should use fragment programs and offscreen
1164 rendering for antialiasing.
1166 The renderHints() function returns a flag that specifies the
1167 rendering hints that are set for this painter. Use the
1168 setRenderHint() function to set or clear the currently set
1171 \section1 Coordinate Transformations
1173 Normally, the QPainter operates on the device's own coordinate
1174 system (usually pixels), but QPainter has good support for
1175 coordinate transformations.
1179 \li nop \li rotate() \li scale() \li translate()
1181 \li \inlineimage qpainter-clock.png
1182 \li \inlineimage qpainter-rotation.png
1183 \li \inlineimage qpainter-scale.png
1184 \li \inlineimage qpainter-translation.png
1187 The most commonly used transformations are scaling, rotation,
1188 translation and shearing. Use the scale() function to scale the
1189 coordinate system by a given offset, the rotate() function to
1190 rotate it clockwise and translate() to translate it (i.e. adding a
1191 given offset to the points). You can also twist the coordinate
1192 system around the origin using the shear() function. See the \l
1193 {painting/affine}{Affine Transformations} example for a visualization of
1194 a sheared coordinate system.
1196 See also the \l {painting/transformations}{Transformations}
1197 example which shows how transformations influence the way that
1198 QPainter renders graphics primitives. In particular it shows how
1199 the order of transformations affects the result.
1204 \b {Affine Transformations Example}
1206 The \l {painting/affine}{Affine Transformations} example shows Qt's
1207 ability to perform affine transformations on painting
1208 operations. The demo also allows the user to experiment with the
1209 transformation operations and see the results immediately.
1211 \li \inlineimage qpainter-affinetransformations.png
1214 All the tranformation operations operate on the transformation
1215 worldTransform(). A matrix transforms a point in the plane to another
1216 point. For more information about the transformation matrix, see
1217 the \l {Coordinate System} and QTransform documentation.
1219 The setWorldTransform() function can replace or add to the currently
1220 set worldTransform(). The resetTransform() function resets any
1221 transformations that were made using translate(), scale(),
1222 shear(), rotate(), setWorldTransform(), setViewport() and setWindow()
1223 functions. The deviceTransform() returns the matrix that transforms
1224 from logical coordinates to device coordinates of the platform
1225 dependent paint device. The latter function is only needed when
1226 using platform painting commands on the platform dependent handle,
1227 and the platform does not do transformations nativly.
1229 When drawing with QPainter, we specify points using logical
1230 coordinates which then are converted into the physical coordinates
1231 of the paint device. The mapping of the logical coordinates to the
1232 physical coordinates are handled by QPainter's combinedTransform(), a
1233 combination of viewport() and window() and worldTransform(). The
1234 viewport() represents the physical coordinates specifying an
1235 arbitrary rectangle, the window() describes the same rectangle in
1236 logical coordinates, and the worldTransform() is identical with the
1237 transformation matrix.
1239 See also \l {Coordinate System}
1243 QPainter can clip any drawing operation to a rectangle, a region,
1244 or a vector path. The current clip is available using the
1245 functions clipRegion() and clipPath(). Whether paths or regions are
1246 preferred (faster) depends on the underlying paintEngine(). For
1247 example, the QImage paint engine prefers paths while the X11 paint
1248 engine prefers regions. Setting a clip is done in the painters
1249 logical coordinates.
1251 After QPainter's clipping, the paint device may also clip. For
1252 example, most widgets clip away the pixels used by child widgets,
1253 and most printers clip away an area near the edges of the paper.
1254 This additional clipping is not reflected by the return value of
1255 clipRegion() or hasClipping().
1257 \section1 Composition Modes
1258 \target Composition Modes
1260 QPainter provides the CompositionMode enum which defines the
1261 Porter-Duff rules for digital image compositing; it describes a
1262 model for combining the pixels in one image, the source, with the
1263 pixels in another image, the destination.
1265 The two most common forms of composition are \l
1266 {QPainter::CompositionMode}{Source} and \l
1267 {QPainter::CompositionMode}{SourceOver}. \l
1268 {QPainter::CompositionMode}{Source} is used to draw opaque objects
1269 onto a paint device. In this mode, each pixel in the source
1270 replaces the corresponding pixel in the destination. In \l
1271 {QPainter::CompositionMode}{SourceOver} composition mode, the
1272 source object is transparent and is drawn on top of the
1275 Note that composition transformation operates pixelwise. For that
1276 reason, there is a difference between using the graphic primitive
1277 itself and its bounding rectangle: The bounding rect contains
1278 pixels with alpha == 0 (i.e the pixels surrounding the
1279 primitive). These pixels will overwrite the other image's pixels,
1280 affectively clearing those, while the primitive only overwrites
1285 \li \inlineimage qpainter-compositiondemo.png
1288 \b {Composition Modes Example}
1290 The \l {painting/composition}{Composition Modes} example, available in
1291 Qt's examples directory, allows you to experiment with the various
1292 composition modes and see the results immediately.
1296 \section1 Limitations
1299 If you are using coordinates with Qt's raster-based paint engine, it is
1300 important to note that, while coordinates greater than +/- 2\sup 15 can
1301 be used, any painting performed with coordinates outside this range is not
1302 guaranteed to be shown; the drawing may be clipped. This is due to the
1303 use of \c{short int} in the implementation.
1305 The outlines generated by Qt's stroker are only an approximation when dealing
1306 with curved shapes. It is in most cases impossible to represent the outline of
1307 a bezier curve segment using another bezier curve segment, and so Qt approximates
1308 the curve outlines by using several smaller curves. For performance reasons there
1309 is a limit to how many curves Qt uses for these outlines, and thus when using
1310 large pen widths or scales the outline error increases. To generate outlines with
1311 smaller errors it is possible to use the QPainterPathStroker class, which has the
1312 setCurveThreshold member function which let's the user specify the error tolerance.
1313 Another workaround is to convert the paths to polygons first and then draw the
1316 \section1 Performance
1318 QPainter is a rich framework that allows developers to do a great
1319 variety of graphical operations, such as gradients, composition
1320 modes and vector graphics. And QPainter can do this across a
1321 variety of different hardware and software stacks. Naturally the
1322 underlying combination of hardware and software has some
1323 implications for performance, and ensuring that every single
1324 operation is fast in combination with all the various combinations
1325 of composition modes, brushes, clipping, transformation, etc, is
1326 close to an impossible task because of the number of
1327 permutations. As a compromise we have selected a subset of the
1328 QPainter API and backends, where performance is guaranteed to be as
1329 good as we can sensibly get it for the given combination of
1330 hardware and software.
1332 The backends we focus on as high-performance engines are:
1336 \li Raster - This backend implements all rendering in pure software
1337 and is always used to render into QImages. For optimal performance
1338 only use the format types QImage::Format_ARGB32_Premultiplied,
1339 QImage::Format_RGB32 or QImage::Format_RGB16. Any other format,
1340 including QImage::Format_ARGB32, has significantly worse
1341 performance. This engine is used by default for QWidget and QPixmap.
1343 \li OpenGL 2.0 (ES) - This backend is the primary backend for
1344 hardware accelerated graphics. It can be run on desktop machines
1345 and embedded devices supporting the OpenGL 2.0 or OpenGL/ES 2.0
1346 specification. This includes most graphics chips produced in the
1347 last couple of years. The engine can be enabled by using QPainter
1348 onto a QOpenGLWidget or by passing \c {-graphicssystem opengl} on the
1349 command line when the underlying system supports it.
1351 \li OpenVG - This backend implements the Khronos standard for 2D
1352 and Vector Graphics. It is primarily for embedded devices with
1353 hardware support for OpenVG. The engine can be enabled by
1354 passing \c {-graphicssystem openvg} on the command line when
1355 the underlying system supports it.
1359 These operations are:
1363 \li Simple transformations, meaning translation and scaling, pluss
1364 0, 90, 180, 270 degree rotations.
1366 \li \c drawPixmap() in combination with simple transformations and
1367 opacity with non-smooth transformation mode
1368 (\c QPainter::SmoothPixmapTransform not enabled as a render hint).
1370 \li Rectangle fills with solid color, two-color linear gradients
1371 and simple transforms.
1373 \li Rectangular clipping with simple transformations and intersect
1376 \li Composition Modes \c QPainter::CompositionMode_Source and
1377 QPainter::CompositionMode_SourceOver
1379 \li Rounded rectangle filling using solid color and two-color
1380 linear gradients fills.
1382 \li 3x3 patched pixmaps, via qDrawBorderPixmap.
1386 This list gives an indication of which features to safely use in
1387 an application where performance is critical. For certain setups,
1388 other operations may be fast too, but before making extensive use
1389 of them, it is recommended to benchmark and verify them on the
1390 system where the software will run in the end. There are also
1391 cases where expensive operations are ok to use, for instance when
1392 the result is cached in a QPixmap.
1394 \sa QPaintDevice, QPaintEngine, {QtSvg Module}, {Basic Drawing Example},
1395 {Drawing Utility Functions}
1399 \enum QPainter::RenderHint
1401 Renderhints are used to specify flags to QPainter that may or
1402 may not be respected by any given engine.
1404 \value Antialiasing Indicates that the engine should antialias
1405 edges of primitives if possible.
1407 \value TextAntialiasing Indicates that the engine should antialias
1408 text if possible. To forcibly disable antialiasing for text, do not
1409 use this hint. Instead, set QFont::NoAntialias on your font's style
1412 \value SmoothPixmapTransform Indicates that the engine should use
1413 a smooth pixmap transformation algorithm (such as bilinear) rather
1414 than nearest neighbor.
1416 \value HighQualityAntialiasing An OpenGL-specific rendering hint
1417 indicating that the engine should use fragment programs and offscreen
1418 rendering for antialiasing.
1420 \value NonCosmeticDefaultPen The engine should interpret pens with a width
1421 of 0 (which otherwise enables QPen::isCosmetic()) as being a non-cosmetic
1422 pen with a width of 1.
1424 \sa renderHints(), setRenderHint(), {QPainter#Rendering
1425 Quality}{Rendering Quality}, {Concentric Circles Example}
1430 Constructs a painter.
1435 QPainter::QPainter()
1436 : d_ptr(new QPainterPrivate(this))
1441 \fn QPainter::QPainter(QPaintDevice *device)
1443 Constructs a painter that begins painting the paint \a device
1446 This constructor is convenient for short-lived painters, e.g. in a
1447 QWidget::paintEvent() and should be used only once. The
1448 constructor calls begin() for you and the QPainter destructor
1449 automatically calls end().
1451 Here's an example using begin() and end():
1452 \snippet code/src_gui_painting_qpainter.cpp 1
1454 The same example using this constructor:
1455 \snippet code/src_gui_painting_qpainter.cpp 2
1457 Since the constructor cannot provide feedback when the initialization
1458 of the painter failed you should rather use begin() and end() to paint
1459 on external devices, e.g. printers.
1464 QPainter::QPainter(QPaintDevice *pd)
1468 if (!QPainterPrivate::attachPainterPrivate(this, pd)) {
1469 d_ptr.reset(new QPainterPrivate(this));
1476 Destroys the painter.
1478 QPainter::~QPainter()
1480 d_ptr->inDestructor = true;
1484 else if (d_ptr->refcount > 1)
1485 d_ptr->detachPainterPrivate(this);
1487 // don't throw anything in the destructor.
1490 // Make sure we haven't messed things up.
1491 Q_ASSERT(d_ptr->inDestructor);
1492 d_ptr->inDestructor = false;
1493 Q_ASSERT(d_ptr->refcount == 1);
1495 free(d_ptr->d_ptrs);
1500 Returns the paint device on which this painter is currently
1501 painting, or 0 if the painter is not active.
1506 QPaintDevice *QPainter::device() const
1508 Q_D(const QPainter);
1509 if (isActive() && d->engine->d_func()->currentClipDevice)
1510 return d->engine->d_func()->currentClipDevice;
1511 return d->original_device;
1515 Returns true if begin() has been called and end() has not yet been
1516 called; otherwise returns false.
1518 \sa begin(), QPaintDevice::paintingActive()
1521 bool QPainter::isActive() const
1523 Q_D(const QPainter);
1528 Initializes the painters pen, background and font to the same as
1529 the given \a device.
1533 \sa begin(), {QPainter#Settings}{Settings}
1535 void QPainter::initFrom(const QPaintDevice *device)
1537 Q_ASSERT_X(device, "QPainter::initFrom(const QPaintDevice *device)", "QPaintDevice cannot be 0");
1540 qWarning("QPainter::initFrom: Painter not active, aborted");
1544 device->initPainter(this);
1547 d->extended->penChanged();
1548 } else if (d->engine) {
1549 d->engine->setDirty(QPaintEngine::DirtyPen);
1550 d->engine->setDirty(QPaintEngine::DirtyBrush);
1551 d->engine->setDirty(QPaintEngine::DirtyFont);
1557 Saves the current painter state (pushes the state onto a stack). A
1558 save() must be followed by a corresponding restore(); the end()
1559 function unwinds the stack.
1564 void QPainter::save()
1566 #ifdef QT_DEBUG_DRAW
1567 if (qt_show_painter_debug_output)
1568 printf("QPainter::save()\n");
1572 qWarning("QPainter::save: Painter not active");
1577 d->state = d->extended->createState(d->states.back());
1578 d->extended->setState(d->state);
1580 d->updateState(d->state);
1581 d->state = new QPainterState(d->states.back());
1582 d->engine->state = d->state;
1584 d->states.push_back(d->state);
1588 Restores the current painter state (pops a saved state off the
1594 void QPainter::restore()
1596 #ifdef QT_DEBUG_DRAW
1597 if (qt_show_painter_debug_output)
1598 printf("QPainter::restore()\n");
1601 if (d->states.size()<=1) {
1602 qWarning("QPainter::restore: Unbalanced save/restore");
1604 } else if (!d->engine) {
1605 qWarning("QPainter::restore: Painter not active");
1609 QPainterState *tmp = d->state;
1610 d->states.pop_back();
1611 d->state = d->states.back();
1615 d->checkEmulation();
1616 d->extended->setState(d->state);
1621 // trigger clip update if the clip path/region has changed since
1623 if (!d->state->clipInfo.isEmpty()
1624 && (tmp->changeFlags & (QPaintEngine::DirtyClipRegion | QPaintEngine::DirtyClipPath))) {
1625 // reuse the tmp state to avoid any extra allocs...
1626 tmp->dirtyFlags = QPaintEngine::DirtyClipPath;
1627 tmp->clipOperation = Qt::NoClip;
1628 tmp->clipPath = QPainterPath();
1629 d->engine->updateState(*tmp);
1630 // replay the list of clip states,
1631 for (int i=0; i<d->state->clipInfo.size(); ++i) {
1632 const QPainterClipInfo &info = d->state->clipInfo.at(i);
1633 tmp->matrix = info.matrix;
1634 tmp->matrix *= d->state->redirectionMatrix;
1635 tmp->clipOperation = info.operation;
1636 if (info.clipType == QPainterClipInfo::RectClip) {
1637 tmp->dirtyFlags = QPaintEngine::DirtyClipRegion | QPaintEngine::DirtyTransform;
1638 tmp->clipRegion = info.rect;
1639 } else if (info.clipType == QPainterClipInfo::RegionClip) {
1640 tmp->dirtyFlags = QPaintEngine::DirtyClipRegion | QPaintEngine::DirtyTransform;
1641 tmp->clipRegion = info.region;
1642 } else { // clipType == QPainterClipInfo::PathClip
1643 tmp->dirtyFlags = QPaintEngine::DirtyClipPath | QPaintEngine::DirtyTransform;
1644 tmp->clipPath = info.path;
1646 d->engine->updateState(*tmp);
1650 //Since we've updated the clip region anyway, pretend that the clip path hasn't changed:
1651 d->state->dirtyFlags &= ~(QPaintEngine::DirtyClipPath | QPaintEngine::DirtyClipRegion);
1652 tmp->changeFlags &= ~(QPaintEngine::DirtyClipPath | QPaintEngine::DirtyClipRegion);
1653 tmp->changeFlags |= QPaintEngine::DirtyTransform;
1656 d->updateState(d->state);
1663 \fn bool QPainter::begin(QPaintDevice *device)
1665 Begins painting the paint \a device and returns true if
1666 successful; otherwise returns false.
1668 Notice that all painter settings (setPen(), setBrush() etc.) are reset
1669 to default values when begin() is called.
1671 The errors that can occur are serious problems, such as these:
1673 \snippet code/src_gui_painting_qpainter.cpp 3
1675 Note that most of the time, you can use one of the constructors
1676 instead of begin(), and that end() is automatically done at
1679 \warning A paint device can only be painted by one painter at a
1682 \warning Painting on a QImage with the format
1683 QImage::Format_Indexed8 is not supported.
1685 \sa end(), QPainter()
1688 static inline void qt_cleanup_painter_state(QPainterPrivate *d)
1697 bool QPainter::begin(QPaintDevice *pd)
1701 if (pd->painters > 0) {
1702 qWarning("QPainter::begin: A paint device can only be painted by one painter at a time.");
1706 if (d_ptr->engine) {
1707 qWarning("QPainter::begin: Painter already active");
1711 if (QPainterPrivate::attachPainterPrivate(this, pd))
1716 d->helper_device = pd;
1717 d->original_device = pd;
1719 QPoint redirectionOffset;
1720 QPaintDevice *rpd = pd->redirected(&redirectionOffset);
1724 #ifdef QT_DEBUG_DRAW
1725 if (qt_show_painter_debug_output)
1726 printf("QPainter::begin(), device=%p, type=%d\n", pd, pd->devType());
1729 if (pd->devType() == QInternal::Pixmap)
1730 static_cast<QPixmap *>(pd)->detach();
1731 else if (pd->devType() == QInternal::Image)
1732 static_cast<QImage *>(pd)->detach();
1734 d->engine = pd->paintEngine();
1737 qWarning("QPainter::begin: Paint device returned engine == 0, type: %d", pd->devType());
1743 d->extended = d->engine->isExtended() ? static_cast<QPaintEngineEx *>(d->engine) : 0;
1744 if (d->emulationEngine)
1745 d->emulationEngine->real_engine = d->extended;
1747 // Setup new state...
1748 Q_ASSERT(!d->state);
1749 d->state = d->extended ? d->extended->createState(0) : new QPainterState;
1750 d->state->painter = this;
1751 d->states.push_back(d->state);
1753 d->state->redirectionMatrix.translate(-redirectionOffset.x(), -redirectionOffset.y());
1754 d->state->brushOrigin = QPointF();
1756 // Slip a painter state into the engine before we do any other operations
1758 d->extended->setState(d->state);
1760 d->engine->state = d->state;
1762 switch (pd->devType()) {
1763 case QInternal::Pixmap:
1765 QPixmap *pm = static_cast<QPixmap *>(pd);
1768 qWarning("QPainter::begin: Cannot paint on a null pixmap");
1769 qt_cleanup_painter_state(d);
1773 if (pm->depth() == 1) {
1774 d->state->pen = QPen(Qt::color1);
1775 d->state->brush = QBrush(Qt::color0);
1779 case QInternal::Image:
1781 QImage *img = static_cast<QImage *>(pd);
1783 if (img->isNull()) {
1784 qWarning("QPainter::begin: Cannot paint on a null image");
1785 qt_cleanup_painter_state(d);
1787 } else if (img->format() == QImage::Format_Indexed8) {
1788 // Painting on indexed8 images is not supported.
1789 qWarning("QPainter::begin: Cannot paint on an image with the QImage::Format_Indexed8 format");
1790 qt_cleanup_painter_state(d);
1793 if (img->depth() == 1) {
1794 d->state->pen = QPen(Qt::color1);
1795 d->state->brush = QBrush(Qt::color0);
1802 if (d->state->ww == 0) // For compat with 3.x painter defaults
1803 d->state->ww = d->state->wh = d->state->vw = d->state->vh = 1024;
1805 d->engine->setPaintDevice(pd);
1807 bool begun = d->engine->begin(pd);
1809 qWarning("QPainter::begin(): Returned false");
1810 if (d->engine->isActive()) {
1813 qt_cleanup_painter_state(d);
1817 d->engine->setActive(begun);
1820 // Copy painter properties from original paint device,
1821 // required for QPixmap::grabWidget()
1822 if (d->original_device->devType() == QInternal::Widget) {
1823 initFrom(d->original_device);
1825 d->state->layoutDirection = Qt::LayoutDirectionAuto;
1826 // make sure we have a font compatible with the paintdevice
1827 d->state->deviceFont = d->state->font = QFont(d->state->deviceFont, device());
1830 QRect systemRect = d->engine->systemRect();
1831 if (!systemRect.isEmpty()) {
1832 d->state->ww = d->state->vw = systemRect.width();
1833 d->state->wh = d->state->vh = systemRect.height();
1835 d->state->ww = d->state->vw = pd->metric(QPaintDevice::PdmWidth);
1836 d->state->wh = d->state->vh = pd->metric(QPaintDevice::PdmHeight);
1839 const QPoint coordinateOffset = d->engine->coordinateOffset();
1840 d->state->redirectionMatrix.translate(-coordinateOffset.x(), -coordinateOffset.y());
1842 Q_ASSERT(d->engine->isActive());
1844 if (!d->state->redirectionMatrix.isIdentity())
1847 Q_ASSERT(d->engine->isActive());
1848 d->state->renderHints = QPainter::TextAntialiasing;
1849 ++d->device->painters;
1851 d->state->emulationSpecifier = 0;
1857 Ends painting. Any resources used while painting are released. You
1858 don't normally need to call this since it is called by the
1861 Returns true if the painter is no longer active; otherwise returns false.
1863 \sa begin(), isActive()
1866 bool QPainter::end()
1868 #ifdef QT_DEBUG_DRAW
1869 if (qt_show_painter_debug_output)
1870 printf("QPainter::end()\n");
1875 qWarning("QPainter::end: Painter not active, aborted");
1876 qt_cleanup_painter_state(d);
1880 if (d->refcount > 1) {
1881 d->detachPainterPrivate(this);
1887 if (d->engine->isActive()) {
1888 ended = d->engine->end();
1891 --d->device->painters;
1892 if (d->device->painters == 0) {
1893 d->engine->setPaintDevice(0);
1894 d->engine->setActive(false);
1898 if (d->states.size() > 1) {
1899 qWarning("QPainter::end: Painter ended with %d saved states",
1903 if (d->engine->autoDestruct()) {
1907 if (d->emulationEngine) {
1908 delete d->emulationEngine;
1909 d->emulationEngine = 0;
1916 qt_cleanup_painter_state(d);
1923 Returns the paint engine that the painter is currently operating
1924 on if the painter is active; otherwise 0.
1928 QPaintEngine *QPainter::paintEngine() const
1930 Q_D(const QPainter);
1937 Flushes the painting pipeline and prepares for the user issuing commands
1938 directly to the underlying graphics context. Must be followed by a call to
1939 endNativePainting().
1941 Note that only the states the underlying paint engine changes will be reset
1942 to their respective default states. The states we reset may change from
1943 release to release. The following states are currently reset in the OpenGL
1947 \li blending is disabled
1948 \li the depth, stencil and scissor tests are disabled
1949 \li the active texture unit is reset to 0
1950 \li the depth mask, depth function and the clear depth are reset to their
1952 \li the stencil mask, stencil operation and stencil function are reset to
1953 their default values
1954 \li the current color is reset to solid white
1957 If, for example, the OpenGL polygon mode is changed by the user inside a
1958 beginNativePaint()/endNativePainting() block, it will not be reset to the
1959 default state by endNativePainting(). Here is an example that shows
1960 intermixing of painter commands and raw OpenGL commands:
1962 \snippet code/src_gui_painting_qpainter.cpp 21
1964 \sa endNativePainting()
1966 void QPainter::beginNativePainting()
1970 qWarning("QPainter::beginNativePainting: Painter not active");
1975 d->extended->beginNativePainting();
1981 Restores the painter after manually issuing native painting commands. Lets
1982 the painter restore any native state that it relies on before calling any
1983 other painter commands.
1985 \sa beginNativePainting()
1987 void QPainter::endNativePainting()
1989 Q_D(const QPainter);
1991 qWarning("QPainter::beginNativePainting: Painter not active");
1996 d->extended->endNativePainting();
1998 d->engine->syncState();
2002 Returns the font metrics for the painter if the painter is
2003 active. Otherwise, the return value is undefined.
2005 \sa font(), isActive(), {QPainter#Settings}{Settings}
2008 QFontMetrics QPainter::fontMetrics() const
2010 Q_D(const QPainter);
2012 qWarning("QPainter::fontMetrics: Painter not active");
2013 return QFontMetrics(QFont());
2015 return QFontMetrics(d->state->font);
2020 Returns the font info for the painter if the painter is
2021 active. Otherwise, the return value is undefined.
2023 \sa font(), isActive(), {QPainter#Settings}{Settings}
2026 QFontInfo QPainter::fontInfo() const
2028 Q_D(const QPainter);
2030 qWarning("QPainter::fontInfo: Painter not active");
2031 return QFontInfo(QFont());
2033 return QFontInfo(d->state->font);
2039 Returns the opacity of the painter. The default value is
2043 qreal QPainter::opacity() const
2045 Q_D(const QPainter);
2047 qWarning("QPainter::opacity: Painter not active");
2050 return d->state->opacity;
2056 Sets the opacity of the painter to \a opacity. The value should
2057 be in the range 0.0 to 1.0, where 0.0 is fully transparent and
2058 1.0 is fully opaque.
2060 Opacity set on the painter will apply to all drawing operations
2064 void QPainter::setOpacity(qreal opacity)
2069 qWarning("QPainter::setOpacity: Painter not active");
2073 opacity = qMin(qreal(1), qMax(qreal(0), opacity));
2075 if (opacity == d->state->opacity)
2078 d->state->opacity = opacity;
2081 d->extended->opacityChanged();
2083 d->state->dirtyFlags |= QPaintEngine::DirtyOpacity;
2088 Returns the currently set brush origin.
2090 \sa setBrushOrigin(), {QPainter#Settings}{Settings}
2093 QPoint QPainter::brushOrigin() const
2095 Q_D(const QPainter);
2097 qWarning("QPainter::brushOrigin: Painter not active");
2100 return QPointF(d->state->brushOrigin).toPoint();
2104 \fn void QPainter::setBrushOrigin(const QPointF &position)
2106 Sets the brush origin to \a position.
2108 The brush origin specifies the (0, 0) coordinate of the painter's
2111 Note that while the brushOrigin() was necessary to adopt the
2112 parent's background for a widget in Qt 3, this is no longer the
2113 case since the Qt 4 painter doesn't paint the background unless
2114 you explicitly tell it to do so by setting the widget's \l
2115 {QWidget::autoFillBackground}{autoFillBackground} property to
2118 \sa brushOrigin(), {QPainter#Settings}{Settings}
2121 void QPainter::setBrushOrigin(const QPointF &p)
2124 #ifdef QT_DEBUG_DRAW
2125 if (qt_show_painter_debug_output)
2126 printf("QPainter::setBrushOrigin(), (%.2f,%.2f)\n", p.x(), p.y());
2130 qWarning("QPainter::setBrushOrigin: Painter not active");
2134 d->state->brushOrigin = p;
2137 d->extended->brushOriginChanged();
2141 d->state->dirtyFlags |= QPaintEngine::DirtyBrushOrigin;
2145 \fn void QPainter::setBrushOrigin(const QPoint &position)
2148 Sets the brush's origin to the given \a position.
2152 \fn void QPainter::setBrushOrigin(int x, int y)
2156 Sets the brush's origin to point (\a x, \a y).
2160 \enum QPainter::CompositionMode
2162 Defines the modes supported for digital image compositing.
2163 Composition modes are used to specify how the pixels in one image,
2164 the source, are merged with the pixel in another image, the
2167 Please note that the bitwise raster operation modes, denoted with
2168 a RasterOp prefix, are only natively supported in the X11 and
2169 raster paint engines. This means that the only way to utilize
2170 these modes on the Mac is via a QImage. The RasterOp denoted blend
2171 modes are \e not supported for pens and brushes with alpha
2172 components. Also, turning on the QPainter::Antialiasing render
2173 hint will effectively disable the RasterOp modes.
2176 \image qpainter-compositionmode1.png
2177 \image qpainter-compositionmode2.png
2179 The most common type is SourceOver (often referred to as just
2180 alpha blending) where the source pixel is blended on top of the
2181 destination pixel in such a way that the alpha component of the
2182 source defines the translucency of the pixel.
2184 When the paint device is a QImage, the image format must be set to
2185 \l {QImage::Format}{Format_ARGB32_Premultiplied} or
2186 \l {QImage::Format}{Format_ARGB32} for the composition modes to have
2187 any effect. For performance the premultiplied version is the preferred
2190 When a composition mode is set it applies to all painting
2191 operator, pens, brushes, gradients and pixmap/image drawing.
2193 \value CompositionMode_SourceOver This is the default mode. The
2194 alpha of the source is used to blend the pixel on top of the
2197 \value CompositionMode_DestinationOver The alpha of the
2198 destination is used to blend it on top of the source pixels. This
2199 mode is the inverse of CompositionMode_SourceOver.
2201 \value CompositionMode_Clear The pixels in the destination are
2202 cleared (set to fully transparent) independent of the source.
2204 \value CompositionMode_Source The output is the source
2205 pixel. (This means a basic copy operation and is identical to
2206 SourceOver when the source pixel is opaque).
2208 \value CompositionMode_Destination The output is the destination
2209 pixel. This means that the blending has no effect. This mode is
2210 the inverse of CompositionMode_Source.
2212 \value CompositionMode_SourceIn The output is the source, where
2213 the alpha is reduced by that of the destination.
2215 \value CompositionMode_DestinationIn The output is the
2216 destination, where the alpha is reduced by that of the
2217 source. This mode is the inverse of CompositionMode_SourceIn.
2219 \value CompositionMode_SourceOut The output is the source, where
2220 the alpha is reduced by the inverse of destination.
2222 \value CompositionMode_DestinationOut The output is the
2223 destination, where the alpha is reduced by the inverse of the
2224 source. This mode is the inverse of CompositionMode_SourceOut.
2226 \value CompositionMode_SourceAtop The source pixel is blended on
2227 top of the destination, with the alpha of the source pixel reduced
2228 by the alpha of the destination pixel.
2230 \value CompositionMode_DestinationAtop The destination pixel is
2231 blended on top of the source, with the alpha of the destination
2232 pixel is reduced by the alpha of the destination pixel. This mode
2233 is the inverse of CompositionMode_SourceAtop.
2235 \value CompositionMode_Xor The source, whose alpha is reduced with
2236 the inverse of the destination alpha, is merged with the
2237 destination, whose alpha is reduced by the inverse of the source
2238 alpha. CompositionMode_Xor is not the same as the bitwise Xor.
2240 \value CompositionMode_Plus Both the alpha and color of the source
2241 and destination pixels are added together.
2243 \value CompositionMode_Multiply The output is the source color
2244 multiplied by the destination. Multiplying a color with white
2245 leaves the color unchanged, while multiplying a color
2246 with black produces black.
2248 \value CompositionMode_Screen The source and destination colors
2249 are inverted and then multiplied. Screening a color with white
2250 produces white, whereas screening a color with black leaves the
2253 \value CompositionMode_Overlay Multiplies or screens the colors
2254 depending on the destination color. The destination color is mixed
2255 with the source color to reflect the lightness or darkness of the
2258 \value CompositionMode_Darken The darker of the source and
2259 destination colors is selected.
2261 \value CompositionMode_Lighten The lighter of the source and
2262 destination colors is selected.
2264 \value CompositionMode_ColorDodge The destination color is
2265 brightened to reflect the source color. A black source color
2266 leaves the destination color unchanged.
2268 \value CompositionMode_ColorBurn The destination color is darkened
2269 to reflect the source color. A white source color leaves the
2270 destination color unchanged.
2272 \value CompositionMode_HardLight Multiplies or screens the colors
2273 depending on the source color. A light source color will lighten
2274 the destination color, whereas a dark source color will darken the
2277 \value CompositionMode_SoftLight Darkens or lightens the colors
2278 depending on the source color. Similar to
2279 CompositionMode_HardLight.
2281 \value CompositionMode_Difference Subtracts the darker of the
2282 colors from the lighter. Painting with white inverts the
2283 destination color, whereas painting with black leaves the
2284 destination color unchanged.
2286 \value CompositionMode_Exclusion Similar to
2287 CompositionMode_Difference, but with a lower contrast. Painting
2288 with white inverts the destination color, whereas painting with
2289 black leaves the destination color unchanged.
2291 \value RasterOp_SourceOrDestination Does a bitwise OR operation on
2292 the source and destination pixels (src OR dst).
2294 \value RasterOp_SourceAndDestination Does a bitwise AND operation
2295 on the source and destination pixels (src AND dst).
2297 \value RasterOp_SourceXorDestination Does a bitwise XOR operation
2298 on the source and destination pixels (src XOR dst).
2300 \value RasterOp_NotSourceAndNotDestination Does a bitwise NOR
2301 operation on the source and destination pixels ((NOT src) AND (NOT
2304 \value RasterOp_NotSourceOrNotDestination Does a bitwise NAND
2305 operation on the source and destination pixels ((NOT src) OR (NOT
2308 \value RasterOp_NotSourceXorDestination Does a bitwise operation
2309 where the source pixels are inverted and then XOR'ed with the
2310 destination ((NOT src) XOR dst).
2312 \value RasterOp_NotSource Does a bitwise operation where the
2313 source pixels are inverted (NOT src).
2315 \value RasterOp_NotSourceAndDestination Does a bitwise operation
2316 where the source is inverted and then AND'ed with the destination
2317 ((NOT src) AND dst).
2319 \value RasterOp_SourceAndNotDestination Does a bitwise operation
2320 where the source is AND'ed with the inverted destination pixels
2321 (src AND (NOT dst)).
2323 \sa compositionMode(), setCompositionMode(), {QPainter#Composition
2324 Modes}{Composition Modes}, {Image Composition Example}
2328 Sets the composition mode to the given \a mode.
2330 \warning Only a QPainter operating on a QImage fully supports all
2331 composition modes. The RasterOp modes are supported for X11 as
2332 described in compositionMode().
2334 \sa compositionMode()
2336 void QPainter::setCompositionMode(CompositionMode mode)
2340 qWarning("QPainter::setCompositionMode: Painter not active");
2343 if (d->state->composition_mode == mode)
2346 d->state->composition_mode = mode;
2347 d->extended->compositionModeChanged();
2351 if (mode >= QPainter::RasterOp_SourceOrDestination) {
2352 if (!d->engine->hasFeature(QPaintEngine::RasterOpModes)) {
2353 qWarning("QPainter::setCompositionMode: "
2354 "Raster operation modes not supported on device");
2357 } else if (mode >= QPainter::CompositionMode_Plus) {
2358 if (!d->engine->hasFeature(QPaintEngine::BlendModes)) {
2359 qWarning("QPainter::setCompositionMode: "
2360 "Blend modes not supported on device");
2363 } else if (!d->engine->hasFeature(QPaintEngine::PorterDuff)) {
2364 if (mode != CompositionMode_Source && mode != CompositionMode_SourceOver) {
2365 qWarning("QPainter::setCompositionMode: "
2366 "PorterDuff modes not supported on device");
2371 d->state->composition_mode = mode;
2372 d->state->dirtyFlags |= QPaintEngine::DirtyCompositionMode;
2376 Returns the current composition mode.
2378 \sa CompositionMode, setCompositionMode()
2380 QPainter::CompositionMode QPainter::compositionMode() const
2382 Q_D(const QPainter);
2384 qWarning("QPainter::compositionMode: Painter not active");
2385 return QPainter::CompositionMode_SourceOver;
2387 return d->state->composition_mode;
2391 Returns the current background brush.
2393 \sa setBackground(), {QPainter#Settings}{Settings}
2396 const QBrush &QPainter::background() const
2398 Q_D(const QPainter);
2400 qWarning("QPainter::background: Painter not active");
2401 return d->fakeState()->brush;
2403 return d->state->bgBrush;
2408 Returns true if clipping has been set; otherwise returns false.
2410 \sa setClipping(), {QPainter#Clipping}{Clipping}
2413 bool QPainter::hasClipping() const
2415 Q_D(const QPainter);
2417 qWarning("QPainter::hasClipping: Painter not active");
2420 return d->state->clipEnabled && d->state->clipOperation != Qt::NoClip;
2425 Enables clipping if \a enable is true, or disables clipping if \a
2428 \sa hasClipping(), {QPainter#Clipping}{Clipping}
2431 void QPainter::setClipping(bool enable)
2434 #ifdef QT_DEBUG_DRAW
2435 if (qt_show_painter_debug_output)
2436 printf("QPainter::setClipping(), enable=%s, was=%s\n",
2437 enable ? "on" : "off",
2438 hasClipping() ? "on" : "off");
2441 qWarning("QPainter::setClipping: Painter not active, state will be reset by begin");
2445 if (hasClipping() == enable)
2448 // we can't enable clipping if we don't have a clip
2450 && (d->state->clipInfo.isEmpty() || d->state->clipInfo.last().operation == Qt::NoClip))
2452 d->state->clipEnabled = enable;
2455 d->extended->clipEnabledChanged();
2459 d->state->dirtyFlags |= QPaintEngine::DirtyClipEnabled;
2460 d->updateState(d->state);
2465 Returns the currently set clip region. Note that the clip region
2466 is given in logical coordinates.
2468 \warning QPainter does not store the combined clip explicitly as
2469 this is handled by the underlying QPaintEngine, so the path is
2470 recreated on demand and transformed to the current logical
2471 coordinate system. This is potentially an expensive operation.
2473 \sa setClipRegion(), clipPath(), setClipping()
2476 QRegion QPainter::clipRegion() const
2478 Q_D(const QPainter);
2480 qWarning("QPainter::clipRegion: Painter not active");
2485 bool lastWasNothing = true;
2488 const_cast<QPainter *>(this)->d_ptr->updateInvMatrix();
2490 // ### Falcon: Use QPainterPath
2491 for (int i=0; i<d->state->clipInfo.size(); ++i) {
2492 const QPainterClipInfo &info = d->state->clipInfo.at(i);
2493 switch (info.clipType) {
2495 case QPainterClipInfo::RegionClip: {
2496 QTransform matrix = (info.matrix * d->invMatrix);
2497 if (lastWasNothing) {
2498 region = info.region * matrix;
2499 lastWasNothing = false;
2502 if (info.operation == Qt::IntersectClip)
2503 region &= info.region * matrix;
2504 else if (info.operation == Qt::NoClip) {
2505 lastWasNothing = true;
2508 region = info.region * matrix;
2512 case QPainterClipInfo::PathClip: {
2513 QTransform matrix = (info.matrix * d->invMatrix);
2514 if (lastWasNothing) {
2515 region = QRegion((info.path * matrix).toFillPolygon().toPolygon(),
2516 info.path.fillRule());
2517 lastWasNothing = false;
2520 if (info.operation == Qt::IntersectClip) {
2521 region &= QRegion((info.path * matrix).toFillPolygon().toPolygon(),
2522 info.path.fillRule());
2523 } else if (info.operation == Qt::NoClip) {
2524 lastWasNothing = true;
2527 region = QRegion((info.path * matrix).toFillPolygon().toPolygon(),
2528 info.path.fillRule());
2533 case QPainterClipInfo::RectClip: {
2534 QTransform matrix = (info.matrix * d->invMatrix);
2535 if (lastWasNothing) {
2536 region = QRegion(info.rect) * matrix;
2537 lastWasNothing = false;
2540 if (info.operation == Qt::IntersectClip) {
2541 // Use rect intersection if possible.
2542 if (matrix.type() <= QTransform::TxScale)
2543 region &= matrix.mapRect(info.rect);
2545 region &= matrix.map(QRegion(info.rect));
2546 } else if (info.operation == Qt::NoClip) {
2547 lastWasNothing = true;
2550 region = QRegion(info.rect) * matrix;
2555 case QPainterClipInfo::RectFClip: {
2556 QTransform matrix = (info.matrix * d->invMatrix);
2557 if (lastWasNothing) {
2558 region = QRegion(info.rectf.toRect()) * matrix;
2559 lastWasNothing = false;
2562 if (info.operation == Qt::IntersectClip) {
2563 // Use rect intersection if possible.
2564 if (matrix.type() <= QTransform::TxScale)
2565 region &= matrix.mapRect(info.rectf.toRect());
2567 region &= matrix.map(QRegion(info.rectf.toRect()));
2568 } else if (info.operation == Qt::NoClip) {
2569 lastWasNothing = true;
2572 region = QRegion(info.rectf.toRect()) * matrix;
2582 extern QPainterPath qt_regionToPath(const QRegion ®ion);
2585 Returns the currently clip as a path. Note that the clip path is
2586 given in logical coordinates.
2588 \warning QPainter does not store the combined clip explicitly as
2589 this is handled by the underlying QPaintEngine, so the path is
2590 recreated on demand and transformed to the current logical
2591 coordinate system. This is potentially an expensive operation.
2593 \sa setClipPath(), clipRegion(), setClipping()
2595 QPainterPath QPainter::clipPath() const
2597 Q_D(const QPainter);
2599 // ### Since we do not support path intersections and path unions yet,
2600 // we just use clipRegion() here...
2602 qWarning("QPainter::clipPath: Painter not active");
2603 return QPainterPath();
2606 // No clip, return empty
2607 if (d->state->clipInfo.size() == 0) {
2608 return QPainterPath();
2611 // Update inverse matrix, used below.
2613 const_cast<QPainter *>(this)->d_ptr->updateInvMatrix();
2615 // For the simple case avoid conversion.
2616 if (d->state->clipInfo.size() == 1
2617 && d->state->clipInfo.at(0).clipType == QPainterClipInfo::PathClip) {
2618 QTransform matrix = (d->state->clipInfo.at(0).matrix * d->invMatrix);
2619 return d->state->clipInfo.at(0).path * matrix;
2621 } else if (d->state->clipInfo.size() == 1
2622 && d->state->clipInfo.at(0).clipType == QPainterClipInfo::RectClip) {
2623 QTransform matrix = (d->state->clipInfo.at(0).matrix * d->invMatrix);
2625 path.addRect(d->state->clipInfo.at(0).rect);
2626 return path * matrix;
2628 // Fallback to clipRegion() for now, since we don't have isect/unite for paths
2629 return qt_regionToPath(clipRegion());
2635 Returns the bounding rectangle of the current clip if there is a clip;
2636 otherwise returns an empty rectangle. Note that the clip region is
2637 given in logical coordinates.
2639 The bounding rectangle is not guaranteed to be tight.
2641 \sa setClipRect(), setClipPath(), setClipRegion()
2646 QRectF QPainter::clipBoundingRect() const
2648 Q_D(const QPainter);
2651 qWarning("QPainter::clipBoundingRect: Painter not active");
2655 // Accumulate the bounding box in device space. This is not 100%
2656 // precise, but it fits within the guarantee and it is reasonably
2659 for (int i=0; i<d->state->clipInfo.size(); ++i) {
2661 const QPainterClipInfo &info = d->state->clipInfo.at(i);
2663 if (info.clipType == QPainterClipInfo::RectClip)
2665 else if (info.clipType == QPainterClipInfo::RectFClip)
2667 else if (info.clipType == QPainterClipInfo::RegionClip)
2668 r = info.region.boundingRect();
2670 r = info.path.boundingRect();
2672 r = info.matrix.mapRect(r);
2676 else if (info.operation == Qt::IntersectClip)
2681 // Map the rectangle back into logical space using the inverse
2684 const_cast<QPainter *>(this)->d_ptr->updateInvMatrix();
2686 return d->invMatrix.mapRect(bounds);
2690 \fn void QPainter::setClipRect(const QRectF &rectangle, Qt::ClipOperation operation)
2692 Enables clipping, and sets the clip region to the given \a
2693 rectangle using the given clip \a operation. The default operation
2694 is to replace the current clip rectangle.
2696 Note that the clip rectangle is specified in logical (painter)
2699 \sa clipRegion(), setClipping(), {QPainter#Clipping}{Clipping}
2701 void QPainter::setClipRect(const QRectF &rect, Qt::ClipOperation op)
2706 if ((!d->state->clipEnabled && op != Qt::NoClip))
2707 op = Qt::ReplaceClip;
2710 qWarning("QPainter::setClipRect: Painter not active");
2713 qreal right = rect.x() + rect.width();
2714 qreal bottom = rect.y() + rect.height();
2715 qreal pts[] = { rect.x(), rect.y(),
2719 QVectorPath vp(pts, 4, 0, QVectorPath::RectangleHint);
2720 d->state->clipEnabled = true;
2721 d->extended->clip(vp, op);
2722 if (op == Qt::ReplaceClip || op == Qt::NoClip)
2723 d->state->clipInfo.clear();
2724 d->state->clipInfo << QPainterClipInfo(rect, op, d->state->matrix);
2725 d->state->clipOperation = op;
2729 if (qreal(int(rect.top())) == rect.top()
2730 && qreal(int(rect.bottom())) == rect.bottom()
2731 && qreal(int(rect.left())) == rect.left()
2732 && qreal(int(rect.right())) == rect.right())
2734 setClipRect(rect.toRect(), op);
2738 if (rect.isEmpty()) {
2739 setClipRegion(QRegion(), op);
2745 setClipPath(path, op);
2749 \fn void QPainter::setClipRect(const QRect &rectangle, Qt::ClipOperation operation)
2752 Enables clipping, and sets the clip region to the given \a rectangle using the given
2755 void QPainter::setClipRect(const QRect &rect, Qt::ClipOperation op)
2760 qWarning("QPainter::setClipRect: Painter not active");
2764 if ((!d->state->clipEnabled && op != Qt::NoClip))
2765 op = Qt::ReplaceClip;
2768 d->state->clipEnabled = true;
2769 d->extended->clip(rect, op);
2770 if (op == Qt::ReplaceClip || op == Qt::NoClip)
2771 d->state->clipInfo.clear();
2772 d->state->clipInfo << QPainterClipInfo(rect, op, d->state->matrix);
2773 d->state->clipOperation = op;
2777 if (d->state->clipOperation == Qt::NoClip && op == Qt::IntersectClip)
2778 op = Qt::ReplaceClip;
2780 d->state->clipRegion = rect;
2781 d->state->clipOperation = op;
2782 if (op == Qt::NoClip || op == Qt::ReplaceClip)
2783 d->state->clipInfo.clear();
2784 d->state->clipInfo << QPainterClipInfo(rect, op, d->state->matrix);
2785 d->state->clipEnabled = true;
2786 d->state->dirtyFlags |= QPaintEngine::DirtyClipRegion | QPaintEngine::DirtyClipEnabled;
2787 d->updateState(d->state);
2791 \fn void QPainter::setClipRect(int x, int y, int width, int height, Qt::ClipOperation operation)
2793 Enables clipping, and sets the clip region to the rectangle beginning at (\a x, \a y)
2794 with the given \a width and \a height.
2798 \fn void QPainter::setClipRegion(const QRegion ®ion, Qt::ClipOperation operation)
2800 Sets the clip region to the given \a region using the specified clip
2801 \a operation. The default clip operation is to replace the current
2804 Note that the clip region is given in logical coordinates.
2806 \sa clipRegion(), setClipRect(), {QPainter#Clipping}{Clipping}
2808 void QPainter::setClipRegion(const QRegion &r, Qt::ClipOperation op)
2811 #ifdef QT_DEBUG_DRAW
2812 QRect rect = r.boundingRect();
2813 if (qt_show_painter_debug_output)
2814 printf("QPainter::setClipRegion(), size=%d, [%d,%d,%d,%d]\n",
2815 r.rects().size(), rect.x(), rect.y(), rect.width(), rect.height());
2818 qWarning("QPainter::setClipRegion: Painter not active");
2822 if ((!d->state->clipEnabled && op != Qt::NoClip))
2823 op = Qt::ReplaceClip;
2826 d->state->clipEnabled = true;
2827 d->extended->clip(r, op);
2828 if (op == Qt::NoClip || op == Qt::ReplaceClip)
2829 d->state->clipInfo.clear();
2830 d->state->clipInfo << QPainterClipInfo(r, op, d->state->matrix);
2831 d->state->clipOperation = op;
2835 if (d->state->clipOperation == Qt::NoClip && op == Qt::IntersectClip)
2836 op = Qt::ReplaceClip;
2838 d->state->clipRegion = r;
2839 d->state->clipOperation = op;
2840 if (op == Qt::NoClip || op == Qt::ReplaceClip)
2841 d->state->clipInfo.clear();
2842 d->state->clipInfo << QPainterClipInfo(r, op, d->state->matrix);
2843 d->state->clipEnabled = true;
2844 d->state->dirtyFlags |= QPaintEngine::DirtyClipRegion | QPaintEngine::DirtyClipEnabled;
2845 d->updateState(d->state);
2852 Sets the transformation matrix to \a matrix and enables transformations.
2854 \note It is advisable to use setWorldTransform() instead of this function to
2855 preserve the properties of perspective transformations.
2857 If \a combine is true, then \a matrix is combined with the current
2858 transformation matrix; otherwise \a matrix replaces the current
2859 transformation matrix.
2861 If \a matrix is the identity matrix and \a combine is false, this
2862 function calls setWorldMatrixEnabled(false). (The identity matrix is the
2863 matrix where QMatrix::m11() and QMatrix::m22() are 1.0 and the
2866 The following functions can transform the coordinate system without using
2875 They operate on the painter's worldMatrix() and are implemented like this:
2877 \snippet code/src_gui_painting_qpainter.cpp 4
2879 Note that when using setWorldMatrix() function you should always have
2880 \a combine be true when you are drawing into a QPicture. Otherwise
2881 it may not be possible to replay the picture with additional
2882 transformations; using the translate(), scale(), etc. convenience
2885 For more information about the coordinate system, transformations
2886 and window-viewport conversion, see \l {Coordinate System}.
2888 \sa setWorldTransform(), QTransform
2891 void QPainter::setWorldMatrix(const QMatrix &matrix, bool combine)
2893 setWorldTransform(QTransform(matrix), combine);
2900 Returns the world transformation matrix.
2902 It is advisable to use worldTransform() because worldMatrix() does not
2903 preserve the properties of perspective transformations.
2905 \sa {QPainter#Coordinate Transformations}{Coordinate Transformations},
2909 const QMatrix &QPainter::worldMatrix() const
2911 Q_D(const QPainter);
2913 qWarning("QPainter::worldMatrix: Painter not active");
2914 return d->fakeState()->transform.toAffine();
2916 return d->state->worldMatrix.toAffine();
2922 Use setWorldTransform() instead.
2924 \sa setWorldTransform()
2927 void QPainter::setMatrix(const QMatrix &matrix, bool combine)
2929 setWorldTransform(QTransform(matrix), combine);
2935 Use worldTransform() instead.
2937 \sa worldTransform()
2940 const QMatrix &QPainter::matrix() const
2942 return worldMatrix();
2950 Returns the transformation matrix combining the current
2951 window/viewport and world transformation.
2953 It is advisable to use combinedTransform() instead of this
2954 function to preserve the properties of perspective transformations.
2956 \sa setWorldTransform(), setWindow(), setViewport()
2958 QMatrix QPainter::combinedMatrix() const
2960 return combinedTransform().toAffine();
2967 Returns the matrix that transforms from logical coordinates to
2968 device coordinates of the platform dependent paint device.
2970 \note It is advisable to use deviceTransform() instead of this
2971 function to preserve the properties of perspective transformations.
2973 This function is \e only needed when using platform painting
2974 commands on the platform dependent handle (Qt::HANDLE), and the
2975 platform does not do transformations nativly.
2977 The QPaintEngine::PaintEngineFeature enum can be queried to
2978 determine whether the platform performs the transformations or
2981 \sa worldMatrix(), QPaintEngine::hasFeature(),
2983 const QMatrix &QPainter::deviceMatrix() const
2985 Q_D(const QPainter);
2987 qWarning("QPainter::deviceMatrix: Painter not active");
2988 return d->fakeState()->transform.toAffine();
2990 return d->state->matrix.toAffine();
2996 Resets any transformations that were made using translate(), scale(),
2997 shear(), rotate(), setWorldMatrix(), setViewport() and
3000 It is advisable to use resetTransform() instead of this function
3001 to preserve the properties of perspective transformations.
3003 \sa {QPainter#Coordinate Transformations}{Coordinate
3007 void QPainter::resetMatrix()
3016 Enables transformations if \a enable is true, or disables
3017 transformations if \a enable is false. The world transformation
3018 matrix is not changed.
3020 \sa worldMatrixEnabled(), worldTransform(), {QPainter#Coordinate
3021 Transformations}{Coordinate Transformations}
3024 void QPainter::setWorldMatrixEnabled(bool enable)
3027 #ifdef QT_DEBUG_DRAW
3028 if (qt_show_painter_debug_output)
3029 printf("QPainter::setMatrixEnabled(), enable=%d\n", enable);
3033 qWarning("QPainter::setMatrixEnabled: Painter not active");
3036 if (enable == d->state->WxF)
3039 d->state->WxF = enable;
3046 Returns true if world transformation is enabled; otherwise returns
3049 \sa setWorldMatrixEnabled(), worldTransform(), {Coordinate System}
3052 bool QPainter::worldMatrixEnabled() const
3054 Q_D(const QPainter);
3056 qWarning("QPainter::worldMatrixEnabled: Painter not active");
3059 return d->state->WxF;
3065 Use setWorldMatrixEnabled() instead.
3067 \sa setWorldMatrixEnabled()
3070 void QPainter::setMatrixEnabled(bool enable)
3072 setWorldMatrixEnabled(enable);
3078 Use worldMatrixEnabled() instead
3080 \sa worldMatrixEnabled()
3083 bool QPainter::matrixEnabled() const
3085 return worldMatrixEnabled();
3089 Scales the coordinate system by (\a{sx}, \a{sy}).
3091 \sa setWorldTransform(), {QPainter#Coordinate Transformations}{Coordinate Transformations}
3094 void QPainter::scale(qreal sx, qreal sy)
3096 #ifdef QT_DEBUG_DRAW
3097 if (qt_show_painter_debug_output)
3098 printf("QPainter::scale(), sx=%f, sy=%f\n", sx, sy);
3102 qWarning("QPainter::scale: Painter not active");
3106 d->state->worldMatrix.scale(sx,sy);
3107 d->state->WxF = true;
3112 Shears the coordinate system by (\a{sh}, \a{sv}).
3114 \sa setWorldTransform(), {QPainter#Coordinate Transformations}{Coordinate Transformations}
3117 void QPainter::shear(qreal sh, qreal sv)
3119 #ifdef QT_DEBUG_DRAW
3120 if (qt_show_painter_debug_output)
3121 printf("QPainter::shear(), sh=%f, sv=%f\n", sh, sv);
3125 qWarning("QPainter::shear: Painter not active");
3129 d->state->worldMatrix.shear(sh, sv);
3130 d->state->WxF = true;
3135 \fn void QPainter::rotate(qreal angle)
3137 Rotates the coordinate system clockwise. The given \a angle parameter uses degree unit.
3139 \sa setWorldTransform(), {QPainter#Coordinate Transformations}{Coordinate Transformations}
3142 void QPainter::rotate(qreal a)
3144 #ifdef QT_DEBUG_DRAW
3145 if (qt_show_painter_debug_output)
3146 printf("QPainter::rotate(), angle=%f\n", a);
3150 qWarning("QPainter::rotate: Painter not active");
3154 d->state->worldMatrix.rotate(a);
3155 d->state->WxF = true;
3160 Translates the coordinate system by the given \a offset; i.e. the
3161 given \a offset is added to points.
3163 \sa setWorldTransform(), {QPainter#Coordinate Transformations}{Coordinate Transformations}
3165 void QPainter::translate(const QPointF &offset)
3167 qreal dx = offset.x();
3168 qreal dy = offset.y();
3169 #ifdef QT_DEBUG_DRAW
3170 if (qt_show_painter_debug_output)
3171 printf("QPainter::translate(), dx=%f, dy=%f\n", dx, dy);
3175 qWarning("QPainter::translate: Painter not active");
3179 d->state->worldMatrix.translate(dx, dy);
3180 d->state->WxF = true;
3185 \fn void QPainter::translate(const QPoint &offset)
3188 Translates the coordinate system by the given \a offset.
3192 \fn void QPainter::translate(qreal dx, qreal dy)
3195 Translates the coordinate system by the vector (\a dx, \a dy).
3199 \fn void QPainter::setClipPath(const QPainterPath &path, Qt::ClipOperation operation)
3201 Enables clipping, and sets the clip path for the painter to the
3202 given \a path, with the clip \a operation.
3204 Note that the clip path is specified in logical (painter)
3207 \sa clipPath(), clipRegion(), {QPainter#Clipping}{Clipping}
3210 void QPainter::setClipPath(const QPainterPath &path, Qt::ClipOperation op)
3212 #ifdef QT_DEBUG_DRAW
3213 if (qt_show_painter_debug_output) {
3214 QRectF b = path.boundingRect();
3215 printf("QPainter::setClipPath(), size=%d, op=%d, bounds=[%.2f,%.2f,%.2f,%.2f]\n",
3216 path.elementCount(), op, b.x(), b.y(), b.width(), b.height());
3222 qWarning("QPainter::setClipPath: Painter not active");
3226 if ((!d->state->clipEnabled && op != Qt::NoClip))
3227 op = Qt::ReplaceClip;
3230 d->state->clipEnabled = true;
3231 d->extended->clip(path, op);
3232 if (op == Qt::NoClip || op == Qt::ReplaceClip)
3233 d->state->clipInfo.clear();
3234 d->state->clipInfo << QPainterClipInfo(path, op, d->state->matrix);
3235 d->state->clipOperation = op;
3239 if (d->state->clipOperation == Qt::NoClip && op == Qt::IntersectClip)
3240 op = Qt::ReplaceClip;
3242 d->state->clipPath = path;
3243 d->state->clipOperation = op;
3244 if (op == Qt::NoClip || op == Qt::ReplaceClip)
3245 d->state->clipInfo.clear();
3246 d->state->clipInfo << QPainterClipInfo(path, op, d->state->matrix);
3247 d->state->clipEnabled = true;
3248 d->state->dirtyFlags |= QPaintEngine::DirtyClipPath | QPaintEngine::DirtyClipEnabled;
3249 d->updateState(d->state);
3253 Draws the outline (strokes) the path \a path with the pen specified
3256 \sa fillPath(), {QPainter#Drawing}{Drawing}
3258 void QPainter::strokePath(const QPainterPath &path, const QPen &pen)
3263 qWarning("QPainter::strokePath: Painter not active");
3271 const QGradient *g = qpen_brush(pen).gradient();
3272 if (!g || g->coordinateMode() == QGradient::LogicalMode) {
3273 d->extended->stroke(qtVectorPathForPath(path), pen);
3278 QBrush oldBrush = d->state->brush;
3279 QPen oldPen = d->state->pen;
3282 setBrush(Qt::NoBrush);
3292 Fills the given \a path using the given \a brush. The outline is
3295 Alternatively, you can specify a QColor instead of a QBrush; the
3296 QBrush constructor (taking a QColor argument) will automatically
3297 create a solid pattern brush.
3301 void QPainter::fillPath(const QPainterPath &path, const QBrush &brush)
3306 qWarning("QPainter::fillPath: Painter not active");
3314 const QGradient *g = brush.gradient();
3315 if (!g || g->coordinateMode() == QGradient::LogicalMode) {
3316 d->extended->fill(qtVectorPathForPath(path), brush);
3321 QBrush oldBrush = d->state->brush;
3322 QPen oldPen = d->state->pen;
3335 Draws the given painter \a path using the current pen for outline
3336 and the current brush for filling.
3340 \li \inlineimage qpainter-path.png
3342 \snippet code/src_gui_painting_qpainter.cpp 5
3345 \sa {painting/painterpaths}{the Painter Paths
3346 example},{painting/deform}{the Vector Deformation example}
3348 void QPainter::drawPath(const QPainterPath &path)
3350 #ifdef QT_DEBUG_DRAW
3351 QRectF pathBounds = path.boundingRect();
3352 if (qt_show_painter_debug_output)
3353 printf("QPainter::drawPath(), size=%d, [%.2f,%.2f,%.2f,%.2f]\n",
3354 path.elementCount(),
3355 pathBounds.x(), pathBounds.y(), pathBounds.width(), pathBounds.height());
3361 qWarning("QPainter::drawPath: Painter not active");
3366 d->extended->drawPath(path);
3369 d->updateState(d->state);
3371 if (d->engine->hasFeature(QPaintEngine::PainterPaths) && d->state->emulationSpecifier == 0) {
3372 d->engine->drawPath(path);
3374 d->draw_helper(path);
3379 \fn void QPainter::drawLine(const QLineF &line)
3381 Draws a line defined by \a line.
3385 \li \inlineimage qpainter-line.png
3387 \snippet code/src_gui_painting_qpainter.cpp 6
3390 \sa drawLines(), drawPolyline(), {Coordinate System}
3394 \fn void QPainter::drawLine(const QLine &line)
3397 Draws a line defined by \a line.
3401 \fn void QPainter::drawLine(const QPoint &p1, const QPoint &p2)
3404 Draws a line from \a p1 to \a p2.
3408 \fn void QPainter::drawLine(const QPointF &p1, const QPointF &p2)
3411 Draws a line from \a p1 to \a p2.
3415 \fn void QPainter::drawLine(int x1, int y1, int x2, int y2)
3418 Draws a line from (\a x1, \a y1) to (\a x2, \a y2) and sets the
3419 current pen position to (\a x2, \a y2).
3423 \fn void QPainter::drawRect(const QRectF &rectangle)
3425 Draws the current \a rectangle with the current pen and brush.
3427 A filled rectangle has a size of \a{rectangle}.size(). A stroked
3428 rectangle has a size of \a{rectangle}.size() plus the pen width.
3432 \li \inlineimage qpainter-rectangle.png
3434 \snippet code/src_gui_painting_qpainter.cpp 7
3437 \sa drawRects(), drawPolygon(), {Coordinate System}
3441 \fn void QPainter::drawRect(const QRect &rectangle)
3445 Draws the current \a rectangle with the current pen and brush.
3449 \fn void QPainter::drawRect(int x, int y, int width, int height)
3453 Draws a rectangle with upper left corner at (\a{x}, \a{y}) and
3454 with the given \a width and \a height.
3458 \fn void QPainter::drawRects(const QRectF *rectangles, int rectCount)
3460 Draws the first \a rectCount of the given \a rectangles using the
3461 current pen and brush.
3465 void QPainter::drawRects(const QRectF *rects, int rectCount)
3467 #ifdef QT_DEBUG_DRAW
3468 if (qt_show_painter_debug_output)
3469 printf("QPainter::drawRects(), count=%d\n", rectCount);
3474 qWarning("QPainter::drawRects: Painter not active");
3482 d->extended->drawRects(rects, rectCount);
3486 d->updateState(d->state);
3488 if (!d->state->emulationSpecifier) {
3489 d->engine->drawRects(rects, rectCount);
3493 if (d->state->emulationSpecifier == QPaintEngine::PrimitiveTransform
3494 && d->state->matrix.type() == QTransform::TxTranslate) {
3495 for (int i=0; i<rectCount; ++i) {
3496 QRectF r(rects[i].x() + d->state->matrix.dx(),
3497 rects[i].y() + d->state->matrix.dy(),
3500 d->engine->drawRects(&r, 1);
3503 if (d->state->brushNeedsResolving() || d->state->penNeedsResolving()) {
3504 for (int i=0; i<rectCount; ++i) {
3505 QPainterPath rectPath;
3506 rectPath.addRect(rects[i]);
3507 d->draw_helper(rectPath, QPainterPrivate::StrokeAndFillDraw);
3510 QPainterPath rectPath;
3511 for (int i=0; i<rectCount; ++i)
3512 rectPath.addRect(rects[i]);
3513 d->draw_helper(rectPath, QPainterPrivate::StrokeAndFillDraw);
3519 \fn void QPainter::drawRects(const QRect *rectangles, int rectCount)
3522 Draws the first \a rectCount of the given \a rectangles using the
3523 current pen and brush.
3525 void QPainter::drawRects(const QRect *rects, int rectCount)
3527 #ifdef QT_DEBUG_DRAW
3528 if (qt_show_painter_debug_output)
3529 printf("QPainter::drawRects(), count=%d\n", rectCount);
3534 qWarning("QPainter::drawRects: Painter not active");
3542 d->extended->drawRects(rects, rectCount);
3546 d->updateState(d->state);
3548 if (!d->state->emulationSpecifier) {
3549 d->engine->drawRects(rects, rectCount);
3553 if (d->state->emulationSpecifier == QPaintEngine::PrimitiveTransform
3554 && d->state->matrix.type() == QTransform::TxTranslate) {
3555 for (int i=0; i<rectCount; ++i) {
3556 QRectF r(rects[i].x() + d->state->matrix.dx(),
3557 rects[i].y() + d->state->matrix.dy(),
3561 d->engine->drawRects(&r, 1);
3564 if (d->state->brushNeedsResolving() || d->state->penNeedsResolving()) {
3565 for (int i=0; i<rectCount; ++i) {
3566 QPainterPath rectPath;
3567 rectPath.addRect(rects[i]);
3568 d->draw_helper(rectPath, QPainterPrivate::StrokeAndFillDraw);
3571 QPainterPath rectPath;
3572 for (int i=0; i<rectCount; ++i)
3573 rectPath.addRect(rects[i]);
3575 d->draw_helper(rectPath, QPainterPrivate::StrokeAndFillDraw);
3581 \fn void QPainter::drawRects(const QVector<QRectF> &rectangles)
3584 Draws the given \a rectangles using the current pen and brush.
3588 \fn void QPainter::drawRects(const QVector<QRect> &rectangles)
3592 Draws the given \a rectangles using the current pen and brush.
3596 \fn void QPainter::drawPoint(const QPointF &position)
3598 Draws a single point at the given \a position using the current
3601 \sa {Coordinate System}
3605 \fn void QPainter::drawPoint(const QPoint &position)
3608 Draws a single point at the given \a position using the current
3612 /*! \fn void QPainter::drawPoint(int x, int y)
3616 Draws a single point at position (\a x, \a y).
3620 Draws the first \a pointCount points in the array \a points using
3621 the current pen's color.
3623 \sa {Coordinate System}
3625 void QPainter::drawPoints(const QPointF *points, int pointCount)
3627 #ifdef QT_DEBUG_DRAW
3628 if (qt_show_painter_debug_output)
3629 printf("QPainter::drawPoints(), count=%d\n", pointCount);
3634 qWarning("QPainter::drawPoints: Painter not active");
3638 if (pointCount <= 0)
3642 d->extended->drawPoints(points, pointCount);
3646 d->updateState(d->state);
3648 if (!d->state->emulationSpecifier) {
3649 d->engine->drawPoints(points, pointCount);
3653 if (d->state->emulationSpecifier == QPaintEngine::PrimitiveTransform
3654 && d->state->matrix.type() == QTransform::TxTranslate) {
3655 // ### use drawPoints function
3656 for (int i=0; i<pointCount; ++i) {
3657 QPointF pt(points[i].x() + d->state->matrix.dx(),
3658 points[i].y() + d->state->matrix.dy());
3659 d->engine->drawPoints(&pt, 1);
3662 QPen pen = d->state->pen;
3663 bool flat_pen = pen.capStyle() == Qt::FlatCap;
3666 pen.setCapStyle(Qt::SquareCap);
3670 for (int i=0; i<pointCount; ++i) {
3671 path.moveTo(points[i].x(), points[i].y());
3672 path.lineTo(points[i].x() + 0.0001, points[i].y());
3674 d->draw_helper(path, QPainterPrivate::StrokeDraw);
3683 Draws the first \a pointCount points in the array \a points using
3684 the current pen's color.
3687 void QPainter::drawPoints(const QPoint *points, int pointCount)
3689 #ifdef QT_DEBUG_DRAW
3690 if (qt_show_painter_debug_output)
3691 printf("QPainter::drawPoints(), count=%d\n", pointCount);
3696 qWarning("QPainter::drawPoints: Painter not active");
3700 if (pointCount <= 0)
3704 d->extended->drawPoints(points, pointCount);
3708 d->updateState(d->state);
3710 if (!d->state->emulationSpecifier) {
3711 d->engine->drawPoints(points, pointCount);
3715 if (d->state->emulationSpecifier == QPaintEngine::PrimitiveTransform
3716 && d->state->matrix.type() == QTransform::TxTranslate) {
3717 // ### use drawPoints function
3718 for (int i=0; i<pointCount; ++i) {
3719 QPointF pt(points[i].x() + d->state->matrix.dx(),
3720 points[i].y() + d->state->matrix.dy());
3721 d->engine->drawPoints(&pt, 1);
3724 QPen pen = d->state->pen;
3725 bool flat_pen = (pen.capStyle() == Qt::FlatCap);
3728 pen.setCapStyle(Qt::SquareCap);
3732 for (int i=0; i<pointCount; ++i) {
3733 path.moveTo(points[i].x(), points[i].y());
3734 path.lineTo(points[i].x() + 0.0001, points[i].y());
3736 d->draw_helper(path, QPainterPrivate::StrokeDraw);
3743 \fn void QPainter::drawPoints(const QPolygonF &points)
3747 Draws the points in the vector \a points.
3751 \fn void QPainter::drawPoints(const QPolygon &points)
3755 Draws the points in the vector \a points.
3759 Sets the background mode of the painter to the given \a mode
3761 Qt::TransparentMode (the default) draws stippled lines and text
3762 without setting the background pixels. Qt::OpaqueMode fills these
3763 space with the current background color.
3765 Note that in order to draw a bitmap or pixmap transparently, you
3766 must use QPixmap::setMask().
3768 \sa backgroundMode(), setBackground(),
3769 {QPainter#Settings}{Settings}
3772 void QPainter::setBackgroundMode(Qt::BGMode mode)
3774 #ifdef QT_DEBUG_DRAW
3775 if (qt_show_painter_debug_output)
3776 printf("QPainter::setBackgroundMode(), mode=%d\n", mode);
3781 qWarning("QPainter::setBackgroundMode: Painter not active");
3784 if (d->state->bgMode == mode)
3787 d->state->bgMode = mode;
3789 d->checkEmulation();
3791 d->state->dirtyFlags |= QPaintEngine::DirtyBackgroundMode;
3796 Returns the current background mode.
3798 \sa setBackgroundMode(), {QPainter#Settings}{Settings}
3800 Qt::BGMode QPainter::backgroundMode() const
3802 Q_D(const QPainter);
3804 qWarning("QPainter::backgroundMode: Painter not active");
3805 return Qt::TransparentMode;
3807 return d->state->bgMode;
3814 Sets the painter's pen to have style Qt::SolidLine, width 0 and the
3818 void QPainter::setPen(const QColor &color)
3820 #ifdef QT_DEBUG_DRAW
3821 if (qt_show_painter_debug_output)
3822 printf("QPainter::setPen(), color=%04x\n", color.rgb());
3826 qWarning("QPainter::setPen: Painter not active");
3830 if (d->state->pen.style() == Qt::SolidLine
3831 && d->state->pen.widthF() == 0
3832 && d->state->pen.isSolid()
3833 && d->state->pen.color() == color)
3836 QPen pen(color.isValid() ? color : QColor(Qt::black), 0, Qt::SolidLine);
3838 d->state->pen = pen;
3840 d->extended->penChanged();
3842 d->state->dirtyFlags |= QPaintEngine::DirtyPen;
3846 Sets the painter's pen to be the given \a pen.
3848 The \a pen defines how to draw lines and outlines, and it also
3849 defines the text color.
3851 \sa pen(), {QPainter#Settings}{Settings}
3854 void QPainter::setPen(const QPen &pen)
3857 #ifdef QT_DEBUG_DRAW
3858 if (qt_show_painter_debug_output)
3859 printf("QPainter::setPen(), color=%04x, (brushStyle=%d) style=%d, cap=%d, join=%d\n",
3860 pen.color().rgb(), pen.brush().style(), pen.style(), pen.capStyle(), pen.joinStyle());
3864 qWarning("QPainter::setPen: Painter not active");
3868 if (d->state->pen == pen)
3871 d->state->pen = pen;
3874 d->checkEmulation();
3875 d->extended->penChanged();
3879 d->state->dirtyFlags |= QPaintEngine::DirtyPen;
3885 Sets the painter's pen to have the given \a style, width 0 and
3889 void QPainter::setPen(Qt::PenStyle style)
3893 qWarning("QPainter::setPen: Painter not active");
3897 if (d->state->pen.style() == style
3898 && (style == Qt::NoPen || (d->state->pen.widthF() == 0
3899 && d->state->pen.isSolid()
3900 && d->state->pen.color() == QColor(Qt::black))))
3903 // QPen(Qt::NoPen) is to avoid creating QPenData, including its brush (from the color)
3904 // Note that this works well as long as QPen(Qt::NoPen) returns a black, zero-width pen
3905 d->state->pen = (style == Qt::NoPen) ? QPen(Qt::NoPen) : QPen(Qt::black, 0, style);
3908 d->extended->penChanged();
3910 d->state->dirtyFlags |= QPaintEngine::DirtyPen;
3915 Returns the painter's current pen.
3917 \sa setPen(), {QPainter#Settings}{Settings}
3920 const QPen &QPainter::pen() const
3922 Q_D(const QPainter);
3924 qWarning("QPainter::pen: Painter not active");
3925 return d->fakeState()->pen;
3927 return d->state->pen;
3932 Sets the painter's brush to the given \a brush.
3934 The painter's brush defines how shapes are filled.
3936 \sa brush(), {QPainter#Settings}{Settings}
3939 void QPainter::setBrush(const QBrush &brush)
3941 #ifdef QT_DEBUG_DRAW
3942 if (qt_show_painter_debug_output)
3943 printf("QPainter::setBrush(), color=%04x, style=%d\n", brush.color().rgb(), brush.style());
3947 qWarning("QPainter::setBrush: Painter not active");
3951 if (d->state->brush.d == brush.d)
3955 d->state->brush = brush;
3956 d->checkEmulation();
3957 d->extended->brushChanged();
3961 d->state->brush = brush;
3962 d->state->dirtyFlags |= QPaintEngine::DirtyBrush;
3969 Sets the painter's brush to black color and the specified \a
3973 void QPainter::setBrush(Qt::BrushStyle style)
3977 qWarning("QPainter::setBrush: Painter not active");
3980 if (d->state->brush.style() == style &&
3981 (style == Qt::NoBrush
3982 || (style == Qt::SolidPattern && d->state->brush.color() == QColor(0, 0, 0))))
3984 d->state->brush = QBrush(Qt::black, style);
3986 d->extended->brushChanged();
3988 d->state->dirtyFlags |= QPaintEngine::DirtyBrush;
3992 Returns the painter's current brush.
3994 \sa QPainter::setBrush(), {QPainter#Settings}{Settings}
3997 const QBrush &QPainter::brush() const
3999 Q_D(const QPainter);
4001 qWarning("QPainter::brush: Painter not active");
4002 return d->fakeState()->brush;
4004 return d->state->brush;
4008 \fn void QPainter::setBackground(const QBrush &brush)
4010 Sets the background brush of the painter to the given \a brush.
4012 The background brush is the brush that is filled in when drawing
4013 opaque text, stippled lines and bitmaps. The background brush has
4014 no effect in transparent background mode (which is the default).
4016 \sa background(), setBackgroundMode(),
4017 {QPainter#Settings}{Settings}
4020 void QPainter::setBackground(const QBrush &bg)
4022 #ifdef QT_DEBUG_DRAW
4023 if (qt_show_painter_debug_output)
4024 printf("QPainter::setBackground(), color=%04x, style=%d\n", bg.color().rgb(), bg.style());
4029 qWarning("QPainter::setBackground: Painter not active");
4032 d->state->bgBrush = bg;
4034 d->state->dirtyFlags |= QPaintEngine::DirtyBackground;
4038 Sets the painter's font to the given \a font.
4040 This font is used by subsequent drawText() functions. The text
4041 color is the same as the pen color.
4043 If you set a font that isn't available, Qt finds a close match.
4044 font() will return what you set using setFont() and fontInfo() returns the
4045 font actually being used (which may be the same).
4047 \sa font(), drawText(), {QPainter#Settings}{Settings}
4050 void QPainter::setFont(const QFont &font)
4054 #ifdef QT_DEBUG_DRAW
4055 if (qt_show_painter_debug_output)
4056 printf("QPainter::setFont(), family=%s, pointSize=%d\n", font.family().toLatin1().constData(), font.pointSize());
4060 qWarning("QPainter::setFont: Painter not active");
4064 d->state->font = QFont(font.resolve(d->state->deviceFont), device());
4066 d->state->dirtyFlags |= QPaintEngine::DirtyFont;
4070 Returns the currently set font used for drawing text.
4072 \sa setFont(), drawText(), {QPainter#Settings}{Settings}
4074 const QFont &QPainter::font() const
4076 Q_D(const QPainter);
4078 qWarning("QPainter::font: Painter not active");
4079 return d->fakeState()->font;
4081 return d->state->font;
4087 Draws the given rectangle \a rect with rounded corners.
4089 The \a xRadius and \a yRadius arguments specify the radii
4090 of the ellipses defining the corners of the rounded rectangle.
4091 When \a mode is Qt::RelativeSize, \a xRadius and
4092 \a yRadius are specified in percentage of half the rectangle's
4093 width and height respectively, and should be in the range
4096 A filled rectangle has a size of rect.size(). A stroked rectangle
4097 has a size of rect.size() plus the pen width.
4101 \li \inlineimage qpainter-roundrect.png
4103 \snippet code/src_gui_painting_qpainter.cpp 8
4106 \sa drawRect(), QPen
4108 void QPainter::drawRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius, Qt::SizeMode mode)
4110 #ifdef QT_DEBUG_DRAW
4111 if (qt_show_painter_debug_output)
4112 printf("QPainter::drawRoundedRect(), [%.2f,%.2f,%.2f,%.2f]\n", rect.x(), rect.y(), rect.width(), rect.height());
4119 if (xRadius <= 0 || yRadius <= 0) { // draw normal rectangle
4125 d->extended->drawRoundedRect(rect, xRadius, yRadius, mode);
4130 path.addRoundedRect(rect, xRadius, yRadius, mode);
4135 \fn void QPainter::drawRoundedRect(const QRect &rect, qreal xRadius, qreal yRadius,
4136 Qt::SizeMode mode = Qt::AbsoluteSize);
4140 Draws the given rectangle \a rect with rounded corners.
4144 \fn void QPainter::drawRoundedRect(int x, int y, int w, int h, qreal xRadius, qreal yRadius,
4145 Qt::SizeMode mode = Qt::AbsoluteSize);
4149 Draws the given rectangle \a x, \a y, \a w, \a h with rounded corners.
4155 Draws a rectangle \a r with rounded corners.
4157 The \a xRnd and \a yRnd arguments specify how rounded the corners
4158 should be. 0 is angled corners, 99 is maximum roundedness.
4160 A filled rectangle has a size of r.size(). A stroked rectangle
4161 has a size of r.size() plus the pen width.
4163 \sa drawRoundedRect()
4165 void QPainter::drawRoundRect(const QRectF &r, int xRnd, int yRnd)
4167 drawRoundedRect(r, xRnd, yRnd, Qt::RelativeSize);
4172 \fn void QPainter::drawRoundRect(const QRect &r, int xRnd = 25, int yRnd = 25)
4177 Draws the rectangle \a r with rounded corners.
4183 \fn QPainter::drawRoundRect(int x, int y, int w, int h, int xRnd, int yRnd)
4187 Draws the rectangle \a x, \a y, \a w, \a h with rounded corners.
4191 \fn void QPainter::drawEllipse(const QRectF &rectangle)
4193 Draws the ellipse defined by the given \a rectangle.
4195 A filled ellipse has a size of \a{rectangle}.\l
4196 {QRect::size()}{size()}. A stroked ellipse has a size of
4197 \a{rectangle}.\l {QRect::size()}{size()} plus the pen width.
4201 \li \inlineimage qpainter-ellipse.png
4203 \snippet code/src_gui_painting_qpainter.cpp 9
4206 \sa drawPie(), {Coordinate System}
4208 void QPainter::drawEllipse(const QRectF &r)
4210 #ifdef QT_DEBUG_DRAW
4211 if (qt_show_painter_debug_output)
4212 printf("QPainter::drawEllipse(), [%.2f,%.2f,%.2f,%.2f]\n", r.x(), r.y(), r.width(), r.height());
4219 QRectF rect(r.normalized());
4222 d->extended->drawEllipse(rect);
4226 d->updateState(d->state);
4227 if (d->state->emulationSpecifier) {
4228 if (d->state->emulationSpecifier == QPaintEngine::PrimitiveTransform
4229 && d->state->matrix.type() == QTransform::TxTranslate) {
4230 rect.translate(QPointF(d->state->matrix.dx(), d->state->matrix.dy()));
4233 path.addEllipse(rect);
4234 d->draw_helper(path, QPainterPrivate::StrokeAndFillDraw);
4239 d->engine->drawEllipse(rect);
4243 \fn QPainter::drawEllipse(const QRect &rectangle)
4247 Draws the ellipse defined by the given \a rectangle.
4249 void QPainter::drawEllipse(const QRect &r)
4251 #ifdef QT_DEBUG_DRAW
4252 if (qt_show_painter_debug_output)
4253 printf("QPainter::drawEllipse(), [%d,%d,%d,%d]\n", r.x(), r.y(), r.width(), r.height());
4260 QRect rect(r.normalized());
4263 d->extended->drawEllipse(rect);
4267 d->updateState(d->state);
4269 if (d->state->emulationSpecifier) {
4270 if (d->state->emulationSpecifier == QPaintEngine::PrimitiveTransform
4271 && d->state->matrix.type() == QTransform::TxTranslate) {
4272 rect.translate(QPoint(qRound(d->state->matrix.dx()), qRound(d->state->matrix.dy())));
4275 path.addEllipse(rect);
4276 d->draw_helper(path, QPainterPrivate::StrokeAndFillDraw);
4281 d->engine->drawEllipse(rect);
4285 \fn QPainter::drawEllipse(int x, int y, int width, int height)
4289 Draws the ellipse defined by the rectangle beginning at (\a{x},
4290 \a{y}) with the given \a width and \a height.
4296 \fn QPainter::drawEllipse(const QPointF ¢er, qreal rx, qreal ry)
4300 Draws the ellipse positioned at \a{center} with radii \a{rx} and \a{ry}.
4306 \fn QPainter::drawEllipse(const QPoint ¢er, int rx, int ry)
4310 Draws the ellipse positioned at \a{center} with radii \a{rx} and \a{ry}.
4314 \fn void QPainter::drawArc(const QRectF &rectangle, int startAngle, int spanAngle)
4316 Draws the arc defined by the given \a rectangle, \a startAngle and
4319 The \a startAngle and \a spanAngle must be specified in 1/16th of
4320 a degree, i.e. a full circle equals 5760 (16 * 360). Positive
4321 values for the angles mean counter-clockwise while negative values
4322 mean the clockwise direction. Zero degrees is at the 3 o'clock
4327 \li \inlineimage qpainter-arc.png
4329 \snippet code/src_gui_painting_qpainter.cpp 10
4332 \sa drawPie(), drawChord(), {Coordinate System}
4335 void QPainter::drawArc(const QRectF &r, int a, int alen)
4337 #ifdef QT_DEBUG_DRAW
4338 if (qt_show_painter_debug_output)
4339 printf("QPainter::drawArc(), [%.2f,%.2f,%.2f,%.2f], angle=%d, sweep=%d\n",
4340 r.x(), r.y(), r.width(), r.height(), a/16, alen/16);
4347 QRectF rect = r.normalized();
4350 path.arcMoveTo(rect, a/16.0);
4351 path.arcTo(rect, a/16.0, alen/16.0);
4352 strokePath(path, d->state->pen);
4355 /*! \fn void QPainter::drawArc(const QRect &rectangle, int startAngle,
4360 Draws the arc defined by the given \a rectangle, \a startAngle and
4365 \fn void QPainter::drawArc(int x, int y, int width, int height,
4366 int startAngle, int spanAngle)
4370 Draws the arc defined by the rectangle beginning at (\a x, \a y)
4371 with the specified \a width and \a height, and the given \a
4372 startAngle and \a spanAngle.
4376 \fn void QPainter::drawPie(const QRectF &rectangle, int startAngle, int spanAngle)
4378 Draws a pie defined by the given \a rectangle, \a startAngle and
4381 The pie is filled with the current brush().
4383 The startAngle and spanAngle must be specified in 1/16th of a
4384 degree, i.e. a full circle equals 5760 (16 * 360). Positive values
4385 for the angles mean counter-clockwise while negative values mean
4386 the clockwise direction. Zero degrees is at the 3 o'clock
4391 \li \inlineimage qpainter-pie.png
4393 \snippet code/src_gui_painting_qpainter.cpp 11
4396 \sa drawEllipse(), drawChord(), {Coordinate System}
4398 void QPainter::drawPie(const QRectF &r, int a, int alen)
4400 #ifdef QT_DEBUG_DRAW
4401 if (qt_show_painter_debug_output)
4402 printf("QPainter::drawPie(), [%.2f,%.2f,%.2f,%.2f], angle=%d, sweep=%d\n",
4403 r.x(), r.y(), r.width(), r.height(), a/16, alen/16);
4414 if (a < 0) a += (360*16);
4417 QRectF rect = r.normalized();
4420 path.moveTo(rect.center());
4421 path.arcTo(rect.x(), rect.y(), rect.width(), rect.height(), a/16.0, alen/16.0);
4422 path.closeSubpath();
4428 \fn void QPainter::drawPie(const QRect &rectangle, int startAngle, int spanAngle)
4431 Draws a pie defined by the given \a rectangle, \a startAngle and
4436 \fn void QPainter::drawPie(int x, int y, int width, int height, int
4437 startAngle, int spanAngle)
4441 Draws the pie defined by the rectangle beginning at (\a x, \a y) with
4442 the specified \a width and \a height, and the given \a startAngle and
4447 \fn void QPainter::drawChord(const QRectF &rectangle, int startAngle, int spanAngle)
4449 Draws the chord defined by the given \a rectangle, \a startAngle and
4450 \a spanAngle. The chord is filled with the current brush().
4452 The startAngle and spanAngle must be specified in 1/16th of a
4453 degree, i.e. a full circle equals 5760 (16 * 360). Positive values
4454 for the angles mean counter-clockwise while negative values mean
4455 the clockwise direction. Zero degrees is at the 3 o'clock
4460 \li \inlineimage qpainter-chord.png
4462 \snippet code/src_gui_painting_qpainter.cpp 12
4465 \sa drawArc(), drawPie(), {Coordinate System}
4467 void QPainter::drawChord(const QRectF &r, int a, int alen)
4469 #ifdef QT_DEBUG_DRAW
4470 if (qt_show_painter_debug_output)
4471 printf("QPainter::drawChord(), [%.2f,%.2f,%.2f,%.2f], angle=%d, sweep=%d\n",
4472 r.x(), r.y(), r.width(), r.height(), a/16, alen/16);
4479 QRectF rect = r.normalized();
4482 path.arcMoveTo(rect, a/16.0);
4483 path.arcTo(rect, a/16.0, alen/16.0);
4484 path.closeSubpath();
4488 \fn void QPainter::drawChord(const QRect &rectangle, int startAngle, int spanAngle)
4492 Draws the chord defined by the given \a rectangle, \a startAngle and
4497 \fn void QPainter::drawChord(int x, int y, int width, int height, int
4498 startAngle, int spanAngle)
4502 Draws the chord defined by the rectangle beginning at (\a x, \a y)
4503 with the specified \a width and \a height, and the given \a
4504 startAngle and \a spanAngle.
4509 Draws the first \a lineCount lines in the array \a lines
4510 using the current pen.
4512 \sa drawLine(), drawPolyline()
4514 void QPainter::drawLines(const QLineF *lines, int lineCount)
4516 #ifdef QT_DEBUG_DRAW
4517 if (qt_show_painter_debug_output)
4518 printf("QPainter::drawLines(), line count=%d\n", lineCount);
4523 if (!d->engine || lineCount < 1)
4527 d->extended->drawLines(lines, lineCount);
4531 d->updateState(d->state);
4533 uint lineEmulation = line_emulation(d->state->emulationSpecifier);
4535 if (lineEmulation) {
4536 if (lineEmulation == QPaintEngine::PrimitiveTransform
4537 && d->state->matrix.type() == QTransform::TxTranslate) {
4538 for (int i = 0; i < lineCount; ++i) {
4539 QLineF line = lines[i];
4540 line.translate(d->state->matrix.dx(), d->state->matrix.dy());
4541 d->engine->drawLines(&line, 1);
4544 QPainterPath linePath;
4545 for (int i = 0; i < lineCount; ++i) {
4546 linePath.moveTo(lines[i].p1());
4547 linePath.lineTo(lines[i].p2());
4549 d->draw_helper(linePath, QPainterPrivate::StrokeDraw);
4553 d->engine->drawLines(lines, lineCount);
4557 \fn void QPainter::drawLines(const QLine *lines, int lineCount)
4560 Draws the first \a lineCount lines in the array \a lines
4561 using the current pen.
4563 void QPainter::drawLines(const QLine *lines, int lineCount)
4565 #ifdef QT_DEBUG_DRAW
4566 if (qt_show_painter_debug_output)
4567 printf("QPainter::drawLine(), line count=%d\n", lineCount);
4572 if (!d->engine || lineCount < 1)
4576 d->extended->drawLines(lines, lineCount);
4580 d->updateState(d->state);
4582 uint lineEmulation = line_emulation(d->state->emulationSpecifier);
4584 if (lineEmulation) {
4585 if (lineEmulation == QPaintEngine::PrimitiveTransform
4586 && d->state->matrix.type() == QTransform::TxTranslate) {
4587 for (int i = 0; i < lineCount; ++i) {
4588 QLineF line = lines[i];
4589 line.translate(d->state->matrix.dx(), d->state->matrix.dy());
4590 d->engine->drawLines(&line, 1);
4593 QPainterPath linePath;
4594 for (int i = 0; i < lineCount; ++i) {
4595 linePath.moveTo(lines[i].p1());
4596 linePath.lineTo(lines[i].p2());
4598 d->draw_helper(linePath, QPainterPrivate::StrokeDraw);
4602 d->engine->drawLines(lines, lineCount);
4608 Draws the first \a lineCount lines in the array \a pointPairs
4609 using the current pen. The lines are specified as pairs of points
4610 so the number of entries in \a pointPairs must be at least \a
4613 void QPainter::drawLines(const QPointF *pointPairs, int lineCount)
4615 Q_ASSERT(sizeof(QLineF) == 2*sizeof(QPointF));
4617 drawLines((QLineF*)pointPairs, lineCount);
4623 Draws the first \a lineCount lines in the array \a pointPairs
4624 using the current pen.
4626 void QPainter::drawLines(const QPoint *pointPairs, int lineCount)
4628 Q_ASSERT(sizeof(QLine) == 2*sizeof(QPoint));
4630 drawLines((QLine*)pointPairs, lineCount);
4635 \fn void QPainter::drawLines(const QVector<QPointF> &pointPairs)
4638 Draws a line for each pair of points in the vector \a pointPairs
4639 using the current pen. If there is an odd number of points in the
4640 array, the last point will be ignored.
4644 \fn void QPainter::drawLines(const QVector<QPoint> &pointPairs)
4647 Draws a line for each pair of points in the vector \a pointPairs
4648 using the current pen.
4652 \fn void QPainter::drawLines(const QVector<QLineF> &lines)
4655 Draws the set of lines defined by the list \a lines using the
4656 current pen and brush.
4660 \fn void QPainter::drawLines(const QVector<QLine> &lines)
4663 Draws the set of lines defined by the list \a lines using the
4664 current pen and brush.
4668 Draws the polyline defined by the first \a pointCount points in \a
4669 points using the current pen.
4671 Note that unlike the drawPolygon() function the last point is \e
4672 not connected to the first, neither is the polyline filled.
4677 \snippet code/src_gui_painting_qpainter.cpp 13
4680 \sa drawLines(), drawPolygon(), {Coordinate System}
4682 void QPainter::drawPolyline(const QPointF *points, int pointCount)
4684 #ifdef QT_DEBUG_DRAW
4685 if (qt_show_painter_debug_output)
4686 printf("QPainter::drawPolyline(), count=%d\n", pointCount);
4690 if (!d->engine || pointCount < 2)
4694 d->extended->drawPolygon(points, pointCount, QPaintEngine::PolylineMode);
4698 d->updateState(d->state);
4700 uint lineEmulation = line_emulation(d->state->emulationSpecifier);
4702 if (lineEmulation) {
4704 // if (lineEmulation == QPaintEngine::PrimitiveTransform
4705 // && d->state->matrix.type() == QTransform::TxTranslate) {
4707 QPainterPath polylinePath(points[0]);
4708 for (int i=1; i<pointCount; ++i)
4709 polylinePath.lineTo(points[i]);
4710 d->draw_helper(polylinePath, QPainterPrivate::StrokeDraw);
4713 d->engine->drawPolygon(points, pointCount, QPaintEngine::PolylineMode);
4720 Draws the polyline defined by the first \a pointCount points in \a
4721 points using the current pen.
4723 void QPainter::drawPolyline(const QPoint *points, int pointCount)
4725 #ifdef QT_DEBUG_DRAW
4726 if (qt_show_painter_debug_output)
4727 printf("QPainter::drawPolyline(), count=%d\n", pointCount);
4731 if (!d->engine || pointCount < 2)
4735 d->extended->drawPolygon(points, pointCount, QPaintEngine::PolylineMode);
4739 d->updateState(d->state);
4741 uint lineEmulation = line_emulation(d->state->emulationSpecifier);
4743 if (lineEmulation) {
4745 // if (lineEmulation == QPaintEngine::PrimitiveTransform
4746 // && d->state->matrix.type() == QTransform::TxTranslate) {
4748 QPainterPath polylinePath(points[0]);
4749 for (int i=1; i<pointCount; ++i)
4750 polylinePath.lineTo(points[i]);
4751 d->draw_helper(polylinePath, QPainterPrivate::StrokeDraw);
4754 d->engine->drawPolygon(points, pointCount, QPaintEngine::PolylineMode);
4759 \fn void QPainter::drawPolyline(const QPolygonF &points)
4763 Draws the polyline defined by the given \a points using the
4768 \fn void QPainter::drawPolyline(const QPolygon &points)
4772 Draws the polyline defined by the given \a points using the
4777 Draws the polygon defined by the first \a pointCount points in the
4778 array \a points using the current pen and brush.
4782 \li \inlineimage qpainter-polygon.png
4784 \snippet code/src_gui_painting_qpainter.cpp 14
4787 The first point is implicitly connected to the last point, and the
4788 polygon is filled with the current brush().
4790 If \a fillRule is Qt::WindingFill, the polygon is filled using the
4791 winding fill algorithm. If \a fillRule is Qt::OddEvenFill, the
4792 polygon is filled using the odd-even fill algorithm. See
4793 \l{Qt::FillRule} for a more detailed description of these fill
4796 \sa drawConvexPolygon(), drawPolyline(), {Coordinate System}
4798 void QPainter::drawPolygon(const QPointF *points, int pointCount, Qt::FillRule fillRule)
4800 #ifdef QT_DEBUG_DRAW
4801 if (qt_show_painter_debug_output)
4802 printf("QPainter::drawPolygon(), count=%d\n", pointCount);
4807 if (!d->engine || pointCount < 2)
4811 d->extended->drawPolygon(points, pointCount, QPaintEngine::PolygonDrawMode(fillRule));
4815 d->updateState(d->state);
4817 uint emulationSpecifier = d->state->emulationSpecifier;
4819 if (emulationSpecifier) {
4820 QPainterPath polygonPath(points[0]);
4821 for (int i=1; i<pointCount; ++i)
4822 polygonPath.lineTo(points[i]);
4823 polygonPath.closeSubpath();
4824 polygonPath.setFillRule(fillRule);
4825 d->draw_helper(polygonPath);
4829 d->engine->drawPolygon(points, pointCount, QPaintEngine::PolygonDrawMode(fillRule));
4834 Draws the polygon defined by the first \a pointCount points in the
4837 void QPainter::drawPolygon(const QPoint *points, int pointCount, Qt::FillRule fillRule)
4839 #ifdef QT_DEBUG_DRAW
4840 if (qt_show_painter_debug_output)
4841 printf("QPainter::drawPolygon(), count=%d\n", pointCount);
4846 if (!d->engine || pointCount < 2)
4850 d->extended->drawPolygon(points, pointCount, QPaintEngine::PolygonDrawMode(fillRule));
4854 d->updateState(d->state);
4856 uint emulationSpecifier = d->state->emulationSpecifier;
4858 if (emulationSpecifier) {
4859 QPainterPath polygonPath(points[0]);
4860 for (int i=1; i<pointCount; ++i)
4861 polygonPath.lineTo(points[i]);
4862 polygonPath.closeSubpath();
4863 polygonPath.setFillRule(fillRule);
4864 d->draw_helper(polygonPath);
4868 d->engine->drawPolygon(points, pointCount, QPaintEngine::PolygonDrawMode(fillRule));
4871 /*! \fn void QPainter::drawPolygon(const QPolygonF &points, Qt::FillRule fillRule)
4875 Draws the polygon defined by the given \a points using the fill
4879 /*! \fn void QPainter::drawPolygon(const QPolygon &points, Qt::FillRule fillRule)
4883 Draws the polygon defined by the given \a points using the fill
4888 \fn void QPainter::drawConvexPolygon(const QPointF *points, int pointCount)
4890 Draws the convex polygon defined by the first \a pointCount points
4891 in the array \a points using the current pen.
4895 \li \inlineimage qpainter-polygon.png
4897 \snippet code/src_gui_painting_qpainter.cpp 15
4900 The first point is implicitly connected to the last point, and the
4901 polygon is filled with the current brush(). If the supplied
4902 polygon is not convex, i.e. it contains at least one angle larger
4903 than 180 degrees, the results are undefined.
4905 On some platforms (e.g. X11), the drawConvexPolygon() function can
4906 be faster than the drawPolygon() function.
4908 \sa drawPolygon(), drawPolyline(), {Coordinate System}
4912 \fn void QPainter::drawConvexPolygon(const QPoint *points, int pointCount)
4915 Draws the convex polygon defined by the first \a pointCount points
4916 in the array \a points using the current pen.
4920 \fn void QPainter::drawConvexPolygon(const QPolygonF &polygon)
4924 Draws the convex polygon defined by \a polygon using the current
4929 \fn void QPainter::drawConvexPolygon(const QPolygon &polygon)
4932 Draws the convex polygon defined by \a polygon using the current
4936 void QPainter::drawConvexPolygon(const QPoint *points, int pointCount)
4938 #ifdef QT_DEBUG_DRAW
4939 if (qt_show_painter_debug_output)
4940 printf("QPainter::drawConvexPolygon(), count=%d\n", pointCount);
4945 if (!d->engine || pointCount < 2)
4949 d->extended->drawPolygon(points, pointCount, QPaintEngine::ConvexMode);
4953 d->updateState(d->state);
4955 uint emulationSpecifier = d->state->emulationSpecifier;
4957 if (emulationSpecifier) {
4958 QPainterPath polygonPath(points[0]);
4959 for (int i=1; i<pointCount; ++i)
4960 polygonPath.lineTo(points[i]);
4961 polygonPath.closeSubpath();
4962 polygonPath.setFillRule(Qt::WindingFill);
4963 d->draw_helper(polygonPath);
4967 d->engine->drawPolygon(points, pointCount, QPaintEngine::ConvexMode);
4970 void QPainter::drawConvexPolygon(const QPointF *points, int pointCount)
4972 #ifdef QT_DEBUG_DRAW
4973 if (qt_show_painter_debug_output)
4974 printf("QPainter::drawConvexPolygon(), count=%d\n", pointCount);
4979 if (!d->engine || pointCount < 2)
4983 d->extended->drawPolygon(points, pointCount, QPaintEngine::ConvexMode);
4987 d->updateState(d->state);
4989 uint emulationSpecifier = d->state->emulationSpecifier;
4991 if (emulationSpecifier) {
4992 QPainterPath polygonPath(points[0]);
4993 for (int i=1; i<pointCount; ++i)
4994 polygonPath.lineTo(points[i]);
4995 polygonPath.closeSubpath();
4996 polygonPath.setFillRule(Qt::WindingFill);
4997 d->draw_helper(polygonPath);
5001 d->engine->drawPolygon(points, pointCount, QPaintEngine::ConvexMode);
5004 static inline QPointF roundInDeviceCoordinates(const QPointF &p, const QTransform &m)
5006 return m.inverted().map(QPointF(m.map(p).toPoint()));
5010 \fn void QPainter::drawPixmap(const QRectF &target, const QPixmap &pixmap, const QRectF &source)
5012 Draws the rectangular portion \a source of the given \a pixmap
5013 into the given \a target in the paint device.
5015 \note The pixmap is scaled to fit the rectangle, if both the pixmap and rectangle size disagree.
5020 \snippet code/src_gui_painting_qpainter.cpp 16
5023 If \a pixmap is a QBitmap it is drawn with the bits that are "set"
5024 using the pens color. If backgroundMode is Qt::OpaqueMode, the
5025 "unset" bits are drawn using the color of the background brush; if
5026 backgroundMode is Qt::TransparentMode, the "unset" bits are
5027 transparent. Drawing bitmaps with gradient or texture colors is
5032 void QPainter::drawPixmap(const QPointF &p, const QPixmap &pm)
5034 #if defined QT_DEBUG_DRAW
5035 if (qt_show_painter_debug_output)
5036 printf("QPainter::drawPixmap(), p=[%.2f,%.2f], pix=[%d,%d]\n",
5038 pm.width(), pm.height());
5043 if (!d->engine || pm.isNull())
5047 qt_painter_thread_test(d->device->devType(), "drawPixmap()", true);
5051 d->extended->drawPixmap(p, pm);
5059 int h = pm.height();
5064 // Emulate opaque background for bitmaps
5065 if (d->state->bgMode == Qt::OpaqueMode && pm.isQBitmap()) {
5066 fillRect(QRectF(x, y, w, h), d->state->bgBrush.color());
5069 d->updateState(d->state);
5071 if ((d->state->matrix.type() > QTransform::TxTranslate
5072 && !d->engine->hasFeature(QPaintEngine::PixmapTransform))
5073 || (!d->state->matrix.isAffine() && !d->engine->hasFeature(QPaintEngine::PerspectiveTransform))
5074 || (d->state->opacity != 1.0 && !d->engine->hasFeature(QPaintEngine::ConstantOpacity)))
5077 // If there is no rotation involved we have to make sure we use the
5078 // antialiased and not the aliased coordinate system by rounding the coordinates.
5079 if (d->state->matrix.type() <= QTransform::TxScale) {
5080 const QPointF p = roundInDeviceCoordinates(QPointF(x, y), d->state->matrix);
5085 setBackgroundMode(Qt::TransparentMode);
5086 setRenderHint(Antialiasing, renderHints() & SmoothPixmapTransform);
5087 QBrush brush(d->state->pen.color(), pm);
5090 setBrushOrigin(QPointF(0, 0));
5092 drawRect(pm.rect());
5095 if (!d->engine->hasFeature(QPaintEngine::PixmapTransform)) {
5096 x += d->state->matrix.dx();
5097 y += d->state->matrix.dy();
5099 d->engine->drawPixmap(QRectF(x, y, w, h), pm, QRectF(0, 0, w, h));
5103 void QPainter::drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr)
5105 #if defined QT_DEBUG_DRAW
5106 if (qt_show_painter_debug_output)
5107 printf("QPainter::drawPixmap(), target=[%.2f,%.2f,%.2f,%.2f], pix=[%d,%d], source=[%.2f,%.2f,%.2f,%.2f]\n",
5108 r.x(), r.y(), r.width(), r.height(),
5109 pm.width(), pm.height(),
5110 sr.x(), sr.y(), sr.width(), sr.height());
5114 if (!d->engine || pm.isNull())
5117 qt_painter_thread_test(d->device->devType(), "drawPixmap()", true);
5122 qreal w = r.width();
5123 qreal h = r.height();
5126 qreal sw = sr.width();
5127 qreal sh = sr.height();
5129 // Sanity-check clipping
5131 sw = pm.width() - sx;
5134 sh = pm.height() - sy;
5142 qreal w_ratio = sx * w/sw;
5150 qreal h_ratio = sy * h/sh;
5157 if (sw + sx > pm.width()) {
5158 qreal delta = sw - (pm.width() - sx);
5159 qreal w_ratio = delta * w/sw;
5164 if (sh + sy > pm.height()) {
5165 qreal delta = sh - (pm.height() - sy);
5166 qreal h_ratio = delta * h/sh;
5171 if (w == 0 || h == 0 || sw <= 0 || sh <= 0)
5175 d->extended->drawPixmap(QRectF(x, y, w, h), pm, QRectF(sx, sy, sw, sh));
5179 // Emulate opaque background for bitmaps
5180 if (d->state->bgMode == Qt::OpaqueMode && pm.isQBitmap())
5181 fillRect(QRectF(x, y, w, h), d->state->bgBrush.color());
5183 d->updateState(d->state);
5185 if ((d->state->matrix.type() > QTransform::TxTranslate
5186 && !d->engine->hasFeature(QPaintEngine::PixmapTransform))
5187 || (!d->state->matrix.isAffine() && !d->engine->hasFeature(QPaintEngine::PerspectiveTransform))
5188 || (d->state->opacity != 1.0 && !d->engine->hasFeature(QPaintEngine::ConstantOpacity))
5189 || ((sw != w || sh != h) && !d->engine->hasFeature(QPaintEngine::PixmapTransform)))
5192 // If there is no rotation involved we have to make sure we use the
5193 // antialiased and not the aliased coordinate system by rounding the coordinates.
5194 if (d->state->matrix.type() <= QTransform::TxScale) {
5195 const QPointF p = roundInDeviceCoordinates(QPointF(x, y), d->state->matrix);
5200 if (d->state->matrix.type() <= QTransform::TxTranslate && sw == w && sh == h) {
5208 scale(w / sw, h / sh);
5209 setBackgroundMode(Qt::TransparentMode);
5210 setRenderHint(Antialiasing, renderHints() & SmoothPixmapTransform);
5213 if (sw == pm.width() && sh == pm.height())
5214 brush = QBrush(d->state->pen.color(), pm);
5216 brush = QBrush(d->state->pen.color(), pm.copy(sx, sy, sw, sh));
5221 drawRect(QRectF(0, 0, sw, sh));
5224 if (!d->engine->hasFeature(QPaintEngine::PixmapTransform)) {
5225 x += d->state->matrix.dx();
5226 y += d->state->matrix.dy();
5228 d->engine->drawPixmap(QRectF(x, y, w, h), pm, QRectF(sx, sy, sw, sh));
5234 \fn void QPainter::drawPixmap(const QRect &target, const QPixmap &pixmap,
5235 const QRect &source)
5238 Draws the rectangular portion \a source of the given \a pixmap
5239 into the given \a target in the paint device.
5241 \note The pixmap is scaled to fit the rectangle, if both the pixmap and rectangle size disagree.
5245 \fn void QPainter::drawPixmap(const QPointF &point, const QPixmap &pixmap,
5246 const QRectF &source)
5249 Draws the rectangular portion \a source of the given \a pixmap
5250 with its origin at the given \a point.
5254 \fn void QPainter::drawPixmap(const QPoint &point, const QPixmap &pixmap,
5255 const QRect &source)
5259 Draws the rectangular portion \a source of the given \a pixmap
5260 with its origin at the given \a point.
5264 \fn void QPainter::drawPixmap(const QPointF &point, const QPixmap &pixmap)
5267 Draws the given \a pixmap with its origin at the given \a point.
5271 \fn void QPainter::drawPixmap(const QPoint &point, const QPixmap &pixmap)
5274 Draws the given \a pixmap with its origin at the given \a point.
5278 \fn void QPainter::drawPixmap(int x, int y, const QPixmap &pixmap)
5282 Draws the given \a pixmap at position (\a{x}, \a{y}).
5286 \fn void QPainter::drawPixmap(const QRect &rectangle, const QPixmap &pixmap)
5289 Draws the given \a pixmap into the given \a rectangle.
5291 \note The pixmap is scaled to fit the rectangle, if both the pixmap and rectangle size disagree.
5295 \fn void QPainter::drawPixmap(int x, int y, int width, int height,
5296 const QPixmap &pixmap)
5300 Draws the \a pixmap into the rectangle at position (\a{x}, \a{y})
5301 with the given \a width and \a height.
5305 \fn void QPainter::drawPixmap(int x, int y, int w, int h, const QPixmap &pixmap,
5306 int sx, int sy, int sw, int sh)
5310 Draws the rectangular portion with the origin (\a{sx}, \a{sy}),
5311 width \a sw and height \a sh, of the given \a pixmap , at the
5312 point (\a{x}, \a{y}), with a width of \a w and a height of \a h.
5313 If sw or sh are equal to zero the width/height of the pixmap
5314 is used and adjusted by the offset sx/sy;
5318 \fn void QPainter::drawPixmap(int x, int y, const QPixmap &pixmap,
5319 int sx, int sy, int sw, int sh)
5323 Draws a pixmap at (\a{x}, \a{y}) by copying a part of the given \a
5324 pixmap into the paint device.
5326 (\a{x}, \a{y}) specifies the top-left point in the paint device that is
5327 to be drawn onto. (\a{sx}, \a{sy}) specifies the top-left point in \a
5328 pixmap that is to be drawn. The default is (0, 0).
5330 (\a{sw}, \a{sh}) specifies the size of the pixmap that is to be drawn.
5331 The default, (0, 0) (and negative) means all the way to the
5332 bottom-right of the pixmap.
5335 void QPainter::drawImage(const QPointF &p, const QImage &image)
5339 if (!d->engine || image.isNull())
5343 d->extended->drawImage(p, image);
5350 int w = image.width();
5351 int h = image.height();
5353 d->updateState(d->state);
5355 if (((d->state->matrix.type() > QTransform::TxTranslate)
5356 && !d->engine->hasFeature(QPaintEngine::PixmapTransform))
5357 || (!d->state->matrix.isAffine() && !d->engine->hasFeature(QPaintEngine::PerspectiveTransform))
5358 || (d->state->opacity != 1.0 && !d->engine->hasFeature(QPaintEngine::ConstantOpacity)))
5361 // If there is no rotation involved we have to make sure we use the
5362 // antialiased and not the aliased coordinate system by rounding the coordinates.
5363 if (d->state->matrix.type() <= QTransform::TxScale) {
5364 const QPointF p = roundInDeviceCoordinates(QPointF(x, y), d->state->matrix);
5369 setBackgroundMode(Qt::TransparentMode);
5370 setRenderHint(Antialiasing, renderHints() & SmoothPixmapTransform);
5371 QBrush brush(image);
5374 setBrushOrigin(QPointF(0, 0));
5376 drawRect(image.rect());
5381 if (d->state->matrix.type() == QTransform::TxTranslate
5382 && !d->engine->hasFeature(QPaintEngine::PixmapTransform)) {
5383 x += d->state->matrix.dx();
5384 y += d->state->matrix.dy();
5387 d->engine->drawImage(QRectF(x, y, w, h), image, QRectF(0, 0, w, h), Qt::AutoColor);
5390 void QPainter::drawImage(const QRectF &targetRect, const QImage &image, const QRectF &sourceRect,
5391 Qt::ImageConversionFlags flags)
5395 if (!d->engine || image.isNull())
5398 qreal x = targetRect.x();
5399 qreal y = targetRect.y();
5400 qreal w = targetRect.width();
5401 qreal h = targetRect.height();
5402 qreal sx = sourceRect.x();
5403 qreal sy = sourceRect.y();
5404 qreal sw = sourceRect.width();
5405 qreal sh = sourceRect.height();
5407 // Sanity-check clipping
5409 sw = image.width() - sx;
5412 sh = image.height() - sy;
5420 qreal w_ratio = sx * w/sw;
5428 qreal h_ratio = sy * h/sh;
5435 if (sw + sx > image.width()) {
5436 qreal delta = sw - (image.width() - sx);
5437 qreal w_ratio = delta * w/sw;
5442 if (sh + sy > image.height()) {
5443 qreal delta = sh - (image.height() - sy);
5444 qreal h_ratio = delta * h/sh;
5449 if (w == 0 || h == 0 || sw <= 0 || sh <= 0)
5453 d->extended->drawImage(QRectF(x, y, w, h), image, QRectF(sx, sy, sw, sh), flags);
5457 d->updateState(d->state);
5459 if (((d->state->matrix.type() > QTransform::TxTranslate || (sw != w || sh != h))
5460 && !d->engine->hasFeature(QPaintEngine::PixmapTransform))
5461 || (!d->state->matrix.isAffine() && !d->engine->hasFeature(QPaintEngine::PerspectiveTransform))
5462 || (d->state->opacity != 1.0 && !d->engine->hasFeature(QPaintEngine::ConstantOpacity)))
5465 // If there is no rotation involved we have to make sure we use the
5466 // antialiased and not the aliased coordinate system by rounding the coordinates.
5467 if (d->state->matrix.type() <= QTransform::TxScale) {
5468 const QPointF p = roundInDeviceCoordinates(QPointF(x, y), d->state->matrix);
5473 if (d->state->matrix.type() <= QTransform::TxTranslate && sw == w && sh == h) {
5480 scale(w / sw, h / sh);
5481 setBackgroundMode(Qt::TransparentMode);
5482 setRenderHint(Antialiasing, renderHints() & SmoothPixmapTransform);
5483 QBrush brush(image);
5486 setBrushOrigin(QPointF(-sx, -sy));
5488 drawRect(QRectF(0, 0, sw, sh));
5493 if (d->state->matrix.type() == QTransform::TxTranslate
5494 && !d->engine->hasFeature(QPaintEngine::PixmapTransform)) {
5495 x += d->state->matrix.dx();
5496 y += d->state->matrix.dy();
5499 d->engine->drawImage(QRectF(x, y, w, h), image, QRectF(sx, sy, sw, sh), flags);
5503 \fn void QPainter::drawGlyphRun(const QPointF &position, const QGlyphRun &glyphs)
5505 Draws the glyphs represented by \a glyphs at \a position. The \a position gives the
5506 edge of the baseline for the string of glyphs. The glyphs will be retrieved from the font
5507 selected on \a glyphs and at offsets given by the positions in \a glyphs.
5511 \sa QGlyphRun::setRawFont(), QGlyphRun::setPositions(), QGlyphRun::setGlyphIndexes()
5513 #if !defined(QT_NO_RAWFONT)
5514 void QPainter::drawGlyphRun(const QPointF &position, const QGlyphRun &glyphRun)
5518 QRawFont font = glyphRun.rawFont();
5519 if (!font.isValid())
5522 QGlyphRunPrivate *glyphRun_d = QGlyphRunPrivate::get(glyphRun);
5524 const quint32 *glyphIndexes = glyphRun_d->glyphIndexData;
5525 const QPointF *glyphPositions = glyphRun_d->glyphPositionData;
5527 int count = qMin(glyphRun_d->glyphIndexDataSize, glyphRun_d->glyphPositionDataSize);
5528 QVarLengthArray<QFixedPoint, 128> fixedPointPositions(count);
5530 QRawFontPrivate *fontD = QRawFontPrivate::get(font);
5531 bool supportsTransformations = d->extended
5532 ? d->extended->supportsTransformations(fontD->fontEngine, d->state->matrix)
5533 : d->engine->type() == QPaintEngine::CoreGraphics || d->state->matrix.isAffine();
5535 for (int i=0; i<count; ++i) {
5536 QPointF processedPosition = position + glyphPositions[i];
5537 if (!supportsTransformations)
5538 processedPosition = d->state->transform().map(processedPosition);
5539 fixedPointPositions[i] = QFixedPoint::fromPointF(processedPosition);
5542 d->drawGlyphs(glyphIndexes, fixedPointPositions.data(), count, font, glyphRun.overline(),
5543 glyphRun.underline(), glyphRun.strikeOut());
5546 void QPainterPrivate::drawGlyphs(const quint32 *glyphArray, QFixedPoint *positions,
5548 const QRawFont &font, bool overline, bool underline,
5555 QRawFontPrivate *fontD = QRawFontPrivate::get(font);
5556 QFontEngine *fontEngine = fontD->fontEngine;
5561 for (int i=0; i<glyphCount; ++i) {
5562 glyph_metrics_t gm = fontEngine->boundingBox(glyphArray[i]);
5563 if (i == 0 || leftMost > positions[i].x)
5564 leftMost = positions[i].x;
5566 // We don't support glyphs that do not share a common baseline. If this turns out to
5567 // be a relevant use case, then we need to find clusters of glyphs that share a baseline
5568 // and do a drawTextItemDecorations call per cluster.
5569 if (i == 0 || baseLine < positions[i].y)
5570 baseLine = positions[i].y;
5572 // We use the advance rather than the actual bounds to match the algorithm in drawText()
5573 if (i == 0 || rightMost < positions[i].x + gm.xoff)
5574 rightMost = positions[i].x + gm.xoff;
5577 QFixed width = rightMost - leftMost;
5579 if (extended != 0 && state->matrix.isAffine()) {
5580 QStaticTextItem staticTextItem;
5581 staticTextItem.color = state->pen.color();
5582 staticTextItem.font = state->font;
5583 staticTextItem.setFontEngine(fontEngine);
5584 staticTextItem.numGlyphs = glyphCount;
5585 staticTextItem.glyphs = reinterpret_cast<glyph_t *>(const_cast<glyph_t *>(glyphArray));
5586 staticTextItem.glyphPositions = positions;
5588 extended->drawStaticTextItem(&staticTextItem);
5590 QTextItemInt textItem;
5591 textItem.fontEngine = fontEngine;
5593 QVarLengthArray<QFixed, 128> advances(glyphCount);
5594 QVarLengthArray<QGlyphJustification, 128> glyphJustifications(glyphCount);
5595 QVarLengthArray<HB_GlyphAttributes, 128> glyphAttributes(glyphCount);
5596 memset(glyphAttributes.data(), 0, glyphAttributes.size() * sizeof(HB_GlyphAttributes));
5597 memset(advances.data(), 0, advances.size() * sizeof(QFixed));
5598 memset(glyphJustifications.data(), 0, glyphJustifications.size() * sizeof(QGlyphJustification));
5600 textItem.glyphs.numGlyphs = glyphCount;
5601 textItem.glyphs.glyphs = reinterpret_cast<HB_Glyph *>(const_cast<quint32 *>(glyphArray));
5602 textItem.glyphs.offsets = positions;
5603 textItem.glyphs.advances_x = advances.data();
5604 textItem.glyphs.advances_y = advances.data();
5605 textItem.glyphs.justifications = glyphJustifications.data();
5606 textItem.glyphs.attributes = glyphAttributes.data();
5608 engine->drawTextItem(QPointF(0, 0), textItem);
5611 QTextItemInt::RenderFlags flags;
5613 flags |= QTextItemInt::Underline;
5615 flags |= QTextItemInt::Overline;
5617 flags |= QTextItemInt::StrikeOut;
5619 drawTextItemDecoration(q, QPointF(leftMost.toReal(), baseLine.toReal()),
5623 ? QTextCharFormat::SingleUnderline
5624 : QTextCharFormat::NoUnderline),
5625 flags, width.toReal(), QTextCharFormat());
5627 #endif // QT_NO_RAWFONT
5631 \fn void QPainter::drawStaticText(const QPoint &topLeftPosition, const QStaticText &staticText)
5635 Draws the \a staticText at the \a topLeftPosition.
5637 \note The y-position is used as the top of the font.
5642 \fn void QPainter::drawStaticText(int left, int top, const QStaticText &staticText)
5646 Draws the \a staticText at coordinates \a left and \a top.
5648 \note The y-position is used as the top of the font.
5652 \fn void QPainter::drawText(const QPointF &position, const QString &text)
5654 Draws the given \a text with the currently defined text direction,
5655 beginning at the given \a position.
5657 This function does not handle the newline character (\\n), as it cannot
5658 break text into multiple lines, and it cannot display the newline character.
5659 Use the QPainter::drawText() overload that takes a rectangle instead
5660 if you want to draw multiple lines of text with the newline character, or
5661 if you want the text to be wrapped.
5663 By default, QPainter draws text anti-aliased.
5665 \note The y-position is used as the baseline of the font.
5667 \sa setFont(), setPen()
5670 void QPainter::drawText(const QPointF &p, const QString &str)
5672 drawText(p, str, 0, 0);
5678 Draws the given \a staticText at the given \a topLeftPosition.
5680 The text will be drawn using the font and the transformation set on the painter. If the
5681 font and/or transformation set on the painter are different from the ones used to initialize
5682 the layout of the QStaticText, then the layout will have to be recalculated. Use
5683 QStaticText::prepare() to initialize \a staticText with the font and transformation with which
5684 it will later be drawn.
5686 If \a topLeftPosition is not the same as when \a staticText was initialized, or when it was
5687 last drawn, then there will be a slight overhead when translating the text to its new position.
5689 \note If the painter's transformation is not affine, then \a staticText will be drawn using
5690 regular calls to drawText(), losing any potential for performance improvement.
5692 \note The y-position is used as the top of the font.
5696 void QPainter::drawStaticText(const QPointF &topLeftPosition, const QStaticText &staticText)
5699 if (!d->engine || staticText.text().isEmpty() || pen().style() == Qt::NoPen)
5702 QStaticTextPrivate *staticText_d =
5703 const_cast<QStaticTextPrivate *>(QStaticTextPrivate::get(&staticText));
5705 if (font() != staticText_d->font) {
5706 staticText_d->font = font();
5707 staticText_d->needsRelayout = true;
5710 // If we don't have an extended paint engine, or if the painter is projected,
5711 // we go through standard code path
5712 if (d->extended == 0 || !d->state->matrix.isAffine()) {
5713 staticText_d->paintText(topLeftPosition, this);
5717 QFontEngine *fe = staticText_d->font.d->engineForScript(QUnicodeTables::Common);
5718 if (fe->type() == QFontEngine::Multi)
5719 fe = static_cast<QFontEngineMulti *>(fe)->engine(0);
5720 bool supportsTransformations = d->extended->supportsTransformations(fe,
5722 if (supportsTransformations && !staticText_d->untransformedCoordinates) {
5723 staticText_d->untransformedCoordinates = true;
5724 staticText_d->needsRelayout = true;
5725 } else if (!supportsTransformations && staticText_d->untransformedCoordinates) {
5726 staticText_d->untransformedCoordinates = false;
5727 staticText_d->needsRelayout = true;
5730 // Don't recalculate entire layout because of translation, rather add the dx and dy
5731 // into the position to move each text item the correct distance.
5732 QPointF transformedPosition = topLeftPosition;
5733 if (!staticText_d->untransformedCoordinates)
5734 transformedPosition = transformedPosition * d->state->matrix;
5735 QTransform oldMatrix;
5737 // The translation has been applied to transformedPosition. Remove translation
5738 // component from matrix.
5739 if (d->state->matrix.isTranslating() && !staticText_d->untransformedCoordinates) {
5740 qreal m11 = d->state->matrix.m11();
5741 qreal m12 = d->state->matrix.m12();
5742 qreal m13 = d->state->matrix.m13();
5743 qreal m21 = d->state->matrix.m21();
5744 qreal m22 = d->state->matrix.m22();
5745 qreal m23 = d->state->matrix.m23();
5746 qreal m33 = d->state->matrix.m33();
5748 oldMatrix = d->state->matrix;
5749 d->state->matrix.setMatrix(m11, m12, m13,
5754 // If the transform is not identical to the text transform,
5755 // we have to relayout the text (for other transformations than plain translation)
5756 bool staticTextNeedsReinit = staticText_d->needsRelayout;
5757 if (!staticText_d->untransformedCoordinates && staticText_d->matrix != d->state->matrix) {
5758 staticText_d->matrix = d->state->matrix;
5759 staticTextNeedsReinit = true;
5762 // Recreate the layout of the static text because the matrix or font has changed
5763 if (staticTextNeedsReinit)
5764 staticText_d->init();
5766 if (transformedPosition != staticText_d->position) { // Translate to actual position
5767 QFixed fx = QFixed::fromReal(transformedPosition.x());
5768 QFixed fy = QFixed::fromReal(transformedPosition.y());
5769 QFixed oldX = QFixed::fromReal(staticText_d->position.x());
5770 QFixed oldY = QFixed::fromReal(staticText_d->position.y());
5771 for (int item=0; item<staticText_d->itemCount;++item) {
5772 QStaticTextItem *textItem = staticText_d->items + item;
5773 for (int i=0; i<textItem->numGlyphs; ++i) {
5774 textItem->glyphPositions[i].x += fx - oldX;
5775 textItem->glyphPositions[i].y += fy - oldY;
5777 textItem->userDataNeedsUpdate = true;
5780 staticText_d->position = transformedPosition;
5783 QPen oldPen = d->state->pen;
5784 QColor currentColor = oldPen.color();
5785 for (int i=0; i<staticText_d->itemCount; ++i) {
5786 QStaticTextItem *item = staticText_d->items + i;
5787 if (item->color.isValid() && currentColor != item->color) {
5788 setPen(item->color);
5789 currentColor = item->color;
5791 d->extended->drawStaticTextItem(item);
5793 qt_draw_decoration_for_glyphs(this, item->glyphs, item->glyphPositions,
5794 item->numGlyphs, item->fontEngine(), staticText_d->font,
5797 if (currentColor != oldPen.color())
5800 if (!staticText_d->untransformedCoordinates && oldMatrix.isTranslating())
5801 d->state->matrix = oldMatrix;
5807 void QPainter::drawText(const QPointF &p, const QString &str, int tf, int justificationPadding)
5809 #ifdef QT_DEBUG_DRAW
5810 if (qt_show_painter_debug_output)
5811 printf("QPainter::drawText(), pos=[%.2f,%.2f], str='%s'\n", p.x(), p.y(), str.toLatin1().constData());
5816 if (!d->engine || str.isEmpty() || pen().style() == Qt::NoPen)
5819 if (tf & Qt::TextBypassShaping) {
5820 // Skip harfbuzz complex shaping, shape using glyph advances only
5821 int len = str.length();
5822 int numGlyphs = len;
5823 QVarLengthGlyphLayoutArray glyphs(len);
5824 QFontEngine *fontEngine = d->state->font.d->engineForScript(QUnicodeTables::Common);
5825 if (!fontEngine->stringToCMap(str.data(), len, &glyphs, &numGlyphs, 0)) {
5826 glyphs.resize(numGlyphs);
5827 if (!fontEngine->stringToCMap(str.data(), len, &glyphs, &numGlyphs, 0))
5828 Q_ASSERT_X(false, Q_FUNC_INFO, "stringToCMap shouldn't fail twice");
5831 QTextItemInt gf(glyphs, &d->state->font, str.data(), len, fontEngine);
5832 drawTextItem(p, gf);
5836 QStackTextEngine engine(str, d->state->font);
5837 engine.option.setTextDirection(d->state->layoutDirection);
5838 if (tf & (Qt::TextForceLeftToRight|Qt::TextForceRightToLeft)) {
5839 engine.ignoreBidi = true;
5840 engine.option.setTextDirection((tf & Qt::TextForceLeftToRight) ? Qt::LeftToRight : Qt::RightToLeft);
5844 line.length = str.length();
5845 engine.shapeLine(line);
5847 int nItems = engine.layoutData->items.size();
5848 QVarLengthArray<int> visualOrder(nItems);
5849 QVarLengthArray<uchar> levels(nItems);
5850 for (int i = 0; i < nItems; ++i)
5851 levels[i] = engine.layoutData->items[i].analysis.bidiLevel;
5852 QTextEngine::bidiReorder(nItems, levels.data(), visualOrder.data());
5854 if (justificationPadding > 0) {
5855 engine.option.setAlignment(Qt::AlignJustify);
5856 engine.forceJustification = true;
5857 // this works because justify() is only interested in the difference between width and textWidth
5858 line.width = justificationPadding;
5859 engine.justify(line);
5861 QFixed x = QFixed::fromReal(p.x());
5863 for (int i = 0; i < nItems; ++i) {
5864 int item = visualOrder[i];
5865 const QScriptItem &si = engine.layoutData->items.at(item);
5866 if (si.analysis.flags >= QScriptAnalysis::TabOrObject) {
5870 QFont f = engine.font(si);
5871 QTextItemInt gf(si, &f);
5872 gf.glyphs = engine.shapedGlyphs(&si);
5873 gf.chars = engine.layoutData->string.unicode() + si.position;
5874 gf.num_chars = engine.length(item);
5875 if (engine.forceJustification) {
5876 for (int j=0; j<gf.glyphs.numGlyphs; ++j)
5877 gf.width += gf.glyphs.effectiveAdvance(j);
5879 gf.width = si.width;
5881 gf.logClusters = engine.logClusters(&si);
5883 drawTextItem(QPointF(x.toReal(), p.y()), gf);
5889 void QPainter::drawText(const QRect &r, int flags, const QString &str, QRect *br)
5891 #ifdef QT_DEBUG_DRAW
5892 if (qt_show_painter_debug_output)
5893 printf("QPainter::drawText(), r=[%d,%d,%d,%d], flags=%d, str='%s'\n",
5894 r.x(), r.y(), r.width(), r.height(), flags, str.toLatin1().constData());
5899 if (!d->engine || str.length() == 0 || pen().style() == Qt::NoPen)
5903 d->updateState(d->state);
5906 qt_format_text(d->state->font, r, flags, 0, str, br ? &bounds : 0, 0, 0, 0, this);
5908 *br = bounds.toAlignedRect();
5912 \fn void QPainter::drawText(const QPoint &position, const QString &text)
5916 Draws the given \a text with the currently defined text direction,
5917 beginning at the given \a position.
5919 By default, QPainter draws text anti-aliased.
5921 \note The y-position is used as the baseline of the font.
5923 \sa setFont(), setPen()
5927 \fn void QPainter::drawText(const QRectF &rectangle, int flags, const QString &text, QRectF *boundingRect)
5930 Draws the given \a text within the provided \a rectangle.
5931 The \a rectangle along with alignment \a flags defines the anchors for the \a text.
5935 \li \inlineimage qpainter-text.png
5937 \snippet code/src_gui_painting_qpainter.cpp 17
5940 The \a boundingRect (if not null) is set to the what the bounding rectangle
5941 should be in order to enclose the whole text. The \a flags argument is a bitwise
5942 OR of the following flags:
5947 \li Qt::AlignHCenter
5948 \li Qt::AlignJustify
5951 \li Qt::AlignVCenter
5953 \li Qt::TextDontClip
5954 \li Qt::TextSingleLine
5955 \li Qt::TextExpandTabs
5956 \li Qt::TextShowMnemonic
5957 \li Qt::TextWordWrap
5958 \li Qt::TextIncludeTrailingSpaces
5961 \sa Qt::AlignmentFlag, Qt::TextFlag, boundingRect(), layoutDirection()
5963 By default, QPainter draws text anti-aliased.
5965 \note The y-coordinate of \a rectangle is used as the top of the font.
5967 void QPainter::drawText(const QRectF &r, int flags, const QString &str, QRectF *br)
5969 #ifdef QT_DEBUG_DRAW
5970 if (qt_show_painter_debug_output)
5971 printf("QPainter::drawText(), r=[%.2f,%.2f,%.2f,%.2f], flags=%d, str='%s'\n",
5972 r.x(), r.y(), r.width(), r.height(), flags, str.toLatin1().constData());
5977 if (!d->engine || str.length() == 0 || pen().style() == Qt::NoPen)
5981 d->updateState(d->state);
5983 qt_format_text(d->state->font, r, flags, 0, str, br, 0, 0, 0, this);
5987 \fn void QPainter::drawText(const QRect &rectangle, int flags, const QString &text, QRect *boundingRect)
5990 Draws the given \a text within the provided \a rectangle according
5991 to the specified \a flags. The \a boundingRect (if not null) is set to
5992 the what the bounding rectangle should be in order to enclose the whole text.
5994 By default, QPainter draws text anti-aliased.
5996 \note The y-coordinate of \a rectangle is used as the top of the font.
5998 \sa setFont(), setPen()
6002 \fn void QPainter::drawText(int x, int y, const QString &text)
6006 Draws the given \a text at position (\a{x}, \a{y}), using the painter's
6007 currently defined text direction.
6009 By default, QPainter draws text anti-aliased.
6011 \note The y-position is used as the baseline of the font.
6013 \sa setFont(), setPen()
6017 \fn void QPainter::drawText(int x, int y, int width, int height, int flags,
6018 const QString &text, QRect *boundingRect)
6022 Draws the given \a text within the rectangle with origin (\a{x},
6023 \a{y}), \a width and \a height.
6025 The \a boundingRect (if not null) is set to the actual bounding
6026 rectangle of the output. The \a flags argument is a bitwise OR of
6027 the following flags:
6032 \li Qt::AlignHCenter
6033 \li Qt::AlignJustify
6036 \li Qt::AlignVCenter
6038 \li Qt::TextSingleLine
6039 \li Qt::TextExpandTabs
6040 \li Qt::TextShowMnemonic
6041 \li Qt::TextWordWrap
6044 By default, QPainter draws text anti-aliased.
6046 \note The y-position is used as the top of the font.
6048 \sa Qt::AlignmentFlag, Qt::TextFlag, setFont(), setPen()
6052 \fn void QPainter::drawText(const QRectF &rectangle, const QString &text,
6053 const QTextOption &option)
6056 Draws the given \a text in the \a rectangle specified using the \a option
6057 to control its positioning and orientation.
6059 By default, QPainter draws text anti-aliased.
6061 \note The y-coordinate of \a rectangle is used as the top of the font.
6063 \sa setFont(), setPen()
6065 void QPainter::drawText(const QRectF &r, const QString &text, const QTextOption &o)
6067 #ifdef QT_DEBUG_DRAW
6068 if (qt_show_painter_debug_output)
6069 printf("QPainter::drawText(), r=[%.2f,%.2f,%.2f,%.2f], str='%s'\n",
6070 r.x(), r.y(), r.width(), r.height(), text.toLatin1().constData());
6075 if (!d->engine || text.length() == 0 || pen().style() == Qt::NoPen)
6079 d->updateState(d->state);
6081 qt_format_text(d->state->font, r, 0, &o, text, 0, 0, 0, 0, this);
6085 \fn void QPainter::drawTextItem(int x, int y, const QTextItem &ti)
6092 \fn void QPainter::drawTextItem(const QPoint &p, const QTextItem &ti)
6097 Draws the text item \a ti at position \a p.
6101 \fn void QPainter::drawTextItem(const QPointF &p, const QTextItem &ti)
6106 Draws the text item \a ti at position \a p.
6108 This method ignores the painters background mode and
6109 color. drawText and qt_format_text have to do it themselves, as
6110 only they know the extents of the complete string.
6112 It ignores the font set on the painter as the text item has one of its own.
6114 The underline and strikeout parameters of the text items font are
6115 ignored aswell. You'll need to pass in the correct flags to get
6116 underlining and strikeout.
6119 static QPixmap generateWavyPixmap(qreal maxRadius, const QPen &pen)
6121 const qreal radiusBase = qMax(qreal(1), maxRadius);
6123 QString key = QLatin1String("WaveUnderline-")
6124 % pen.color().name()
6125 % HexString<qreal>(radiusBase);
6128 if (QPixmapCache::find(key, pixmap))
6131 const qreal halfPeriod = qMax(qreal(2), qreal(radiusBase * 1.61803399)); // the golden ratio
6132 const int width = qCeil(100 / (2 * halfPeriod)) * (2 * halfPeriod);
6133 const int radius = qFloor(radiusBase);
6140 while (xs < width) {
6143 path.quadTo(xs - halfPeriod / 2, ys, xs, 0);
6146 pixmap = QPixmap(width, radius * 2);
6147 pixmap.fill(Qt::transparent);
6150 wavePen.setCapStyle(Qt::SquareCap);
6152 // This is to protect against making the line too fat, as happens on Mac OS X
6153 // due to it having a rather thick width for the regular underline.
6154 const qreal maxPenWidth = .8 * radius;
6155 if (wavePen.widthF() > maxPenWidth)
6156 wavePen.setWidth(maxPenWidth);
6158 QPainter imgPainter(&pixmap);
6159 imgPainter.setPen(wavePen);
6160 imgPainter.setRenderHint(QPainter::Antialiasing);
6161 imgPainter.translate(0, radius);
6162 imgPainter.drawPath(path);
6165 QPixmapCache::insert(key, pixmap);
6170 static void drawTextItemDecoration(QPainter *painter, const QPointF &pos, const QFontEngine *fe, QTextEngine *textEngine,
6171 QTextCharFormat::UnderlineStyle underlineStyle,
6172 QTextItem::RenderFlags flags, qreal width,
6173 const QTextCharFormat &charFormat)
6175 if (underlineStyle == QTextCharFormat::NoUnderline
6176 && !(flags & (QTextItem::StrikeOut | QTextItem::Overline)))
6179 const QPen oldPen = painter->pen();
6180 const QBrush oldBrush = painter->brush();
6181 painter->setBrush(Qt::NoBrush);
6183 pen.setStyle(Qt::SolidLine);
6184 pen.setWidthF(fe->lineThickness().toReal());
6185 pen.setCapStyle(Qt::FlatCap);
6187 QLineF line(pos.x(), pos.y(), pos.x() + qFloor(width), pos.y());
6189 const qreal underlineOffset = fe->underlinePosition().toReal();
6190 // deliberately ceil the offset to avoid the underline coming too close to
6191 // the text above it.
6192 const qreal underlinePos = pos.y() + qCeil(underlineOffset);
6194 if (underlineStyle == QTextCharFormat::SpellCheckUnderline) {
6195 QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme();
6197 underlineStyle = QTextCharFormat::UnderlineStyle(theme->themeHint(QPlatformTheme::SpellCheckUnderlineStyle).toInt());
6200 if (underlineStyle == QTextCharFormat::WaveUnderline) {
6202 painter->translate(0, pos.y() + 1);
6204 QColor uc = charFormat.underlineColor();
6208 // Adapt wave to underlineOffset or pen width, whatever is larger, to make it work on all platforms
6209 const QPixmap wave = generateWavyPixmap(qMax(underlineOffset, pen.widthF()), pen);
6210 const int descent = (int) fe->descent().toReal();
6212 painter->setBrushOrigin(painter->brushOrigin().x(), 0);
6213 painter->fillRect(pos.x(), 0, qCeil(width), qMin(wave.height(), descent), wave);
6215 } else if (underlineStyle != QTextCharFormat::NoUnderline) {
6216 QColor uc = charFormat.underlineColor();
6220 pen.setStyle((Qt::PenStyle)(underlineStyle));
6221 painter->setPen(pen);
6222 QLineF underline(line.x1(), underlinePos, line.x2(), underlinePos);
6224 textEngine->addUnderline(painter, underline);
6226 painter->drawLine(underline);
6229 pen.setStyle(Qt::SolidLine);
6230 pen.setColor(oldPen.color());
6232 if (flags & QTextItem::StrikeOut) {
6233 QLineF strikeOutLine = line;
6234 strikeOutLine.translate(0., - fe->ascent().toReal() / 3.);
6235 painter->setPen(pen);
6237 textEngine->addStrikeOut(painter, strikeOutLine);
6239 painter->drawLine(strikeOutLine);
6242 if (flags & QTextItem::Overline) {
6243 QLineF overline = line;
6244 overline.translate(0., - fe->ascent().toReal());
6245 painter->setPen(pen);
6247 textEngine->addOverline(painter, overline);
6249 painter->drawLine(overline);
6252 painter->setPen(oldPen);
6253 painter->setBrush(oldBrush);
6256 Q_GUI_EXPORT void qt_draw_decoration_for_glyphs(QPainter *painter, const glyph_t *glyphArray,
6257 const QFixedPoint *positions, int glyphCount,
6258 QFontEngine *fontEngine, const QFont &font,
6259 const QTextCharFormat &charFormat)
6261 if (!(font.underline() || font.strikeOut() || font.overline()))
6267 for (int i=0; i<glyphCount; ++i) {
6268 glyph_metrics_t gm = fontEngine->boundingBox(glyphArray[i]);
6269 if (i == 0 || leftMost > positions[i].x)
6270 leftMost = positions[i].x;
6272 // We don't support glyphs that do not share a common baseline. If this turns out to
6273 // be a relevant use case, then we need to find clusters of glyphs that share a baseline
6274 // and do a drawTextItemDecoration call per cluster.
6275 if (i == 0 || baseLine < positions[i].y)
6276 baseLine = positions[i].y;
6278 // We use the advance rather than the actual bounds to match the algorithm in drawText()
6279 if (i == 0 || rightMost < positions[i].x + gm.xoff)
6280 rightMost = positions[i].x + gm.xoff;
6283 QFixed width = rightMost - leftMost;
6284 QTextItem::RenderFlags flags = 0;
6286 if (font.underline())
6287 flags |= QTextItem::Underline;
6288 if (font.overline())
6289 flags |= QTextItem::Overline;
6290 if (font.strikeOut())
6291 flags |= QTextItem::StrikeOut;
6293 drawTextItemDecoration(painter, QPointF(leftMost.toReal(), baseLine.toReal()),
6296 font.underline() ? QTextCharFormat::SingleUnderline
6297 : QTextCharFormat::NoUnderline, flags,
6298 width.toReal(), charFormat);
6301 void QPainter::drawTextItem(const QPointF &p, const QTextItem &ti)
6305 d->drawTextItem(p, ti, static_cast<QTextEngine *>(0));
6308 void QPainterPrivate::drawTextItem(const QPointF &p, const QTextItem &_ti, QTextEngine *textEngine)
6310 #ifdef QT_DEBUG_DRAW
6311 if (qt_show_painter_debug_output)
6312 printf("QPainter::drawTextItem(), pos=[%.f,%.f], str='%s'\n",
6313 p.x(), p.y(), qPrintable(_ti.text()));
6322 qt_painter_thread_test(device->devType(),
6324 QFontDatabase::supportsThreadedFontRendering());
6327 QTextItemInt &ti = const_cast<QTextItemInt &>(static_cast<const QTextItemInt &>(_ti));
6329 if (!extended && state->bgMode == Qt::OpaqueMode) {
6330 QRectF rect(p.x(), p.y() - ti.ascent.toReal(), ti.width.toReal(), (ti.ascent + ti.descent + 1).toReal());
6331 q->fillRect(rect, state->bgBrush);
6334 if (q->pen().style() == Qt::NoPen)
6337 const QPainter::RenderHints oldRenderHints = state->renderHints;
6338 if (!state->renderHints & QPainter::Antialiasing && state->matrix.type() >= QTransform::TxScale) {
6339 // draw antialias decoration (underline/overline/strikeout) with
6343 const QTransform &m = state->matrix;
6344 if (state->matrix.type() < QTransform::TxShear) {
6345 bool isPlain90DegreeRotation =
6346 (qFuzzyIsNull(m.m11())
6347 && qFuzzyIsNull(m.m12() - qreal(1))
6348 && qFuzzyIsNull(m.m21() + qreal(1))
6349 && qFuzzyIsNull(m.m22())
6352 (qFuzzyIsNull(m.m11() + qreal(1))
6353 && qFuzzyIsNull(m.m12())
6354 && qFuzzyIsNull(m.m21())
6355 && qFuzzyIsNull(m.m22() + qreal(1))
6358 (qFuzzyIsNull(m.m11())
6359 && qFuzzyIsNull(m.m12() + qreal(1))
6360 && qFuzzyIsNull(m.m21() - qreal(1))
6361 && qFuzzyIsNull(m.m22())
6364 aa = !isPlain90DegreeRotation;
6367 q->setRenderHint(QPainter::Antialiasing, true);
6373 if (!ti.glyphs.numGlyphs) {
6375 } else if (ti.fontEngine->type() == QFontEngine::Multi) {
6376 QFontEngineMulti *multi = static_cast<QFontEngineMulti *>(ti.fontEngine);
6378 const QGlyphLayout &glyphs = ti.glyphs;
6379 int which = glyphs.glyphs[0] >> 24;
6384 bool rtl = ti.flags & QTextItem::RightToLeft;
6386 x += ti.width.toReal();
6390 for (end = 0; end < ti.glyphs.numGlyphs; ++end) {
6391 const int e = glyphs.glyphs[end] >> 24;
6396 QTextItemInt ti2 = ti.midItem(multi->engine(which), start, end - start);
6398 // set the high byte to zero and calc the width
6399 for (i = start; i < end; ++i) {
6400 glyphs.glyphs[i] = glyphs.glyphs[i] & 0xffffff;
6401 ti2.width += ti.glyphs.effectiveAdvance(i);
6405 x -= ti2.width.toReal();
6407 engine->drawTextItem(QPointF(x, y), ti2);
6410 x += ti2.width.toReal();
6412 // reset the high byte for all glyphs and advance to the next sub-string
6413 const int hi = which << 24;
6414 for (i = start; i < end; ++i) {
6415 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
6423 QTextItemInt ti2 = ti.midItem(multi->engine(which), start, end - start);
6425 // set the high byte to zero and calc the width
6426 for (i = start; i < end; ++i) {
6427 glyphs.glyphs[i] = glyphs.glyphs[i] & 0xffffff;
6428 ti2.width += ti.glyphs.effectiveAdvance(i);
6432 x -= ti2.width.toReal();
6435 extended->drawTextItem(QPointF(x, y), ti2);
6437 engine->drawTextItem(QPointF(x,y), ti2);
6439 // reset the high byte for all glyphs
6440 const int hi = which << 24;
6441 for (i = start; i < end; ++i)
6442 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
6446 extended->drawTextItem(p, ti);
6448 engine->drawTextItem(p, ti);
6450 drawTextItemDecoration(q, p, ti.fontEngine, textEngine, ti.underlineStyle,
6451 ti.flags, ti.width.toReal(), ti.charFormat);
6453 if (state->renderHints != oldRenderHints) {
6454 state->renderHints = oldRenderHints;
6456 extended->renderHintsChanged();
6458 state->dirtyFlags |= QPaintEngine::DirtyHints;
6463 \fn QRectF QPainter::boundingRect(const QRectF &rectangle, int flags, const QString &text)
6465 Returns the bounding rectangle of the \a text as it will appear
6466 when drawn inside the given \a rectangle with the specified \a
6467 flags using the currently set font(); i.e the function tells you
6468 where the drawText() function will draw when given the same
6471 If the \a text does not fit within the given \a rectangle using
6472 the specified \a flags, the function returns the required
6475 The \a flags argument is a bitwise OR of the following flags:
6479 \li Qt::AlignHCenter
6482 \li Qt::AlignVCenter
6484 \li Qt::TextSingleLine
6485 \li Qt::TextExpandTabs
6486 \li Qt::TextShowMnemonic
6487 \li Qt::TextWordWrap
6488 \li Qt::TextIncludeTrailingSpaces
6490 If several of the horizontal or several of the vertical alignment
6491 flags are set, the resulting alignment is undefined.
6493 \sa drawText(), Qt::Alignment, Qt::TextFlag
6497 \fn QRect QPainter::boundingRect(const QRect &rectangle, int flags,
6498 const QString &text)
6502 Returns the bounding rectangle of the \a text as it will appear
6503 when drawn inside the given \a rectangle with the specified \a
6504 flags using the currently set font().
6508 \fn QRect QPainter::boundingRect(int x, int y, int w, int h, int flags,
6509 const QString &text);
6513 Returns the bounding rectangle of the given \a text as it will
6514 appear when drawn inside the rectangle beginning at the point
6515 (\a{x}, \a{y}) with width \a w and height \a h.
6517 QRect QPainter::boundingRect(const QRect &rect, int flags, const QString &str)
6520 return QRect(rect.x(),rect.y(), 0,0);
6522 drawText(rect, flags | Qt::TextDontPrint, str, &brect);
6528 QRectF QPainter::boundingRect(const QRectF &rect, int flags, const QString &str)
6531 return QRectF(rect.x(),rect.y(), 0,0);
6533 drawText(rect, flags | Qt::TextDontPrint, str, &brect);
6538 \fn QRectF QPainter::boundingRect(const QRectF &rectangle,
6539 const QString &text, const QTextOption &option)
6543 Instead of specifying flags as a bitwise OR of the
6544 Qt::AlignmentFlag and Qt::TextFlag, this overloaded function takes
6545 an \a option argument. The QTextOption class provides a
6546 description of general rich text properties.
6550 QRectF QPainter::boundingRect(const QRectF &r, const QString &text, const QTextOption &o)
6554 if (!d->engine || text.length() == 0)
6555 return QRectF(r.x(),r.y(), 0,0);
6558 qt_format_text(d->state->font, r, Qt::TextDontPrint, &o, text, &br, 0, 0, 0, this);
6563 \fn void QPainter::drawTiledPixmap(const QRectF &rectangle, const QPixmap &pixmap, const QPointF &position)
6565 Draws a tiled \a pixmap, inside the given \a rectangle with its
6566 origin at the given \a position.
6568 Calling drawTiledPixmap() is similar to calling drawPixmap()
6569 several times to fill (tile) an area with a pixmap, but is
6570 potentially much more efficient depending on the underlying window
6575 void QPainter::drawTiledPixmap(const QRectF &r, const QPixmap &pixmap, const QPointF &sp)
6577 #ifdef QT_DEBUG_DRAW
6578 if (qt_show_painter_debug_output)
6579 printf("QPainter::drawTiledPixmap(), target=[%.2f,%.2f,%.2f,%.2f], pix=[%d,%d], offset=[%.2f,%.2f]\n",
6580 r.x(), r.y(), r.width(), r.height(),
6581 pixmap.width(), pixmap.height(),
6586 if (!d->engine || pixmap.isNull() || r.isEmpty())
6590 qt_painter_thread_test(d->device->devType(), "drawTiledPixmap()", true);
6593 qreal sw = pixmap.width();
6594 qreal sh = pixmap.height();
6598 sx = qRound(sw) - qRound(-sx) % qRound(sw);
6600 sx = qRound(sx) % qRound(sw);
6602 sy = qRound(sh) - -qRound(sy) % qRound(sh);
6604 sy = qRound(sy) % qRound(sh);
6608 d->extended->drawTiledPixmap(r, pixmap, QPointF(sx, sy));
6612 if (d->state->bgMode == Qt::OpaqueMode && pixmap.isQBitmap())
6613 fillRect(r, d->state->bgBrush);
6615 d->updateState(d->state);
6616 if ((d->state->matrix.type() > QTransform::TxTranslate
6617 && !d->engine->hasFeature(QPaintEngine::PixmapTransform))
6618 || (d->state->opacity != 1.0 && !d->engine->hasFeature(QPaintEngine::ConstantOpacity)))
6621 setBackgroundMode(Qt::TransparentMode);
6622 setRenderHint(Antialiasing, renderHints() & SmoothPixmapTransform);
6623 setBrush(QBrush(d->state->pen.color(), pixmap));
6626 // If there is no rotation involved we have to make sure we use the
6627 // antialiased and not the aliased coordinate system by rounding the coordinates.
6628 if (d->state->matrix.type() <= QTransform::TxScale) {
6629 const QPointF p = roundInDeviceCoordinates(r.topLeft(), d->state->matrix);
6631 if (d->state->matrix.type() <= QTransform::TxTranslate) {
6636 setBrushOrigin(QPointF(r.x()-sx, r.y()-sy));
6637 drawRect(QRectF(p, r.size()));
6639 setBrushOrigin(QPointF(r.x()-sx, r.y()-sy));
6648 if (d->state->matrix.type() == QTransform::TxTranslate
6649 && !d->engine->hasFeature(QPaintEngine::PixmapTransform)) {
6650 x += d->state->matrix.dx();
6651 y += d->state->matrix.dy();
6654 d->engine->drawTiledPixmap(QRectF(x, y, r.width(), r.height()), pixmap, QPointF(sx, sy));
6658 \fn QPainter::drawTiledPixmap(const QRect &rectangle, const QPixmap &pixmap,
6659 const QPoint &position = QPoint())
6662 Draws a tiled \a pixmap, inside the given \a rectangle with its
6663 origin at the given \a position.
6667 \fn void QPainter::drawTiledPixmap(int x, int y, int width, int height, const
6668 QPixmap &pixmap, int sx, int sy);
6671 Draws a tiled \a pixmap in the specified rectangle.
6673 (\a{x}, \a{y}) specifies the top-left point in the paint device
6674 that is to be drawn onto; with the given \a width and \a
6675 height. (\a{sx}, \a{sy}) specifies the top-left point in the \a
6676 pixmap that is to be drawn; this defaults to (0, 0).
6679 #ifndef QT_NO_PICTURE
6682 \fn void QPainter::drawPicture(const QPointF &point, const QPicture &picture)
6684 Replays the given \a picture at the given \a point.
6686 The QPicture class is a paint device that records and replays
6687 QPainter commands. A picture serializes the painter commands to an
6688 IO device in a platform-independent format. Everything that can be
6689 painted on a widget or pixmap can also be stored in a picture.
6691 This function does exactly the same as QPicture::play() when
6692 called with \a point = QPoint(0, 0).
6697 \snippet code/src_gui_painting_qpainter.cpp 18
6700 \sa QPicture::play()
6703 void QPainter::drawPicture(const QPointF &p, const QPicture &picture)
6711 d->updateState(d->state);
6715 const_cast<QPicture *>(&picture)->play(this);
6720 \fn void QPainter::drawPicture(const QPoint &point, const QPicture &picture)
6723 Replays the given \a picture at the given \a point.
6727 \fn void QPainter::drawPicture(int x, int y, const QPicture &picture)
6730 Draws the given \a picture at point (\a x, \a y).
6733 #endif // QT_NO_PICTURE
6736 \fn void QPainter::eraseRect(const QRectF &rectangle)
6738 Erases the area inside the given \a rectangle. Equivalent to
6740 \snippet code/src_gui_painting_qpainter.cpp 19
6744 void QPainter::eraseRect(const QRectF &r)
6748 fillRect(r, d->state->bgBrush);
6751 static inline bool needsResolving(const QBrush &brush)
6753 Qt::BrushStyle s = brush.style();
6754 return ((s == Qt::LinearGradientPattern || s == Qt::RadialGradientPattern ||
6755 s == Qt::ConicalGradientPattern) &&
6756 brush.gradient()->coordinateMode() == QGradient::ObjectBoundingMode);
6760 \fn void QPainter::eraseRect(const QRect &rectangle)
6763 Erases the area inside the given \a rectangle.
6767 \fn void QPainter::eraseRect(int x, int y, int width, int height)
6770 Erases the area inside the rectangle beginning at (\a x, \a y)
6771 with the given \a width and \a height.
6776 \fn void QPainter::fillRect(int x, int y, int width, int height, Qt::BrushStyle style)
6779 Fills the rectangle beginning at (\a{x}, \a{y}) with the given \a
6780 width and \a height, using the brush \a style specified.
6786 \fn void QPainter::fillRect(const QRect &rectangle, Qt::BrushStyle style)
6789 Fills the given \a rectangle with the brush \a style specified.
6795 \fn void QPainter::fillRect(const QRectF &rectangle, Qt::BrushStyle style)
6798 Fills the given \a rectangle with the brush \a style specified.
6804 \fn void QPainter::fillRect(const QRectF &rectangle, const QBrush &brush)
6806 Fills the given \a rectangle with the \a brush specified.
6808 Alternatively, you can specify a QColor instead of a QBrush; the
6809 QBrush constructor (taking a QColor argument) will automatically
6810 create a solid pattern brush.
6814 void QPainter::fillRect(const QRectF &r, const QBrush &brush)
6822 const QGradient *g = brush.gradient();
6823 if (!g || g->coordinateMode() == QGradient::LogicalMode) {
6824 d->extended->fillRect(r, brush);
6829 QPen oldPen = pen();
6830 QBrush oldBrush = this->brush();
6832 if (brush.style() == Qt::SolidPattern) {
6833 d->colorBrush.setStyle(Qt::SolidPattern);
6834 d->colorBrush.setColor(brush.color());
6835 setBrush(d->colorBrush);
6846 \fn void QPainter::fillRect(const QRect &rectangle, const QBrush &brush)
6849 Fills the given \a rectangle with the specified \a brush.
6852 void QPainter::fillRect(const QRect &r, const QBrush &brush)
6860 const QGradient *g = brush.gradient();
6861 if (!g || g->coordinateMode() == QGradient::LogicalMode) {
6862 d->extended->fillRect(r, brush);
6867 QPen oldPen = pen();
6868 QBrush oldBrush = this->brush();
6870 if (brush.style() == Qt::SolidPattern) {
6871 d->colorBrush.setStyle(Qt::SolidPattern);
6872 d->colorBrush.setColor(brush.color());
6873 setBrush(d->colorBrush);
6886 \fn void QPainter::fillRect(const QRect &rectangle, const QColor &color)
6889 Fills the given \a rectangle with the \a color specified.
6893 void QPainter::fillRect(const QRect &r, const QColor &color)
6901 d->extended->fillRect(r, color);
6905 fillRect(r, QBrush(color));
6910 \fn void QPainter::fillRect(const QRectF &rectangle, const QColor &color)
6913 Fills the given \a rectangle with the \a color specified.
6917 void QPainter::fillRect(const QRectF &r, const QColor &color)
6925 d->extended->fillRect(r, color);
6929 fillRect(r, QBrush(color));
6933 \fn void QPainter::fillRect(int x, int y, int width, int height, const QBrush &brush)
6937 Fills the rectangle beginning at (\a{x}, \a{y}) with the given \a
6938 width and \a height, using the given \a brush.
6942 \fn void QPainter::fillRect(int x, int y, int width, int height, const QColor &color)
6946 Fills the rectangle beginning at (\a{x}, \a{y}) with the given \a
6947 width and \a height, using the given \a color.
6953 \fn void QPainter::fillRect(int x, int y, int width, int height, Qt::GlobalColor color)
6957 Fills the rectangle beginning at (\a{x}, \a{y}) with the given \a
6958 width and \a height, using the given \a color.
6964 \fn void QPainter::fillRect(const QRect &rectangle, Qt::GlobalColor color);
6968 Fills the given \a rectangle with the specified \a color.
6974 \fn void QPainter::fillRect(const QRectF &rectangle, Qt::GlobalColor color);
6978 Fills the given \a rectangle with the specified \a color.
6984 Sets the given render \a hint on the painter if \a on is true;
6985 otherwise clears the render hint.
6987 \sa setRenderHints(), renderHints(), {QPainter#Rendering
6988 Quality}{Rendering Quality}
6990 void QPainter::setRenderHint(RenderHint hint, bool on)
6992 #ifdef QT_DEBUG_DRAW
6993 if (qt_show_painter_debug_output)
6994 printf("QPainter::setRenderHint: hint=%x, %s\n", hint, on ? "on" : "off");
6998 static const bool antialiasingDisabled = qgetenv("QT_NO_ANTIALIASING").toInt();
6999 if (hint == QPainter::Antialiasing && antialiasingDisabled)
7003 setRenderHints(hint, on);
7009 Sets the given render \a hints on the painter if \a on is true;
7010 otherwise clears the render hints.
7012 \sa setRenderHint(), renderHints(), {QPainter#Rendering
7013 Quality}{Rendering Quality}
7016 void QPainter::setRenderHints(RenderHints hints, bool on)
7021 qWarning("QPainter::setRenderHint: Painter must be active to set rendering hints");
7026 d->state->renderHints |= hints;
7028 d->state->renderHints &= ~hints;
7031 d->extended->renderHintsChanged();
7033 d->state->dirtyFlags |= QPaintEngine::DirtyHints;
7037 Returns a flag that specifies the rendering hints that are set for
7040 \sa testRenderHint(), {QPainter#Rendering Quality}{Rendering Quality}
7042 QPainter::RenderHints QPainter::renderHints() const
7044 Q_D(const QPainter);
7049 return d->state->renderHints;
7053 \fn bool QPainter::testRenderHint(RenderHint hint) const
7056 Returns true if \a hint is set; otherwise returns false.
7058 \sa renderHints(), setRenderHint()
7062 Returns true if view transformation is enabled; otherwise returns
7065 \sa setViewTransformEnabled(), worldTransform()
7068 bool QPainter::viewTransformEnabled() const
7070 Q_D(const QPainter);
7072 qWarning("QPainter::viewTransformEnabled: Painter not active");
7075 return d->state->VxF;
7079 \fn void QPainter::setWindow(const QRect &rectangle)
7081 Sets the painter's window to the given \a rectangle, and enables
7082 view transformations.
7084 The window rectangle is part of the view transformation. The
7085 window specifies the logical coordinate system. Its sister, the
7086 viewport(), specifies the device coordinate system.
7088 The default window rectangle is the same as the device's
7091 \sa window(), viewTransformEnabled(), {Coordinate
7092 System#Window-Viewport Conversion}{Window-Viewport Conversion}
7096 \fn void QPainter::setWindow(int x, int y, int width, int height)
7099 Sets the painter's window to the rectangle beginning at (\a x, \a
7100 y) and the given \a width and \a height.
7103 void QPainter::setWindow(const QRect &r)
7105 #ifdef QT_DEBUG_DRAW
7106 if (qt_show_painter_debug_output)
7107 printf("QPainter::setWindow(), [%d,%d,%d,%d]\n", r.x(), r.y(), r.width(), r.height());
7113 qWarning("QPainter::setWindow: Painter not active");
7117 d->state->wx = r.x();
7118 d->state->wy = r.y();
7119 d->state->ww = r.width();
7120 d->state->wh = r.height();
7122 d->state->VxF = true;
7127 Returns the window rectangle.
7129 \sa setWindow(), setViewTransformEnabled()
7132 QRect QPainter::window() const
7134 Q_D(const QPainter);
7136 qWarning("QPainter::window: Painter not active");
7139 return QRect(d->state->wx, d->state->wy, d->state->ww, d->state->wh);
7143 \fn void QPainter::setViewport(const QRect &rectangle)
7145 Sets the painter's viewport rectangle to the given \a rectangle,
7146 and enables view transformations.
7148 The viewport rectangle is part of the view transformation. The
7149 viewport specifies the device coordinate system. Its sister, the
7150 window(), specifies the logical coordinate system.
7152 The default viewport rectangle is the same as the device's
7155 \sa viewport(), viewTransformEnabled(), {Coordinate
7156 System#Window-Viewport Conversion}{Window-Viewport Conversion}
7160 \fn void QPainter::setViewport(int x, int y, int width, int height)
7163 Sets the painter's viewport rectangle to be the rectangle
7164 beginning at (\a x, \a y) with the given \a width and \a height.
7167 void QPainter::setViewport(const QRect &r)
7169 #ifdef QT_DEBUG_DRAW
7170 if (qt_show_painter_debug_output)
7171 printf("QPainter::setViewport(), [%d,%d,%d,%d]\n", r.x(), r.y(), r.width(), r.height());
7177 qWarning("QPainter::setViewport: Painter not active");
7181 d->state->vx = r.x();
7182 d->state->vy = r.y();
7183 d->state->vw = r.width();
7184 d->state->vh = r.height();
7186 d->state->VxF = true;
7191 Returns the viewport rectangle.
7193 \sa setViewport(), setViewTransformEnabled()
7196 QRect QPainter::viewport() const
7198 Q_D(const QPainter);
7200 qWarning("QPainter::viewport: Painter not active");
7203 return QRect(d->state->vx, d->state->vy, d->state->vw, d->state->vh);
7207 Enables view transformations if \a enable is true, or disables
7208 view transformations if \a enable is false.
7210 \sa viewTransformEnabled(), {Coordinate System#Window-Viewport
7211 Conversion}{Window-Viewport Conversion}
7214 void QPainter::setViewTransformEnabled(bool enable)
7216 #ifdef QT_DEBUG_DRAW
7217 if (qt_show_painter_debug_output)
7218 printf("QPainter::setViewTransformEnabled(), enable=%d\n", enable);
7224 qWarning("QPainter::setViewTransformEnabled: Painter not active");
7228 if (enable == d->state->VxF)
7231 d->state->VxF = enable;
7240 Please use QWidget::render() instead.
7242 Redirects all paint commands for the given paint \a device, to the
7243 \a replacement device. The optional point \a offset defines an
7244 offset within the source device.
7246 The redirection will not be effective until the begin() function
7247 has been called; make sure to call end() for the given \a
7248 device's painter (if any) before redirecting. Call
7249 restoreRedirected() to restore the previous redirection.
7251 \warning Making use of redirections in the QPainter API implies
7252 that QPainter::begin() and QPaintDevice destructors need to hold
7253 a mutex for a short period. This can impact performance. Use of
7254 QWidget::render is strongly encouraged.
7256 \sa redirected(), restoreRedirected()
7258 void QPainter::setRedirected(const QPaintDevice *device,
7259 QPaintDevice *replacement,
7260 const QPoint &offset)
7262 Q_ASSERT(device != 0);
7264 Q_UNUSED(replacement)
7266 qWarning("QPainter::setRedirected(): ignoring call to deprecated function, use QWidget::render() instead");
7274 Using QWidget::render() obsoletes the use of this function.
7276 Restores the previous redirection for the given \a device after a
7277 call to setRedirected().
7279 \warning Making use of redirections in the QPainter API implies
7280 that QPainter::begin() and QPaintDevice destructors need to hold
7281 a mutex for a short period. This can impact performance. Use of
7282 QWidget::render is strongly encouraged.
7286 void QPainter::restoreRedirected(const QPaintDevice *device)
7289 qWarning("QPainter::restoreRedirected(): ignoring call to deprecated function, use QWidget::render() instead");
7297 Using QWidget::render() obsoletes the use of this function.
7299 Returns the replacement for given \a device. The optional out
7300 parameter \a offset returns the offset within the replaced device.
7302 \warning Making use of redirections in the QPainter API implies
7303 that QPainter::begin() and QPaintDevice destructors need to hold
7304 a mutex for a short period. This can impact performance. Use of
7305 QWidget::render is strongly encouraged.
7307 \sa setRedirected(), restoreRedirected()
7309 QPaintDevice *QPainter::redirected(const QPaintDevice *device, QPoint *offset)
7316 void qt_format_text(const QFont &fnt, const QRectF &_r,
7317 int tf, const QString& str, QRectF *brect,
7318 int tabstops, int *ta, int tabarraylen,
7321 qt_format_text(fnt, _r,
7323 tabstops, ta, tabarraylen,
7326 void qt_format_text(const QFont &fnt, const QRectF &_r,
7327 int tf, const QTextOption *option, const QString& str, QRectF *brect,
7328 int tabstops, int *ta, int tabarraylen,
7332 Q_ASSERT( !((tf & ~Qt::TextDontPrint)!=0 && option!=0) ); // we either have an option or flags
7335 tf |= option->alignment();
7336 if (option->wrapMode() != QTextOption::NoWrap)
7337 tf |= Qt::TextWordWrap;
7339 if (option->flags() & QTextOption::IncludeTrailingSpaces)
7340 tf |= Qt::TextIncludeTrailingSpaces;
7342 if (option->tabStop() >= 0 || !option->tabArray().isEmpty())
7343 tf |= Qt::TextExpandTabs;
7346 // we need to copy r here to protect against the case (&r == brect).
7349 bool dontclip = (tf & Qt::TextDontClip);
7350 bool wordwrap = (tf & Qt::TextWordWrap) || (tf & Qt::TextWrapAnywhere);
7351 bool singleline = (tf & Qt::TextSingleLine);
7352 bool showmnemonic = (tf & Qt::TextShowMnemonic);
7353 bool hidemnmemonic = (tf & Qt::TextHideMnemonic);
7355 Qt::LayoutDirection layout_direction;
7356 if (tf & Qt::TextForceLeftToRight)
7357 layout_direction = Qt::LeftToRight;
7358 else if (tf & Qt::TextForceRightToLeft)
7359 layout_direction = Qt::RightToLeft;
7361 layout_direction = option->textDirection();
7363 layout_direction = painter->layoutDirection();
7365 layout_direction = Qt::LeftToRight;
7367 tf = QGuiApplicationPrivate::visualAlignment(layout_direction, QFlag(tf));
7369 bool isRightToLeft = layout_direction == Qt::RightToLeft;
7370 bool expandtabs = ((tf & Qt::TextExpandTabs) &&
7371 (((tf & Qt::AlignLeft) && !isRightToLeft) ||
7372 ((tf & Qt::AlignRight) && isRightToLeft)));
7375 tf |= Qt::TextDontPrint;
7377 uint maxUnderlines = 0;
7378 int numUnderlines = 0;
7379 QVarLengthArray<int, 32> underlinePositions(1);
7381 QFontMetricsF fm(fnt);
7384 start_lengthVariant:
7385 bool hasMoreLengthVariants = false;
7386 // compatible behaviour to the old implementation. Replace
7388 int old_offset = offset;
7389 for (; offset < text.length(); offset++) {
7390 QChar chr = text.at(offset);
7391 if (chr == QLatin1Char('\r') || (singleline && chr == QLatin1Char('\n'))) {
7392 text[offset] = QLatin1Char(' ');
7393 } else if (chr == QLatin1Char('\n')) {
7394 text[offset] = QChar::LineSeparator;
7395 } else if (chr == QLatin1Char('&')) {
7397 } else if (chr == QLatin1Char('\t')) {
7399 text[offset] = QLatin1Char(' ');
7400 } else if (!tabarraylen && !tabstops) {
7401 tabstops = qRound(fm.width(QLatin1Char('x'))*8);
7403 } else if (chr == QChar(ushort(0x9c))) {
7404 // string with multiple length variants
7405 hasMoreLengthVariants = true;
7410 int length = offset - old_offset;
7411 if ((hidemnmemonic || showmnemonic) && maxUnderlines > 0) {
7412 underlinePositions.resize(maxUnderlines + 1);
7414 QChar *cout = text.data() + old_offset;
7418 if (*cin == QLatin1Char('&')) {
7424 if (*cin != QLatin1Char('&') && !hidemnmemonic)
7425 underlinePositions[numUnderlines++] = cout - text.data() - old_offset;
7434 // no need to do extra work for underlines if we don't paint
7435 if (tf & Qt::TextDontPrint)
7438 underlinePositions[numUnderlines] = -1;
7442 QString finalText = text.mid(old_offset, length);
7443 QStackTextEngine engine(finalText, fnt);
7445 engine.option = *option;
7448 if (engine.option.tabStop() < 0 && tabstops > 0)
7449 engine.option.setTabStop(tabstops);
7451 if (engine.option.tabs().isEmpty() && ta) {
7453 for (int i = 0; i < tabarraylen; i++)
7454 tabs.append(qreal(ta[i]));
7455 engine.option.setTabArray(tabs);
7458 engine.option.setTextDirection(layout_direction);
7459 if (tf & Qt::AlignJustify)
7460 engine.option.setAlignment(Qt::AlignJustify);
7462 engine.option.setAlignment(Qt::AlignLeft); // do not do alignment twice
7464 if (!option && (tf & Qt::TextWrapAnywhere))
7465 engine.option.setWrapMode(QTextOption::WrapAnywhere);
7467 if (tf & Qt::TextJustificationForced)
7468 engine.forceJustification = true;
7469 QTextLayout textLayout(&engine);
7470 textLayout.setCacheEnabled(true);
7471 textLayout.engine()->underlinePositions = underlinePositions.data();
7473 if (finalText.isEmpty()) {
7474 height = fm.height();
7476 tf |= Qt::TextDontPrint;
7478 qreal lineWidth = 0x01000000;
7479 if (wordwrap || (tf & Qt::TextJustificationForced))
7480 lineWidth = qMax<qreal>(0, r.width());
7482 tf |= Qt::TextIncludeTrailingSpaces;
7483 textLayout.engine()->ignoreBidi = bool(tf & Qt::TextDontPrint);
7484 textLayout.beginLayout();
7486 qreal leading = fm.leading();
7490 QTextLine l = textLayout.createLine();
7494 l.setLineWidth(lineWidth);
7496 l.setPosition(QPointF(0., height));
7497 height += l.height();
7498 width = qMax(width, l.naturalTextWidth());
7499 if (!dontclip && !brect && height >= r.height())
7502 textLayout.endLayout();
7507 if (tf & Qt::AlignBottom) {
7508 yoff = r.height() - height;
7509 } else if (tf & Qt::AlignVCenter) {
7510 yoff = (r.height() - height)/2;
7512 QTransform::TransformationType type = painter->transform().type();
7513 if (type <= QTransform::TxScale) {
7514 // do the rounding manually to work around inconsistencies
7515 // in the paint engines when drawing on floating point offsets
7516 const qreal scale = painter->transform().m22();
7518 yoff = -qRound(-yoff * scale) / scale;
7522 if (tf & Qt::AlignRight) {
7523 xoff = r.width() - width;
7524 } else if (tf & Qt::AlignHCenter) {
7525 xoff = (r.width() - width)/2;
7527 QTransform::TransformationType type = painter->transform().type();
7528 if (type <= QTransform::TxScale) {
7529 // do the rounding manually to work around inconsistencies
7530 // in the paint engines when drawing on floating point offsets
7531 const qreal scale = painter->transform().m11();
7533 xoff = qRound(xoff * scale) / scale;
7537 QRectF bounds = QRectF(r.x() + xoff, r.y() + yoff, width, height);
7539 if (hasMoreLengthVariants && !(tf & Qt::TextLongestVariant) && !r.contains(bounds)) {
7541 goto start_lengthVariant;
7546 if (!(tf & Qt::TextDontPrint)) {
7547 bool restore = false;
7548 if (!dontclip && !r.contains(bounds)) {
7551 painter->setClipRect(r, Qt::IntersectClip);
7554 for (int i = 0; i < textLayout.lineCount(); i++) {
7555 QTextLine line = textLayout.lineAt(i);
7556 QTextEngine *eng = textLayout.engine();
7557 eng->enableDelayDecorations();
7559 qreal advance = line.horizontalAdvance();
7561 if (tf & Qt::AlignRight) {
7562 xoff = r.width() - advance -
7563 eng->leadingSpaceWidth(eng->lines[line.lineNumber()]).toReal();
7565 else if (tf & Qt::AlignHCenter)
7566 xoff = (r.width() - advance) / 2;
7568 line.draw(painter, QPointF(r.x() + xoff, r.y() + yoff));
7569 eng->drawDecorations(painter);
7579 Sets the layout direction used by the painter when drawing text,
7580 to the specified \a direction.
7582 The default is Qt::LayoutDirectionAuto, which will implicitly determine the
7583 direction from the text drawn.
7585 \sa QTextOption::setTextDirection(), layoutDirection(), drawText(), {QPainter#Settings}{Settings}
7587 void QPainter::setLayoutDirection(Qt::LayoutDirection direction)
7591 d->state->layoutDirection = direction;
7595 Returns the layout direction used by the painter when drawing text.
7597 \sa QTextOption::textDirection(), setLayoutDirection(), drawText(), {QPainter#Settings}{Settings}
7599 Qt::LayoutDirection QPainter::layoutDirection() const
7601 Q_D(const QPainter);
7602 return d->state ? d->state->layoutDirection : Qt::LayoutDirectionAuto;
7605 QPainterState::QPainterState(const QPainterState *s)
7606 : brushOrigin(s->brushOrigin), font(s->font), deviceFont(s->deviceFont),
7607 pen(s->pen), brush(s->brush), bgBrush(s->bgBrush),
7608 clipRegion(s->clipRegion), clipPath(s->clipPath),
7609 clipOperation(s->clipOperation),
7610 renderHints(s->renderHints), clipInfo(s->clipInfo),
7611 worldMatrix(s->worldMatrix), matrix(s->matrix), redirectionMatrix(s->redirectionMatrix),
7612 wx(s->wx), wy(s->wy), ww(s->ww), wh(s->wh),
7613 vx(s->vx), vy(s->vy), vw(s->vw), vh(s->vh),
7614 opacity(s->opacity), WxF(s->WxF), VxF(s->VxF),
7615 clipEnabled(s->clipEnabled), bgMode(s->bgMode), painter(s->painter),
7616 layoutDirection(s->layoutDirection),
7617 composition_mode(s->composition_mode),
7618 emulationSpecifier(s->emulationSpecifier), changeFlags(0)
7620 dirtyFlags = s->dirtyFlags;
7623 QPainterState::QPainterState()
7624 : brushOrigin(0, 0), bgBrush(Qt::white), clipOperation(Qt::NoClip),
7626 wx(0), wy(0), ww(0), wh(0), vx(0), vy(0), vw(0), vh(0),
7627 opacity(1), WxF(false), VxF(false), clipEnabled(true),
7628 bgMode(Qt::TransparentMode), painter(0),
7629 layoutDirection(QGuiApplication::layoutDirection()),
7630 composition_mode(QPainter::CompositionMode_SourceOver),
7631 emulationSpecifier(0), changeFlags(0)
7636 QPainterState::~QPainterState()
7640 void QPainterState::init(QPainter *p) {
7641 bgBrush = Qt::white;
7642 bgMode = Qt::TransparentMode;
7646 wx = wy = ww = wh = 0;
7647 vx = vy = vw = vh = 0;
7650 brushOrigin = QPointF(0, 0);
7652 font = deviceFont = QFont();
7653 clipRegion = QRegion();
7654 clipPath = QPainterPath();
7655 clipOperation = Qt::NoClip;
7657 worldMatrix.reset();
7659 layoutDirection = QGuiApplication::layoutDirection();
7660 composition_mode = QPainter::CompositionMode_SourceOver;
7661 emulationSpecifier = 0;
7669 \fn void QPainter::drawImage(const QRectF &target, const QImage &image, const QRectF &source,
7670 Qt::ImageConversionFlags flags)
7672 Draws the rectangular portion \a source of the given \a image
7673 into the \a target rectangle in the paint device.
7675 \note The image is scaled to fit the rectangle, if both the image and rectangle size disagree.
7677 If the image needs to be modified to fit in a lower-resolution
7678 result (e.g. converting from 32-bit to 8-bit), use the \a flags to
7679 specify how you would prefer this to happen.
7684 \snippet code/src_gui_painting_qpainter.cpp 20
7691 \fn void QPainter::drawImage(const QRect &target, const QImage &image, const QRect &source,
7692 Qt::ImageConversionFlags flags)
7695 Draws the rectangular portion \a source of the given \a image
7696 into the \a target rectangle in the paint device.
7698 \note The image is scaled to fit the rectangle, if both the image and rectangle size disagree.
7702 \fn void QPainter::drawImage(const QPointF &point, const QImage &image)
7706 Draws the given \a image at the given \a point.
7710 \fn void QPainter::drawImage(const QPoint &point, const QImage &image)
7714 Draws the given \a image at the given \a point.
7718 \fn void QPainter::drawImage(const QPointF &point, const QImage &image, const QRectF &source,
7719 Qt::ImageConversionFlags flags = 0)
7723 Draws the rectangular portion \a source of the given \a image with
7724 its origin at the given \a point.
7728 \fn void QPainter::drawImage(const QPoint &point, const QImage &image, const QRect &source,
7729 Qt::ImageConversionFlags flags = 0)
7732 Draws the rectangular portion \a source of the given \a image with
7733 its origin at the given \a point.
7737 \fn void QPainter::drawImage(const QRectF &rectangle, const QImage &image)
7741 Draws the given \a image into the given \a rectangle.
7743 \note The image is scaled to fit the rectangle, if both the image and rectangle size disagree.
7747 \fn void QPainter::drawImage(const QRect &rectangle, const QImage &image)
7751 Draws the given \a image into the given \a rectangle.
7753 \note The image is scaled to fit the rectangle, if both the image and rectangle size disagree.
7757 \fn void QPainter::drawImage(int x, int y, const QImage &image,
7758 int sx, int sy, int sw, int sh,
7759 Qt::ImageConversionFlags flags)
7762 Draws an image at (\a{x}, \a{y}) by copying a part of \a image into
7765 (\a{x}, \a{y}) specifies the top-left point in the paint device that is
7766 to be drawn onto. (\a{sx}, \a{sy}) specifies the top-left point in \a
7767 image that is to be drawn. The default is (0, 0).
7769 (\a{sw}, \a{sh}) specifies the size of the image that is to be drawn.
7770 The default, (0, 0) (and negative) means all the way to the
7771 bottom-right of the image.
7775 \class QPaintEngineState
7779 \brief The QPaintEngineState class provides information about the
7780 active paint engine's current state.
7783 QPaintEngineState records which properties that have changed since
7784 the last time the paint engine was updated, as well as their
7787 Which properties that have changed can at any time be retrieved
7788 using the state() function. This function returns an instance of
7789 the QPaintEngine::DirtyFlags type which stores an OR combination
7790 of QPaintEngine::DirtyFlag values. The QPaintEngine::DirtyFlag
7791 enum defines whether a property has changed since the last update
7794 If a property is marked with a dirty flag, its current value can
7795 be retrieved using the corresponding get function:
7800 \header \li Property Flag \li Current Property Value
7801 \row \li QPaintEngine::DirtyBackground \li backgroundBrush()
7802 \row \li QPaintEngine::DirtyBackgroundMode \li backgroundMode()
7803 \row \li QPaintEngine::DirtyBrush \li brush()
7804 \row \li QPaintEngine::DirtyBrushOrigin \li brushOrigin()
7805 \row \li QPaintEngine::DirtyClipRegion \e or QPaintEngine::DirtyClipPath
7807 \row \li QPaintEngine::DirtyClipPath \li clipPath()
7808 \row \li QPaintEngine::DirtyClipRegion \li clipRegion()
7809 \row \li QPaintEngine::DirtyCompositionMode \li compositionMode()
7810 \row \li QPaintEngine::DirtyFont \li font()
7811 \row \li QPaintEngine::DirtyTransform \li transform()
7812 \row \li QPaintEngine::DirtyClipEnabled \li isClipEnabled()
7813 \row \li QPaintEngine::DirtyPen \li pen()
7814 \row \li QPaintEngine::DirtyHints \li renderHints()
7817 The QPaintEngineState class also provide the painter() function
7818 which returns a pointer to the painter that is currently updating
7821 An instance of this class, representing the current state of the
7822 active paint engine, is passed as argument to the
7823 QPaintEngine::updateState() function. The only situation in which
7824 you will have to use this class directly is when implementing your
7832 \fn QPaintEngine::DirtyFlags QPaintEngineState::state() const
7834 Returns a combination of flags identifying the set of properties
7835 that need to be updated when updating the paint engine's state
7836 (i.e. during a call to the QPaintEngine::updateState() function).
7838 \sa QPaintEngine::updateState()
7843 Returns the pen in the current paint engine state.
7845 This variable should only be used when the state() returns a
7846 combination which includes the QPaintEngine::DirtyPen flag.
7848 \sa state(), QPaintEngine::updateState()
7851 QPen QPaintEngineState::pen() const
7853 return static_cast<const QPainterState *>(this)->pen;
7857 Returns the brush in the current paint engine state.
7859 This variable should only be used when the state() returns a
7860 combination which includes the QPaintEngine::DirtyBrush flag.
7862 \sa state(), QPaintEngine::updateState()
7865 QBrush QPaintEngineState::brush() const
7867 return static_cast<const QPainterState *>(this)->brush;
7871 Returns the brush origin in the current paint engine state.
7873 This variable should only be used when the state() returns a
7874 combination which includes the QPaintEngine::DirtyBrushOrigin flag.
7876 \sa state(), QPaintEngine::updateState()
7879 QPointF QPaintEngineState::brushOrigin() const
7881 return static_cast<const QPainterState *>(this)->brushOrigin;
7885 Returns the background brush in the current paint engine state.
7887 This variable should only be used when the state() returns a
7888 combination which includes the QPaintEngine::DirtyBackground flag.
7890 \sa state(), QPaintEngine::updateState()
7893 QBrush QPaintEngineState::backgroundBrush() const
7895 return static_cast<const QPainterState *>(this)->bgBrush;
7899 Returns the background mode in the current paint engine
7902 This variable should only be used when the state() returns a
7903 combination which includes the QPaintEngine::DirtyBackgroundMode flag.
7905 \sa state(), QPaintEngine::updateState()
7908 Qt::BGMode QPaintEngineState::backgroundMode() const
7910 return static_cast<const QPainterState *>(this)->bgMode;
7914 Returns the font in the current paint engine
7917 This variable should only be used when the state() returns a
7918 combination which includes the QPaintEngine::DirtyFont flag.
7920 \sa state(), QPaintEngine::updateState()
7923 QFont QPaintEngineState::font() const
7925 return static_cast<const QPainterState *>(this)->font;
7932 Returns the matrix in the current paint engine
7935 \note It is advisable to use transform() instead of this function to
7936 preserve the properties of perspective transformations.
7938 This variable should only be used when the state() returns a
7939 combination which includes the QPaintEngine::DirtyTransform flag.
7941 \sa state(), QPaintEngine::updateState()
7944 QMatrix QPaintEngineState::matrix() const
7946 const QPainterState *st = static_cast<const QPainterState *>(this);
7948 return st->matrix.toAffine();
7954 Returns the matrix in the current paint engine state.
7956 This variable should only be used when the state() returns a
7957 combination which includes the QPaintEngine::DirtyTransform flag.
7959 \sa state(), QPaintEngine::updateState()
7963 QTransform QPaintEngineState::transform() const
7965 const QPainterState *st = static_cast<const QPainterState *>(this);
7972 Returns the clip operation in the current paint engine
7975 This variable should only be used when the state() returns a
7976 combination which includes either the QPaintEngine::DirtyClipPath
7977 or the QPaintEngine::DirtyClipRegion flag.
7979 \sa state(), QPaintEngine::updateState()
7982 Qt::ClipOperation QPaintEngineState::clipOperation() const
7984 return static_cast<const QPainterState *>(this)->clipOperation;
7990 Returns whether the coordinate of the fill 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::brushNeedsResolving() const
7996 const QBrush &brush = static_cast<const QPainterState *>(this)->brush;
7997 return needsResolving(brush);
8004 Returns whether the coordinate of the stroke have been specified
8005 as bounded by the current rendering operation and have to be
8006 resolved (about the currently rendered primitive).
8008 bool QPaintEngineState::penNeedsResolving() const
8010 const QPen &pen = static_cast<const QPainterState *>(this)->pen;
8011 return needsResolving(pen.brush());
8015 Returns the clip region in the current paint engine state.
8017 This variable should only be used when the state() returns a
8018 combination which includes the QPaintEngine::DirtyClipRegion flag.
8020 \sa state(), QPaintEngine::updateState()
8023 QRegion QPaintEngineState::clipRegion() const
8025 return static_cast<const QPainterState *>(this)->clipRegion;
8029 Returns the clip path in the current paint engine state.
8031 This variable should only be used when the state() returns a
8032 combination which includes the QPaintEngine::DirtyClipPath flag.
8034 \sa state(), QPaintEngine::updateState()
8037 QPainterPath QPaintEngineState::clipPath() const
8039 return static_cast<const QPainterState *>(this)->clipPath;
8043 Returns whether clipping is enabled or not in the current paint
8046 This variable should only be used when the state() returns a
8047 combination which includes the QPaintEngine::DirtyClipEnabled
8050 \sa state(), QPaintEngine::updateState()
8053 bool QPaintEngineState::isClipEnabled() const
8055 return static_cast<const QPainterState *>(this)->clipEnabled;
8059 Returns the render hints in the current paint engine state.
8061 This variable should only be used when the state() returns a
8062 combination which includes the QPaintEngine::DirtyHints
8065 \sa state(), QPaintEngine::updateState()
8068 QPainter::RenderHints QPaintEngineState::renderHints() const
8070 return static_cast<const QPainterState *>(this)->renderHints;
8074 Returns the composition mode in the current paint engine state.
8076 This variable should only be used when the state() returns a
8077 combination which includes the QPaintEngine::DirtyCompositionMode
8080 \sa state(), QPaintEngine::updateState()
8083 QPainter::CompositionMode QPaintEngineState::compositionMode() const
8085 return static_cast<const QPainterState *>(this)->composition_mode;
8090 Returns a pointer to the painter currently updating the paint
8094 QPainter *QPaintEngineState::painter() const
8096 return static_cast<const QPainterState *>(this)->painter;
8103 Returns the opacity in the current paint engine state.
8106 qreal QPaintEngineState::opacity() const
8108 return static_cast<const QPainterState *>(this)->opacity;
8114 Sets the world transformation matrix.
8115 If \a combine is true, the specified \a transform is combined with
8116 the current matrix; otherwise it replaces the current matrix.
8118 \sa transform(), setWorldTransform()
8121 void QPainter::setTransform(const QTransform &transform, bool combine )
8123 setWorldTransform(transform, combine);
8127 Returns the world transformation matrix.
8129 \sa worldTransform()
8132 const QTransform & QPainter::transform() const
8134 return worldTransform();
8139 Returns the matrix that transforms from logical coordinates to
8140 device coordinates of the platform dependent paint device.
8142 This function is \e only needed when using platform painting
8143 commands on the platform dependent handle (Qt::HANDLE), and the
8144 platform does not do transformations nativly.
8146 The QPaintEngine::PaintEngineFeature enum can be queried to
8147 determine whether the platform performs the transformations or
8150 \sa worldTransform(), QPaintEngine::hasFeature(),
8153 const QTransform & QPainter::deviceTransform() const
8155 Q_D(const QPainter);
8157 qWarning("QPainter::deviceTransform: Painter not active");
8158 return d->fakeState()->transform;
8160 return d->state->matrix;
8165 Resets any transformations that were made using translate(),
8166 scale(), shear(), rotate(), setWorldTransform(), setViewport()
8169 \sa {Coordinate Transformations}
8172 void QPainter::resetTransform()
8175 #ifdef QT_DEBUG_DRAW
8176 if (qt_show_painter_debug_output)
8177 printf("QPainter::resetMatrix()\n");
8180 qWarning("QPainter::resetMatrix: Painter not active");
8184 d->state->wx = d->state->wy = d->state->vx = d->state->vy = 0; // default view origins
8185 d->state->ww = d->state->vw = d->device->metric(QPaintDevice::PdmWidth);
8186 d->state->wh = d->state->vh = d->device->metric(QPaintDevice::PdmHeight);
8187 d->state->worldMatrix = QTransform();
8188 setMatrixEnabled(false);
8189 setViewTransformEnabled(false);
8191 d->extended->transformChanged();
8193 d->state->dirtyFlags |= QPaintEngine::DirtyTransform;
8197 Sets the world transformation matrix.
8198 If \a combine is true, the specified \a matrix is combined with the current matrix;
8199 otherwise it replaces the current matrix.
8201 \sa transform(), setTransform()
8204 void QPainter::setWorldTransform(const QTransform &matrix, bool combine )
8209 qWarning("QPainter::setWorldTransform: Painter not active");
8214 d->state->worldMatrix = matrix * d->state->worldMatrix; // combines
8216 d->state->worldMatrix = matrix; // set new matrix
8218 d->state->WxF = true;
8223 Returns the world transformation matrix.
8226 const QTransform & QPainter::worldTransform() const
8228 Q_D(const QPainter);
8230 qWarning("QPainter::worldTransform: Painter not active");
8231 return d->fakeState()->transform;
8233 return d->state->worldMatrix;
8237 Returns the transformation matrix combining the current
8238 window/viewport and world transformation.
8240 \sa setWorldTransform(), setWindow(), setViewport()
8243 QTransform QPainter::combinedTransform() const
8245 Q_D(const QPainter);
8247 qWarning("QPainter::combinedTransform: Painter not active");
8248 return QTransform();
8250 return d->state->worldMatrix * d->viewTransform();
8256 This function is used to draw \a pixmap, or a sub-rectangle of \a pixmap,
8257 at multiple positions with different scale, rotation and opacity. \a
8258 fragments is an array of \a fragmentCount elements specifying the
8259 parameters used to draw each pixmap fragment. The \a hints
8260 parameter can be used to pass in drawing hints.
8262 This function is potentially faster than multiple calls to drawPixmap(),
8263 since the backend can optimize state changes.
8265 \sa QPainter::PixmapFragment, QPainter::PixmapFragmentHint
8268 void QPainter::drawPixmapFragments(const PixmapFragment *fragments, int fragmentCount,
8269 const QPixmap &pixmap, PixmapFragmentHints hints)
8273 if (!d->engine || pixmap.isNull())
8277 for (int i = 0; i < fragmentCount; ++i) {
8278 QRectF sourceRect(fragments[i].sourceLeft, fragments[i].sourceTop,
8279 fragments[i].width, fragments[i].height);
8280 if (!(QRectF(pixmap.rect()).contains(sourceRect)))
8281 qWarning("QPainter::drawPixmapFragments - the source rect is not contained by the pixmap's rectangle");
8285 if (d->engine->isExtended()) {
8286 d->extended->drawPixmapFragments(fragments, fragmentCount, pixmap, hints);
8288 qreal oldOpacity = opacity();
8289 QTransform oldTransform = transform();
8291 for (int i = 0; i < fragmentCount; ++i) {
8292 QTransform transform = oldTransform;
8295 if (fragments[i].rotation == 0) {
8296 xOffset = fragments[i].x;
8297 yOffset = fragments[i].y;
8299 transform.translate(fragments[i].x, fragments[i].y);
8300 transform.rotate(fragments[i].rotation);
8302 setOpacity(oldOpacity * fragments[i].opacity);
8303 setTransform(transform);
8305 qreal w = fragments[i].scaleX * fragments[i].width;
8306 qreal h = fragments[i].scaleY * fragments[i].height;
8307 QRectF sourceRect(fragments[i].sourceLeft, fragments[i].sourceTop,
8308 fragments[i].width, fragments[i].height);
8309 drawPixmap(QRectF(-0.5 * w + xOffset, -0.5 * h + yOffset, w, h), pixmap, sourceRect);
8312 setOpacity(oldOpacity);
8313 setTransform(oldTransform);
8319 \class QPainter::PixmapFragment
8322 \brief This class is used in conjunction with the
8323 QPainter::drawPixmapFragments() function to specify how a pixmap, or
8324 sub-rect of a pixmap, is drawn.
8326 The \a sourceLeft, \a sourceTop, \a width and \a height variables are used
8327 as a source rectangle within the pixmap passed into the
8328 QPainter::drawPixmapFragments() function. The variables \a x, \a y, \a
8329 width and \a height are used to calculate the target rectangle that is
8330 drawn. \a x and \a y denotes the center of the target rectangle. The \a
8331 width and \a height in the target rectangle is scaled by the \a scaleX and
8332 \a scaleY values. The resulting target rectangle is then rotated \a
8333 rotation degrees around the \a x, \a y center point.
8335 \sa QPainter::drawPixmapFragments()
8341 This is a convenience function that returns a QPainter::PixmapFragment that is
8342 initialized with the \a pos, \a sourceRect, \a scaleX, \a scaleY, \a
8343 rotation, \a opacity parameters.
8346 QPainter::PixmapFragment QPainter::PixmapFragment::create(const QPointF &pos, const QRectF &sourceRect,
8347 qreal scaleX, qreal scaleY, qreal rotation,
8350 PixmapFragment fragment = {pos.x(), pos.y(), sourceRect.x(), sourceRect.y(), sourceRect.width(),
8351 sourceRect.height(), scaleX, scaleY, rotation, opacity};
8356 \variable QPainter::PixmapFragment::x
8357 \brief the x coordinate of center point in the target rectangle.
8361 \variable QPainter::PixmapFragment::y
8362 \brief the y coordinate of the center point in the target rectangle.
8366 \variable QPainter::PixmapFragment::sourceLeft
8367 \brief the left coordinate of the source rectangle.
8371 \variable QPainter::PixmapFragment::sourceTop
8372 \brief the top coordinate of the source rectangle.
8376 \variable QPainter::PixmapFragment::width
8378 \brief the width of the source rectangle and is used to calculate the width
8379 of the target rectangle.
8383 \variable QPainter::PixmapFragment::height
8385 \brief the height of the source rectangle and is used to calculate the
8386 height of the target rectangle.
8390 \variable QPainter::PixmapFragment::scaleX
8391 \brief the horizontal scale of the target rectangle.
8395 \variable QPainter::PixmapFragment::scaleY
8396 \brief the vertical scale of the target rectangle.
8400 \variable QPainter::PixmapFragment::rotation
8402 \brief the rotation of the target rectangle in degrees. The target
8403 rectangle is rotated after it has been scaled.
8407 \variable QPainter::PixmapFragment::opacity
8409 \brief the opacity of the target rectangle, where 0.0 is fully transparent
8410 and 1.0 is fully opaque.
8416 \enum QPainter::PixmapFragmentHint
8418 \value OpaqueHint Indicates that the pixmap fragments to be drawn are
8419 opaque. Opaque fragments are potentially faster to draw.
8421 \sa QPainter::drawPixmapFragments(), QPainter::PixmapFragment
8424 void qt_draw_helper(QPainterPrivate *p, const QPainterPath &path, QPainterPrivate::DrawOperation operation)
8426 p->draw_helper(path, operation);