Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |

# QPainter Class Reference

The QPainter class does low-level painting e.g. on widgets. More...

    #include <qdrawutil.h>

Inherited by QDirectPainter.

- List of all members, including inherited members
- Obsolete members
- Compatibility members

### Public Types

- typedef RenderHints
- enum TextDirection { Auto, RTL, LTR }

### Public Functions

- QPainter ()
- QPainter ( QPaintDevice * pd )
- ~QPainter ()
- const QBrush & background () const
- Qt::BGMode backgroundMode () const
- bool begin ( QPaintDevice * pd )
- QRect boundingRect ( int x, int y, int w, int h, int flags, const QString & str, int len = -1 )
- QRect boundingRect ( const QRect & r, int flags, const QString & str, int len = -1 )
- const QBrush & brush () const
- QPoint brushOrigin () const
- void clearRenderHints ( RenderHints hints )
- QRegion clipRegion () const
- QPaintDevice * device () const
- void drawArc ( const QRect & r, int a, int alen )
- void drawArc ( int x, int y, int w, int h, int startAngle, int spanAngle )
- void drawChord ( const QRect & r, int a, int alen )
- void drawChord ( int x, int y, int w, int h, int startAngle, int spanAngle )
- void drawConvexPolygon ( const QPointArray & a, int index = 0, int npoints = -1 )
- void drawEdges ( const QRect & r, Qt::RectangleEdges edges )
- void drawEdges ( int x, int y, int width, int height, Qt::RectangleEdges edges )
- void drawEllipse ( const QRect & r )
- void drawEllipse ( int x, int y, int w, int h )
- void drawLine ( const QPoint & p1, const QPoint & p2 )
- void drawLine ( int x1, int y1, int x2, int y2 )
- void drawLineSegments ( const QPointArray & a, int index = 0, int nlines = -1 )
- void drawPath ( const QPainterPath & path )
- void drawPicture ( const QPoint & p, const QPicture & picture )
- void drawPicture ( int x, int y, const QPicture & picture )
- void drawPie ( const QRect & r, int a, int alen )
- void drawPie ( int x, int y, int w, int h, int startAngle, int spanAngle )
- void drawPixmap ( const QRect & r, const QPixmap & pm, const QRect & sr, Qt::PixmapDrawingMode mode = Qt::ComposePixmap )
- void drawPixmap ( int x, int y, const QPixmap & pixmap, Qt::PixmapDrawingMode mode = Qt::ComposePixmap )
- void drawPixmap ( int x, int y, int width, int height, const QPixmap & pixmap, Qt::PixmapDrawingMode mode = Qt::ComposePixmap )
- void drawPixmap ( int x, int y, int w, int h, const QPixmap & pm, int sx, int sy, int sw, int sh, Qt::PixmapDrawingMode mode = Qt::ComposePixmap )
- void drawPixmap ( int x, int y, const QPixmap & pixmap, int sx, int sy, int sw, int sh, Qt::PixmapDrawingMode mode = Qt::ComposePixmap )
- void drawPixmap ( const QPoint & p, const QPixmap & pm, const QRect & sr, Qt::PixmapDrawingMode mode = Qt::ComposePixmap )
- void drawPixmap ( const QPoint & p, const QPixmap & pm, Qt::PixmapDrawingMode mode = Qt::ComposePixmap )
- void drawPixmap ( const QRect & r, const QPixmap & pm, Qt::PixmapDrawingMode mode = Qt::ComposePixmap )
- void drawPoint ( const QPoint & p )
- void drawPoint ( int x, int y )
- void drawPoints ( const QPointArray & pa )
- void drawPolygon ( const QPointArray & pa, bool winding = false, int index = 0, int npoints = -1 )
- void drawPolygon ( const QPolygon & polygon, bool winding = false, int index = 0, int npoints = -1 )
- void drawPolyline ( const QPointArray & a, int index = 0, int npoints = -1 )
- void drawRect ( const QRect & r )
- void drawRect ( const QRectF & r )
- void drawRect ( int x, int y, int w, int h )
- void drawRects ( const QList<QRectF> & rects )
- void drawRoundRect ( const QRect & r, int xRnd = 25, int yRnd = 25 )
- void drawRoundRect ( int x, int y, int w, int h, int xRnd = 25, int yRnd = 25 )
- void drawText ( const QPoint & p, const QString & str, TextDirection dir = Auto )
- void drawText ( int x, int y, const QString & text, TextDirection dir = Auto )
- void drawText ( int x, int y, int w, int h, int flags, const QString & str, int len = -1, QRect * br = 0 )
- void drawText ( const QRect & r, int flags, const QString & str, int len = -1, QRect * br = 0 )
- void drawTiledPixmap ( int x, int y, int w, int h, const QPixmap & pixmap, int sx = 0, int sy = 0, Qt::PixmapDrawingMode mode = Qt::ComposePixmap )
- void drawTiledPixmap ( const QRect & r, const QPixmap & pixmap, const QPoint & sp = QPoint(), Qt::PixmapDrawingMode mode = Qt::ComposePixmap )
- bool end ()
- void eraseRect ( const QRect & r )
- void eraseRect ( int x, int y, int w, int h )
- void fillPath ( const QPainterPath & path, const QBrush & brush )
- void fillRect ( const QRect & r, const QBrush & brush )
- void fillRect ( int x, int y, int w, int h, const QBrush & brush )
- const QFont & font () const
- QFontInfo fontInfo () const
- QFontMetrics fontMetrics () const
- bool hasClipping () const
- bool hasViewXForm () const
- void initFrom ( const QWidget * widget )
- bool isActive () const
- const QMatrix & matrix () const
- bool matrixEnabled () const
- const QPen & pen () const
- RenderHints renderHints () const
- void resetMatrix ()
- void restore ()
- void rotate ( double a )
- void save ()
- void scale ( double sx, double sy )
- void setBackground ( const QBrush & bg )
- void setBackgroundMode ( Qt::BGMode mode )
- void setBrush ( Qt::BrushStyle style )
- void setBrush ( const QBrush & brush )
- void setBrushOrigin ( const QPoint & p )
- void setBrushOrigin ( int x, int y )
- void setClipPath ( const QPainterPath & path )
- void setClipRect ( int x, int y, int w, int h )
- void setClipRect ( const QRect & rect )
- void setClipRegion ( const QRegion & r )
- void setClipping ( bool enable )
- void setFont ( const QFont & font )
- void setMatrix ( const QMatrix & matrix, bool combine = false )
- void setMatrixEnabled ( bool enable )
- void setPen ( const QPen & pen )
- void setPen ( const QColor & color )
- void setPen ( Qt::PenStyle style )
- void setRenderHints ( RenderHints hints )
- void setViewXForm ( bool enable )
- void setViewport ( int x, int y, int w, int h )
- void setViewport ( const QRect & r )
- void setWindow ( int x, int y, int w, int h )
- void setWindow ( const QRect & r )
- void shear ( double sh, double sv )
- void strokePath ( const QPainterPath & path, const QPen & pen )
- RenderHints supportedRenderHints () const
- void translate ( double dx, double dy )
- void translate ( const QPoint & offset )
- QRect viewport () const
- QRect window () const

### Related Non-Members

- void qDrawPlainRect ( QPainter * p, int x, int y, int w, int h, const QColor & c, int lineWidth = 1, const QBrush * fill = 0 )
- void qDrawShadeLine ( QPainter * p, int x1, int y1, int x2, int y2, const QPalette & pal, bool sunken = true, int lineWidth = 1, int midLineWidth = 0 )
- void qDrawShadePanel ( QPainter * p, int x, int y, int w, int h, const QPalette & pal, bool sunken = false, int lineWidth = 1, const QBrush * fill = 0 )
- void qDrawShadeRect ( QPainter * p, int x, int y, int w, int h, const QPalette & pal, bool sunken = false, int lineWidth = 1, int midLineWidth = 0, const QBrush * fill = 0 )
- void qDrawWinButton ( QPainter * p, int x, int y, int w, int h, const QPalette & pal, bool sunken = false, const QBrush * fill = 0 )
- void qDrawWinPanel ( QPainter * p, int x, int y, int w, int h, const QPalette & pal, bool sunken = false, const QBrush * fill = 0 )

## Detailed Description

The QPainter class does low-level painting e.g. on widgets.

The painter provides highly optimized functions to do most of the drawing GUI programs require. QPainter can draw everything from simple lines to complex shapes like pies and chords. It can also draw aligned text and pixmaps. Normally, it draws in a "natural" coordinate system, but it can also do view and world transformation.

The typical use of a painter is:

- Construct a painter.
- Set a pen, a brush etc.
- Draw.
- Destroy the painter.

Mostly, all this is done inside a paint event. (In fact, 99% of all QPainter use is in a reimplementation of QWidget::paintEvent(), and the painter is heavily optimized for such use.) Here's one very simple example:

        void SimpleExampleWidget::paintEvent()
        {
            QPainter paint(this);
            paint.setPen(Qt::blue);
            paint.drawText(rect(), Qt::AlignCenter, "The Text");
        }

If you need to draw a complex shape, especially if you need to do so repeatedly, consider creating a QPainterPath and drawing it using drawPath().

Usage is simple, and there are many settings you can use:

- font() is the currently set font. If you set a font that isn't available, Qt finds a close match. In fact font() returns what you set using setFont() and fontInfo() returns the font actually being used (which may be the same).
- brush() is the currently set brush; the color or pattern that's used for filling e.g. circles.
- pen() is the currently set pen; the color or stipple that's used for drawing lines or boundaries.
- backgroundMode() is `Opaque` or `Transparent`, i.e. whether backgroundColor() is used or not.
- backgroundColor() only applies when backgroundMode() is Opaque and pen() is a stipple. In that case, it describes the color of the background pixels in the stipple.
- brushOrigin() is the origin of the tiled brushes, normally the origin of the window.
- viewport(), window(), matrix() and many more make up the painter's coordinate transformation system. See The Coordinate System for an explanation of this, or see below for a very brief overview of the functions.
- hasClipping() is whether the painter clips at all. (The paint device clips, too.) If the painter clips, it clips to clipRegion().
- pos() is the current position, set by moveTo() and used by lineTo().

Note that some of these settings mirror settings in some paint devices, e.g. QWidget::font(). QPainter::begin() (or the QPainter constructor) copies these attributes from the paint device. Calling, for example, QWidget::setFont() doesn't take effect until the next time a painter begins painting on it.

save() saves all of these settings on an internal stack, restore() pops them back.

The core functionality of QPainter is drawing, and there are functions to draw most primitives: drawPoint(), drawPoints(), drawLine(), drawRect(), drawRoundRect(), drawEllipse(), drawArc(), drawPie(), drawChord(), drawLine:Segments(), drawPolyline(), drawPolygon(), drawConvexPolygon() and drawCubicBezier(). All of these functions take integer coordinates; there are no floating-point versions since we want drawing to be as fast as possible.

The functions that draw curved primitives accept angles measured in 1/16s of a degree. Angles are measured in an counter-clockwise direction from the rightmost edge of the primitive being drawn.

There are functions to draw pixmaps/images, namely drawPixmap(), drawImage() and drawTiledPixmap(). drawPixmap() and drawImage() produce the same result, except that drawPixmap() is faster on-screen and drawImage() faster and sometimes better on QPrinter and QPicture.

Text drawing is done using drawText(), and when you need fine-grained positioning, boundingRect() tells you where a given drawText() command would draw.

There is a drawPicture() function that draws the contents of an entire QPicture using this painter. drawPicture() is the only function that disregards all the painter's settings: the QPicture has its own settings.

Normally, the QPainter operates on the device's own coordinate system (usually pixels), but QPainter has good support for coordinate transformation. See The Coordinate System for a more general overview and a simple example.

The most common functions used are scale(), rotate(), translate() and shear(), all of which operate on the matrix(). setMatrix() can replace or add to the currently set matrix().

setViewport() sets the rectangle on which QPainter operates. The default is the entire device, which is usually fine, except on printers. setWindow() sets the coordinate system, that is, the rectangle that maps to viewport(). What's drawn inside the window() ends up being inside the viewport(). The window's default is the same as the viewport, and if you don't use the transformations, they are optimized away, gaining another little bit of speed.

After all the coordinate transformation is done, QPainter can clip the drawing to an arbitrary rectangle or region. hasClipping() is true if QPainter clips, and clipRegion() returns the clip region. You can set it using either setClipRegion() or setClipRect(). Note that the clipping can be slow. It's all system-dependent, but as a rule of thumb, you can assume that drawing speed is inversely proportional to the number of rectangles in the clip region.

After QPainter's clipping, the paint device may also clip. For example, most widgets clip away the pixels used by child widgets, and most printers clip away an area near the edges of the paper. This additional clipping is not reflected by the return value of clipRegion() or hasClipping().

QPainter also includes some less-used functions that are very useful on those occasions when they're needed.

isActive() indicates whether the painter is active. begin() (and the most usual constructor) makes it active. end() (and the destructor) deactivates it. If the painter is active, device() returns the paint device on which the painter paints.

Sometimes it is desirable to make someone else paint on an unusual QPaintDevice. QPainter supports a static function to do this, redirect(). We recommend not using it, but for some hacks it's perfect.

setTabStops() and setTabArray() can change where the tab stops are, but these are very seldomly used.

Warning: A QPainter can only be used inside a paintEvent() or a function called by a paintEvent().

Warning: Note that QPainter does not attempt to work around coordinate limitations in the underlying window system. Some platforms may behave incorrectly with coordinates outside +/-4000.

Application Walkthrough Coordinate System Overview

See also QPaintDevice, QWidget, QPixmap, QPrinter, and QPicture.

## Member Type Documentation

### enum QPainter::TextDirection

`QPainter::Auto` |
`QPainter::RTL` | right to left
`QPainter::LTR` | left to right

See also drawText().

## Member Function Documentation

### QPainter::QPainter ()

Constructs a painter.

Notice that all painter settings (setPen, setBrush etc.) are reset to default values when begin() is called.

See also begin() and end().

### QPainter::QPainter ( QPaintDevice * pd )

Constructs a painter that begins painting the paint device pd immediately.

This constructor is convenient for short-lived painters, e.g. in a paint event and should be used only once. The constructor calls begin() for you and the QPainter destructor automatically calls end().

Here's an example using begin() and end():

        void MyWidget::paintEvent(QPaintEvent *)
        {
            QPainter p;
            p.begin(this);
            p.drawLine(...);        // drawing code
            p.end();
        }

The same example using this constructor:

        void MyWidget::paintEvent(QPaintEvent *)
        {
            QPainter p(this);
            p.drawLine(...);        // drawing code
        }

Since the constructor cannot provide feedback when the initialization of the painter failed you should rather use begin() and end() to paint on external devices, e.g. printers.

See also begin() and end().

### QPainter::~QPainter ()

Destroys the painter.

### const QBrush & QPainter::background () const

Returns the current background brush.

See also setBackground() and QBrush.

### Qt::BGMode QPainter::backgroundMode () const

Returns the current background mode.

See also setBackgroundMode() and Qt::BGMode.

### bool QPainter::begin ( QPaintDevice * pd )

Begins painting the paint device pd and returns true if successful; otherwise returns false.

The errors that can occur are serious problems, such as these:

        p->begin(0); // impossible - paint device cannot be 0

        QPixmap pm(0, 0);
        p->begin(pm); // impossible - pm.isNull();

        p->begin(myWidget);
        p2->begin(myWidget); // impossible - only one painter at a time

Note that most of the time, you can use one of the constructors instead of begin(), and that end() is automatically done at destruction.

Warning: A paint device can only be painted by one painter at a time.

See also end().

### QRect QPainter::boundingRect ( int x, int y, int w, int h, int flags, const QString & str, int len = -1 )

Returns the bounding rectangle of the aligned text that would be printed with the corresponding drawText() function using the first len characters of the string str, if len is > -1, or the whole of the string if len is -1. The drawing, and hence the bounding rectangle, is constrained to the rectangle that begins at point (x, y) with width w and height h, or to the rectangle required to draw the text, whichever is the larger.

The flags argument is the bitwise OR of the following flags:

Flag | Meaning
--- | ---
`Qt::AlignAuto` | aligns according to the language, usually left.
`Qt::AlignLeft` | aligns to the left border.
`Qt::AlignRight` | aligns to the right border.
`Qt::AlignHCenter` | aligns horizontally centered.
`Qt::AlignTop` | aligns to the top border.
`Qt::AlignBottom` | aligns to the bottom border.
`Qt::AlignVCenter` | aligns vertically centered.
`Qt::AlignCenter` | (== `Qt::AlignHCenter` | `Qt::AlignVCenter`).
`Qt::TextSingleLine` | ignores newline characters in the text.
`Qt::TextExpandTabs` | expands tabs.
`Qt::TextShowMnemonic` | interprets "&x" as "<u>x</u>".
`Qt::TextWordBreak` | breaks the text to fit the rectangle.

Qt::Horizontal alignment defaults to `Qt::AlignLeft` and vertical alignment defaults to `Qt::AlignTop`.

If several of the horizontal or several of the vertical alignment flags are set, the resulting alignment is undefined.

See also Qt::TextFlags.

### QRect QPainter::boundingRect ( const QRect & r, int flags, const QString & str, int len = -1 )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Returns the bounding rectangle constrained by rectangle r.

### const QBrush & QPainter::brush () const

Returns the painter's current brush.

See also QPainter::setBrush().

### QPoint QPainter::brushOrigin () const

Returns the brush origin currently set.

See also setBrushOrigin().

### void QPainter::clearRenderHints ( RenderHints hints )

Clears the render hints supplied in hints. Several render hints can be OR'ed together in hints.

### QRegion QPainter::clipRegion () const

Returns the currently set clip region. Note that the clip region is given in logical coordinates and subject to coordinate transformation.

See also setClipRegion(), setClipRect(), and setClipping().

### QPaintDevice * QPainter::device () const

Returns the paint device on which this painter is currently painting, or 0 if the painter is not active.

See also QPaintDevice::paintingActive().

### void QPainter::drawArc ( const QRect & r, int a, int alen )

Draws an arc defined by the rectangle r, the start angle a and the arc length alen.

The angles a and alen are 1/16th of a degree, i.e. a full circle equals 5760 (16*360). Positive values of a and alen mean counter-clockwise while negative values mean the clockwise direction. Zero degrees is at the 3 o'clock position.

Example:

        QPainter p(myWidget);
        p.drawArc(QRect(10,10, 70,100), 100*16, 160*16); // draws a "(" arc

See also drawPie() and drawChord().

### void QPainter::drawArc ( int x, int y, int w, int h, int startAngle, int spanAngle )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Draws the arc that fits inside the rectangle (x, y, w, h), with the given startAngle and spanAngle.

### void QPainter::drawChord ( const QRect & r, int a, int alen )

Draws a chord defined by the rectangle r, the start angle a and the arc length alen.

The chord is filled with the current brush().

The angles a and alen are 1/16th of a degree, i.e. a full circle equals 5760 (16*360). Positive values of a and alen mean counter-clockwise while negative values mean the clockwise direction. Zero degrees is at the 3 o'clock position.

See also drawArc() and drawPie().

### void QPainter::drawChord ( int x, int y, int w, int h, int startAngle, int spanAngle )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Draws a chord that fits inside the rectangle (x, y, w, h) with the given startAngle and spanAngle.

### void QPainter::drawConvexPolygon ( const QPointArray & a, int index = 0, int npoints = -1 )

Draws the convex polygon defined by the npoints points in a starting at a[index] (index defaults to 0).

If the supplied polygon is not convex, the results are undefined.

On some platforms (e.g. X Window), this is faster than drawPolygon().

### void QPainter::drawEdges ( const QRect & r, Qt::RectangleEdges edges )

Draws the one or more edges of the rectangle r. Edges to draw are specified in edges. Drawing all edges are equivalent to setting the current brush to Qt::NoBrush and calling QPainter::drawRec(t).

See also drawRect().

### void QPainter::drawEdges ( int x, int y, int width, int height, Qt::RectangleEdges edges )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Draws the edges specified by the rectangle at position (x, y) and with the given width and height.

### void QPainter::drawEllipse ( const QRect & r )

Draws the ellipse that fits inside rectangle r.

### void QPainter::drawEllipse ( int x, int y, int w, int h )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Draws an ellipse with center at (x + w/2, y + h/2) and size (w, h).

### void QPainter::drawLine ( const QPoint & p1, const QPoint & p2 )

Draws a line from point p1 to point p2.

See also pen().

### void QPainter::drawLine ( int x1, int y1, int x2, int y2 )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Draws a line from (x1, y1) to (x2, y2) and sets the current pen position to (x2, y2).

### void QPainter::drawLineSegments ( const QPointArray & a, int index = 0, int nlines = -1 )

Draws nlines separate lines from points defined in a, starting at a[index] (index defaults to 0). If nlines is -1 (the default) all points until the end of the array are used (i.e. (a.size()-index)/2 lines are drawn).

Draws the 1st line from a[index] to a[index + 1]. Draws the 2nd line from a[index + 2] to a[index + 3] etc.

See also drawPolyline(), drawPolygon(), and QPen.

### void QPainter::drawPath ( const QPainterPath & path )

Draws the painter path specified by path using the current pen for outline and the current brush for filling.

### void QPainter::drawPicture ( const QPoint & p, const QPicture & picture )

Replays the picture picture at point p.

This function does exactly the same as QPicture::play() when called with p = QPoint(0, 0).

### void QPainter::drawPicture ( int x, int y, const QPicture & picture )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Draws picture picture at point (x, y).

### void QPainter::drawPie ( const QRect & r, int a, int alen )

Draws a pie defined by the rectangle r, the start angle a and the arc length alen.

The pie is filled with the current brush().

The angles a and alen are 1/16th of a degree, i.e. a full circle equals 5760 (16*360). Positive values of a and alen mean counter-clockwise while negative values mean the clockwise direction. Zero degrees is at the 3 o'clock position.

See also drawArc() and drawChord().

### void QPainter::drawPie ( int x, int y, int w, int h, int startAngle, int spanAngle )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Draws a pie segment that fits inside the rectangle (x, y, w, h) with the given startAngle and spanAngle.

### void QPainter::drawPixmap ( const QRect & r, const QPixmap & pm, const QRect & sr, Qt::PixmapDrawingMode mode = Qt::ComposePixmap )

Draws the rectanglular portion sr, of pixmap pm, into rectangle r in the paint device. The blend mode mode decides how the pixmap is merged with the target paint device.

See also Qt::PixmapDrawingMode.

### void QPainter::drawPixmap ( int x, int y, const QPixmap & pixmap, Qt::PixmapDrawingMode mode = Qt::ComposePixmap )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Draws the given pixmap at position (x, y) using the specified drawing mode.

### void QPainter::drawPixmap ( int x, int y, int width, int height, const QPixmap & pixmap, Qt::PixmapDrawingMode mode = Qt::ComposePixmap )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Draws the pixmap in the rectangle at position (x, y) and of the given width and height.

### void QPainter::drawPixmap ( int x, int y, int w, int h, const QPixmap & pm, int sx, int sy, int sw, int sh, Qt::PixmapDrawingMode mode = Qt::ComposePixmap )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Draws the rectangular portion with the origin (sx, sy), width sw and height sh, of the pixmap pm, at the point (x, y), with a width of w and a height of h. If mode is QPainter::CopyPixmap pm will not be masked to QPixmap::mask()

### void QPainter::drawPixmap ( int x, int y, const QPixmap & pixmap, int sx, int sy, int sw, int sh, Qt::PixmapDrawingMode mode = Qt::ComposePixmap )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Draws a pixmap at (x, y) by copying a part of pixmap into the paint device.

(x, y) specifies the top-left point in the paint device that is to be drawn onto. (sx, sy) specifies the top-left point in pixmap that is to be drawn. The default is (0, 0).

(sw, sh) specifies the size of the pixmap that is to be drawn. The default, (-1, -1), means all the way to the bottom-right of the pixmap.

See also QPixmap::setMask().

### void QPainter::drawPixmap ( const QPoint & p, const QPixmap & pm, const QRect & sr, Qt::PixmapDrawingMode mode = Qt::ComposePixmap )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Draws the rectangle sr of pixmap pm with its origin at point p.

### void QPainter::drawPixmap ( const QPoint & p, const QPixmap & pm, Qt::PixmapDrawingMode mode = Qt::ComposePixmap )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Draws the pixmap pm with its origin at point p.

### void QPainter::drawPixmap ( const QRect & r, const QPixmap & pm, Qt::PixmapDrawingMode mode = Qt::ComposePixmap )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Draws the pixmap pm into the rectangle r.

### void QPainter::drawPoint ( const QPoint & p )

Draws a single point at position p using the current pen's color.

See also QPen.

### void QPainter::drawPoint ( int x, int y )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Draws a single point at position (x, y).

### void QPainter::drawPoints ( const QPointArray & pa )

Draws the array of points pa using the current pen's color.

See also QPen.

### void QPainter::drawPolygon ( const QPointArray & pa, bool winding = false, int index = 0, int npoints = -1 )

### void QPainter::drawPolygon ( const QPolygon & polygon, bool winding = false, int index = 0, int npoints = -1 )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Draws the polygon defined by the npoints points in a starting at a[index]. (index defaults to 0.)

If npoints is -1 (the default) all points until the end of the array are used (i.e. a.size()-index line segments define the polygon).

The first point is always connected to the last point.

The polygon is filled with the current brush(). If winding is true, the polygon is filled using the winding fill algorithm. If winding is false, the polygon is filled using the even-odd (alternative) fill algorithm.

See also drawLineSegments(), drawPolyline(), and QPen.

### void QPainter::drawPolyline ( const QPointArray & a, int index = 0, int npoints = -1 )

Draws the polyline defined by the npoints points in a starting at a[index]. (index defaults to 0.)

If npoints is -1 (the default) all points until the end of the array are used (i.e. a.size()-index-1 line segments are drawn).

See also drawLineSegments(), drawPolygon(), and QPen.

### void QPainter::drawRect ( const QRect & r )

### void QPainter::drawRect ( const QRectF & r )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Draws the rectangle r.

See also QPen and drawRoundRect().

### void QPainter::drawRect ( int x, int y, int w, int h )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Draws a rectangle with upper left corner at (x, y) and with width w and height h.

### void QPainter::drawRects ( const QList<QRectF> & rects )

Draws all the rectangles in the rects list using the current pen and brush.

See also drawRect().

### void QPainter::drawRoundRect ( const QRect & r, int xRnd = 25, int yRnd = 25 )

Draws a rectangle r with rounded corners.

The xRnd and yRnd arguments specify how rounded the corners should be. 0 is angled corners, 99 is maximum roundedness.

The width and height include all of the drawn lines.

See also drawRect() and QPen.

### void QPainter::drawRoundRect ( int x, int y, int w, int h, int xRnd = 25, int yRnd = 25 )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Draws a rectangle with rounded corners at (x, y), with width w and height h.

The xRnd and yRnd arguments specify how rounded the corners should be. 0 is angled corners, 99 is maximum roundedness.

The width and height include all of the drawn lines.

See also drawRect() and QPen.

### void QPainter::drawText ( const QPoint & p, const QString & str, TextDirection dir = Auto )

Draws the string str at position p. The text's direction is given by dir.

See also QPainter::TextDirection.

### void QPainter::drawText ( int x, int y, const QString & text, TextDirection dir = Auto )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Draws the given text at position (x, y), in text direction dir.

### void QPainter::drawText ( int x, int y, int w, int h, int flags, const QString & str, int len = -1, QRect * br = 0 )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Draws the string str within the rectangle with origin (x, y), width w and height h. If len is -1 (the default) all the text is drawn, otherwise only the first len characters are drawn. The flags that are given in the flags parameter are Qt::AlignmentFlags and Qt::TextFlags OR'd together. br (if not null) is set to the actual bounding rectangle of the output.

### void QPainter::drawText ( const QRect & r, int flags, const QString & str, int len = -1, QRect * br = 0 )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Draws the string str within the rectangle r. If len is -1 (the default) all the text is drawn, otherwise only the first len characters are drawn. The flags that are given in the flags parameter are Qt::AlignmentFlags and Qt::TextFlags OR'd together. br (if not null) is set to the actual bounding rectangle of the output.

### void QPainter::drawTiledPixmap ( int x, int y, int w, int h, const QPixmap & pixmap, int sx = 0, int sy = 0, Qt::PixmapDrawingMode mode = Qt::ComposePixmap )

Draws a tiled pixmap in the specified rectangle.

(x, y) specifies the top-left point in the paint device that is to be drawn onto; with the width and height given by w and h. (sx, sy) specifies the top-left point in the pixmap that is to be drawn; this defaults to (0, 0). The pixmap is drawn using the given drawing mode.

Calling drawTiledPixmap() is similar to calling drawPixmap() several times to fill (tile) an area with a pixmap, but is potentially much more efficient depending on the underlying window system.

See also drawPixmap().

### void QPainter::drawTiledPixmap ( const QRect & r, const QPixmap & pixmap, const QPoint & sp = QPoint(), Qt::PixmapDrawingMode mode = Qt::ComposePixmap )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Draws a tiled pixmap, inside rectangle r with its origin at point sp, using the given drawing mode.

### bool QPainter::end ()

Ends painting. Any resources used while painting are released. You don't normally need to call this since it is called by the destructor.

See also begin() and isActive().

### void QPainter::eraseRect ( const QRect & r )

Erases the area inside the rectangle r. Equivalent to `fillRect(r, backgroundColor())`.

### void QPainter::eraseRect ( int x, int y, int w, int h )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Erases the area inside x, y, w, h. Equivalent to `fillRect(x, y, w, h, backgroundColor())`.

### void QPainter::fillPath ( const QPainterPath & path, const QBrush & brush )

Fills the path path using the given brush. The outline is not drawn.

### void QPainter::fillRect ( const QRect & r, const QBrush & brush )

Fills the rectangle r with the brush.

You can specify a QColor as brush, since there is a QBrush constructor that takes a QColor argument and creates a solid pattern brush.

See also drawRect().

### void QPainter::fillRect ( int x, int y, int w, int h, const QBrush & brush )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Fills the rectangle (x, y, w, h) with the brush.

You can specify a QColor as brush, since there is a QBrush constructor that takes a QColor argument and creates a solid pattern brush.

See also drawRect().

### const QFont & QPainter::font () const

Returns the currently set painter font.

See also setFont() and QFont.

### QFontInfo QPainter::fontInfo () const

Returns the font info for the painter, if the painter is active. It is not possible to obtain font information for an inactive painter, so the return value is undefined if the painter is not active.

See also fontMetrics() and isActive().

### QFontMetrics QPainter::fontMetrics () const

Returns the font metrics for the painter, if the painter is active. It is not possible to obtain metrics for an inactive painter, so the return value is undefined if the painter is not active.

See also fontInfo() and isActive().

### bool QPainter::hasClipping () const

Returns true if clipping has been set; otherwise returns false.

See also setClipping().

### bool QPainter::hasViewXForm () const

Returns true if view transformation is enabled; otherwise returns false.

See also setViewXForm() and xForm().

### void QPainter::initFrom ( const QWidget * widget )

Initializes the painters pen, background and font to the same as widget.

### bool QPainter::isActive () const

Returns true if the painter is active painting, i.e. begin() has been called and end() has not yet been called; otherwise returns false.

See also QPaintDevice::paintingActive().

### const QMatrix & QPainter::matrix () const

Returns the world transformation matrix.

See also setMatrix().

### bool QPainter::matrixEnabled () const

Returns true if world transformation is enabled; otherwise returns false.

See also setMatrixEnabled() and setMatrix().

### const QPen & QPainter::pen () const

Returns the painter's current pen.

See also setPen().

### RenderHints QPainter::renderHints () const

Returns a flag that specifies the rendering hints that are set for this painter.

### void QPainter::resetMatrix ()

Resets any transformations that were made using translate(), scale(), shear(), rotate(), setMatrix(), setViewport() and setWindow().

See also matrix() and setMatrix().

### void QPainter::restore ()

Restores the current painter state (pops a saved state off the stack).

See also save().

### void QPainter::rotate ( double a )

Rotates the coordinate system a degrees counterclockwise.

See also translate(), scale(), shear(), resetXForm(), setMatrix(), and xForm().

### void QPainter::save ()

Saves the current painter state (pushes the state onto a stack). A save() must be followed by a corresponding restore(). end() unwinds the stack.

See also restore().

### void QPainter::scale ( double sx, double sy )

Scales the coordinate system by (sx, sy).

See also translate(), shear(), rotate(), resetXForm(), setMatrix(), and xForm().

### void QPainter::setBackground ( const QBrush & bg )

Sets the background brush of the painter to bg.

The background brush is the brush that is filled in when drawing opaque text, stippled lines and bitmaps. The background brush has no effect in transparent background mode (which is the default).

See also background(), setBackgroundMode(), and Qt::BGMode.

### void QPainter::setBackgroundMode ( Qt::BGMode mode )

Sets the background mode of the painter to mode, which must be either `Qt::TransparentMode` (the default) or `Qt::OpaqueMode`.

Transparent mode draws stippled lines and text without setting the background pixels. Opaque mode fills these space with the current background color.

Note that in order to draw a bitmap or pixmap transparently, you must use QPixmap::setMask().

See also backgroundMode() and setBackground().

### void QPainter::setBrush ( Qt::BrushStyle style )

Sets the painter's brush to black color and the specified style.

See also brush() and QBrush.

### void QPainter::setBrush ( const QBrush & brush )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Sets the painter's brush to brush.

The brush defines how shapes are filled.

See also brush().

### void QPainter::setBrushOrigin ( const QPoint & p )

Sets the brush origin to p.

The brush origin specifies the (0, 0) coordinate of the painter's brush. This setting only applies to pattern brushes and pixmap brushes.

See also brushOrigin().

### void QPainter::setBrushOrigin ( int x, int y )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Sets the brush's origin to point (x, y).

### void QPainter::setClipPath ( const QPainterPath & path )

Sets the clippath for the painter to path.

The clip path is specified in logical (painter) coordinates.

Warning: This function is not yet implemented.

### void QPainter::setClipRect ( int x, int y, int w, int h )

Sets the clip region to the rectangle x, y, w, h and enables clipping.

See also setClipRegion(), clipRegion(), and setClipping().

### void QPainter::setClipRect ( const QRect & rect )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Sets the clip region of the rectange rect.

### void QPainter::setClipRegion ( const QRegion & r )

Sets the clip region to r and enables clipping.

Note that the clip region is given in logical coordinates and subject to coordinate transformation.

See also setClipRect(), clipRegion(), and setClipping().

### void QPainter::setClipping ( bool enable )

Enables clipping if enable is true, or disables clipping if enable is false.

See also hasClipping(), setClipRect(), and setClipRegion().

### void QPainter::setFont ( const QFont & font )

Sets the painter's font to font.

This font is used by subsequent drawText() functions. The text color is the same as the pen color.

See also font() and drawText().

### void QPainter::setMatrix ( const QMatrix & matrix, bool combine = false )

Sets the transformation matrix to matrix and enables transformations.

If combine is true, then matrix is combined with the current transformation matrix; otherwise matrix replaces the current transformation matrix.

If matrix is the identity matrix and combine is false, this function calls setMatrixEnabled(false). (The identity matrix is the matrix where QMatrix::m11() and QMatrix::m22() are 1.0 and the rest are 0.0.)

World transformations are applied after the view transformations (i.e. window and viewport).

The following functions can transform the coordinate system without using a QMatrix:

- translate()
- scale()
- shear()
- rotate()

They operate on the painter's worldMatrix() and are implemented like this:

        void QPainter::rotate(double a)
        {
            QMatrix m;
            m.rotate(a);
            setMatrix(m, true);
        }

Note that you should always use combine when you are drawing into a QPicture. Otherwise it may not be possible to replay the picture with additional transformations. Using translate(), scale(), etc., is safe.

For a brief overview of coordinate transformation, see the Coordinate System Overview.

See also matrix(), setMatrixEnabled(), and QMatrix.

### void QPainter::setMatrixEnabled ( bool enable )

Enables transformations if enable is true, or disables world transformations if enable is false. The world transformation matrix is not changed.

See also setMatrix() and matrix().

### void QPainter::setPen ( const QPen & pen )

Sets a new painter pen.

The pen defines how to draw lines and outlines, and it also defines the text color.

See also pen().

### void QPainter::setPen ( const QColor & color )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Sets the painter's pen to have style `Qt::SolidLine`, width 0 and the specified color.

See also pen() and QPen.

### void QPainter::setPen ( Qt::PenStyle style )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Sets the painter's pen to have style style, width 0 and black color.

See also pen() and QPen.

### void QPainter::setRenderHints ( RenderHints hints )

Sets the render hints supplied in hints. Several render hints can be OR'ed together in hints.

### void QPainter::setViewXForm ( bool enable )

Enables view transformations if enable is true, or disables view transformations if enable is false.

See also hasViewXForm(), setWindow(), setViewport(), setMatrix(), and setMatrixEnabled().

### void QPainter::setViewport ( int x, int y, int w, int h )

Sets the viewport rectangle view transformation for the painter and enables view transformation.

The viewport rectangle is part of the view transformation. The viewport specifies the device coordinate system and is specified by the x, y, w width and h height parameters. Its sister, the window(), specifies the logical coordinate system.

The default viewport rectangle is the same as the device's rectangle. See the Coordinate System Overview for an overview of coordinate transformation.

See also viewport(), setWindow(), setViewXForm(), setMatrix(), and setMatrixEnabled().

### void QPainter::setViewport ( const QRect & r )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Sets the painter's viewport rectangle to r.

### void QPainter::setWindow ( int x, int y, int w, int h )

Sets the window rectangle view transformation for the painter and enables view transformation.

The window rectangle is part of the view transformation. The window specifies the logical coordinate system and is specified by the x, y, w width and h height parameters. Its sister, the viewport(), specifies the device coordinate system.

The default window rectangle is the same as the device's rectangle. See the Coordinate System Overview for an overview of coordinate transformation.

See also window(), setViewport(), setViewXForm(), setMatrix(), and setMatrixEnabled().

### void QPainter::setWindow ( const QRect & r )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Sets the painter's window to the rectangle r.

### void QPainter::shear ( double sh, double sv )

Shears the coordinate system by (sh, sv).

See also translate(), scale(), rotate(), resetXForm(), setMatrix(), and xForm().

### void QPainter::strokePath ( const QPainterPath & path, const QPen & pen )

Draws the outline (strokes) the path path with the pen specified by pen

### RenderHints QPainter::supportedRenderHints () const

Returns a flag that specifies the rendering hints that this painter supports.

### void QPainter::translate ( double dx, double dy )

Translates the coordinate system by (dx, dy). After this call, (dx, dy) is added to points.

For example, the following code draws the same point twice:

        void MyWidget::paintEvent()
        {
            QPainter paint(this);

            paint.drawPoint(0, 0);

            paint.translate(100.0, 40.0);
            paint.drawPoint(-100, -40);
        }

See also scale(), shear(), rotate(), resetXForm(), setMatrix(), and xForm().

### void QPainter::translate ( const QPoint & offset )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Translates the coordinate system by the given offset.

### QRect QPainter::viewport () const

Returns the viewport rectangle.

See also setViewport() and setViewXForm().

### QRect QPainter::window () const

Returns the window rectangle.

See also setWindow() and setViewXForm().

## Related Non-Members

### void qDrawPlainRect ( QPainter * p, int x, int y, int w, int h, const QColor & c, int lineWidth = 1, const QBrush * fill = 0 )

`#include <qdrawutil.h>`

Draws the plain rectangle specified by (x, y, w, h) using the painter p.

The color argument c specifies the line color.

The lineWidth argument specifies the line width.

The rectangle's interior is filled with the fill brush unless fill is 0.

If you want to use a QFrame widget instead, you can make it display a plain rectangle, for example `QFrame::setFrameStyle( QFrame::Box | QFrame::Plain)`.

Warning: This function does not look at QWidget::style() or QApplication::style(). Use the drawing functions in QStyle to make widgets that follow the current GUI style.

See also qDrawShadeRect() and QStyle::drawPrimitive().

### void qDrawShadeLine ( QPainter * p, int x1, int y1, int x2, int y2, const QPalette & pal, bool sunken = true, int lineWidth = 1, int midLineWidth = 0 )

`#include <qdrawutil.h>`

Draws a horizontal (y1 == y2) or vertical (x1 == x2) shaded line using the painter p.

Nothing is drawn if y1 != y2 and x1 != x2 (i.e. the line is neither horizontal nor vertical).

The palette pal specifies the shading colors (light, dark and middle colors).

The line appears sunken if sunken is true, or raised if sunken is false.

The lineWidth argument specifies the line width for each of the lines. It is not the total line width.

The midLineWidth argument specifies the width of a middle line drawn in the QPalette::mid() color.

If you want to use a QFrame widget instead, you can make it display a shaded line, for example `QFrame::setFrameStyle( QFrame::HLine | QFrame::Sunken)`.

Warning: This function does not look at QWidget::style() or QApplication::style(). Use the drawing functions in QStyle to make widgets that follow the current GUI style.

See also qDrawShadeRect(), qDrawShadePanel(), and QStyle::drawPrimitive().

### void qDrawShadePanel ( QPainter * p, int x, int y, int w, int h, const QPalette & pal, bool sunken = false, int lineWidth = 1, const QBrush * fill = 0 )

`#include <qdrawutil.h>`

Draws the shaded panel specified by (x, y, w, h) using the painter p.

The palette pal specifies the shading colors (light, dark and middle colors).

The panel appears sunken if sunken is true, or raised if sunken is false.

The lineWidth argument specifies the line width.

The panel's interior is filled with the fill brush unless fill is 0.

If you want to use a QFrame widget instead, you can make it display a shaded panel, for example `QFrame::setFrameStyle( QFrame::Panel | QFrame::Sunken)`.

Warning: This function does not look at QWidget::style() or QApplication::style(). Use the drawing functions in QStyle to make widgets that follow the current GUI style.

See also qDrawWinPanel(), qDrawShadeLine(), qDrawShadeRect(), and QStyle::drawPrimitive().

### void qDrawShadeRect ( QPainter * p, int x, int y, int w, int h, const QPalette & pal, bool sunken = false, int lineWidth = 1, int midLineWidth = 0, const QBrush * fill = 0 )

`#include <qdrawutil.h>`

Draws the shaded rectangle specified by (x, y, w, h) using the painter p.

The paletted pal specifies the shading colors (light, dark and middle colors).

The rectangle appears sunken if sunken is true, or raised if sunken is false.

The lineWidth argument specifies the line width for each of the lines. It is not the total line width.

The midLineWidth argument specifies the width of a middle line drawn in the QPalette::mid() color.

The rectangle's interior is filled with the fill brush unless fill is 0.

If you want to use a QFrame widget instead, you can make it display a shaded rectangle, for example `QFrame::setFrameStyle( QFrame::Box | QFrame::Raised)`.

Warning: This function does not look at QWidget::style() or QApplication::style(). Use the drawing functions in QStyle to make widgets that follow the current GUI style.

QStyle::drawComplexControl()

See also qDrawShadeLine(), qDrawShadePanel(), qDrawPlainRect(), QStyle::drawItem(), and QStyle::drawControl().

### void qDrawWinButton ( QPainter * p, int x, int y, int w, int h, const QPalette & pal, bool sunken = false, const QBrush * fill = 0 )

`#include <qdrawutil.h>`

Draws the Windows-style button specified by (x, y, w, h) using the painter p.

The palette pal specifies the shading colors (light, dark and middle colors).

The button appears sunken if sunken is true, or raised if sunken is false.

The line width is 2 pixels.

The button's interior is filled with the `*`fill brush unless fill is 0.

Warning: This function does not look at QWidget::style() or QApplication::style(). Use the drawing functions in QStyle to make widgets that follow the current GUI style.

See also qDrawWinPanel() and QStyle::drawControl().

### void qDrawWinPanel ( QPainter * p, int x, int y, int w, int h, const QPalette & pal, bool sunken = false, const QBrush * fill = 0 )

`#include <qdrawutil.h>`

Draws the Windows-style panel specified by (x, y, w, h) using the painter p.

The palette pal specifies the shading colors.

The panel appears sunken if sunken is true, or raised if sunken is false.

The line width is 2 pixels.

The button's interior is filled with the fill brush unless fill is 0.

If you want to use a QFrame widget instead, you can make it display a shaded panel, for example `QFrame::setFrameStyle( QFrame::WinPanel | QFrame::Raised)`.

Warning: This function does not look at QWidget::style() or QApplication::style(). Use the drawing functions in QStyle to make widgets that follow the current GUI style.

See also qDrawShadePanel(), qDrawWinButton(), and QStyle::drawPrimitive().