QPainter Class Reference


The QPainter class paints on paint devices. (details) (complete member list)

#include <qpainter.h>

Public Members

Static Public Members

Private Members


Detailed Description

The QPainter class paints on paint devices.

The painter provides graphics rendering on any QPaintDevice object.

Graphics can be transformed using view transformation, world transformation or a combination of these two. View transformation is a window/viewport transformation with translation and scaling. World transformation is a full 2D transformation including rotation and shearing.

The typical use of a painter is:

  1. Call begin() to begin painting on a device.
  2. Set a pen, a brush etc.
  3. Perform the drawing.
  4. Call end() to finish painting.

Example of use:

  QWidget  widget;
  QPainter paint;
  paint.begin( &widget );
  paint.setPen( blue );
  paint.drawText( 20, 20, 100, 100, AlignCenter, "The Text" );
  paint.end();

Member Function Documentation

QPainter::QPainter ()

Constructs a painter.

QPainter::~QPainter ()

Destroys the painter.

bool QPainter::begin (const QPaintDevice *pd)

Begins painting the paint device pd and returns TRUE if successful, or FALSE if it cannot begin painting.

Warning: This function resets the pen, brush, font and all other painter settings to default values for the painter device. That means any pen, brush etc. you set before calling begin() will be overwritten.

See also: end().

QRect QPainter::boundingRect (int x, int y, int w, int h, int tf, const char *str, int len = -1, char **internal=0)

Returns the bounding rectangle of the aligned text that would be printed with the corresponding drawText() function.

The tf text formatting is the bitwise OR of the following flags:

QRect QPainter::boundingRect (const QRect &r, int tf, const char *str, int len = -1, char **i=0)

Synonymous to boundingRect(x,y,w,h,...).

void QPainter::drawArc (int x, int y, int w, int h, int a1, int a2)

Draws an arc defined by the rectangle (x,y,w,h) and the start angle a1 and the end angle a2.

void QPainter::drawArc (const QRect &r, int a1, int a2)

Synonymous to drawArc(x,y,w,h,a1,a2).

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

Draws a Bezier curve defined by the npoints control points in a, starting at a[index].

void QPainter::drawChord (int x, int y, int w, int h, int a1, int a2)

Draws a chord defined by the rectangle (x,y,w,h) and the start angle a1 and the end angle a2.

void QPainter::drawChord (const QRect &r, int a1, int a2)

Synonymous to drawChord(x,y,w,h,a1,a2).

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

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

void QPainter::drawEllipse (const QRect &r)

Synonymous to drawEllipse(x,y,w,h).

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

Draws a line from (x1,y2) to (x2,y2).

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

Synonymous to drawLine(x1,y1,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].

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

void QPainter::drawPicture (const QPicture &pic)

Replays the picture pic.

This function does exactly the same as QPicture::play().

void QPainter::drawPie (int x, int y, int w, int h, int a1, int a2)

Draws a pie defined by the rectangle (x,y,w,h) and the start angle a1 and the end angle a2.

void QPainter::drawPie (const QRect &r, int a1, int a2)

Synonymous to drawPie(x,y,w,h,a1,a2).

void QPainter::drawPixmap (int x, int y, const QPixmap &pixmap)

Draws a pixmap at (x,y).

void QPainter::drawPixmap (const QPoint &p, const QPixmap &pm)

Synonymous to drawPixmap(x,y,pm).

void QPainter::drawPoint (const QPoint &p)

Synonymous to drawPoint(x,y).

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

Draws/plots a single point at (x,y) using the current pen.

void QPainter::drawPolygon (const QPointArray &a, bool winding=FALSE, int index=0, int npoints=-1)

Draws the polygon defined by the npoints points in a starting at a[index].

The first point is connected to the last point.

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].

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

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

The width and height include both lines.

void QPainter::drawRect (const QRect &r)

Synonymous to drawRect(x,y,w,h).

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

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

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

The width and height include both lines.

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

Synonymous to drawRoundRect(x,y,w,h,xRnd,yRnd).

void QPainter::drawShadeLine (int x1, int y1, int x2, int y2, const QColor &tColor, const QColor &bColor, int lw=1, const QColor &mColor=black, int mlw=0)

Draw a horizontal (y1 == y2) or vertical (x1 == x2) shaded line.

The tColor argument specifies the top (or left) color and bColor specifies the bottom (or right) color.

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

The mw argument specifies the width of a middle line drawn in mColor.

If tColor is brighter that bColor, the line appears to be raised from the surface. If tColor is darker that bColor, the line appears to be sunken into the surface.

See also: drawShadeRect() and drawShadePanel().

void QPainter::drawShadeLine (const QPoint &p1, const QPoint &p2, const QColor &tc, const QColor &bc, int lw=1, const QColor &mc=black, int mlw=0)

Synonymous to drawShadeLine(x1,y1,x2,y2,...).

void QPainter::drawShadePanel (int x, int y, int w, int h, const QColor &tColor, const QColor &bColor, int lw=1, const QColor &fColor=black, bool fill=FALSE)

Draw a shaded panel given by (x,y,w,h).

void QPainter::drawShadePanel (const QRect &r, const QColor &tc, const QColor &bc, int lw=1, const QColor &fc=black, bool fill=FALSE)

Synonymous to drawShadePanel(x1,y1,x2,y2,...).

void QPainter::drawShadeRect (int x, int y, int w, int h, const QColor &tColor, const QColor &bColor, int lw=1, const QColor &mColor=black, int mlw=0)

Draw a shaded rectangle given by (x,y,w,h).

The arguments have the same meaning as for drawShadeLine().

See also: drawShadeLine(), drawShadePanel().

void QPainter::drawShadeRect (const QRect &r, const QColor &tc, const QColor &bc, int lw=1, const QColor &mc=black, int mlw=0)

Synonymous to drawShadeRect(x1,y1,x2,y2,...).

void QPainter::drawText (int x, int y, const char *str, int len = -1)

Draws at most len characters from str at position (x,y).

(x,y) is the base line position.

void QPainter::drawText (int x, int y, int w, int h, int tf, const char *str, int len = -1, QRect *brect=0, char **internal=0)

Draws at most len characters from str in the rectangle (x,y,w,h).

This function draws formatted text.

The tf text formatting is the bitwise OR of the following flags:

void QPainter::drawText (const QPoint &p, const char *s, int len = -1)

Synonymous to drawText(x,y,...).

void QPainter::drawText (const QRect &r, int tf, const char *str, int len = -1, QRect *br=0, char **i=0)

Synonymous to drawText(x,y,w,h,...).

bool QPainter::end ()

End painting. Any resources used while painting are released.

See also: begin().

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

Calls fillRect( x, y, w, h, backgroundColor() )

void QPainter::eraseRect (const QRect &r)

Synonymous to eraseRect(x,y,w,h).

void QPainter::fillRect (int x, int y, int w, int h, const QColor &color)

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

void QPainter::fillRect (const QRect &r, const QColor &c)

Synonymous to fillRect(x,y,w,h,c).

QFont & QPainter::font ()

Returns the current painter font.

void QPainter::lineTo (const QPoint &p)

Synonymous to lineTo(x,y).

void QPainter::lineTo (int x, int y)

Draws a line from the current point to (x,y) and sets this to the new current point.

See also: moveTo().

void QPainter::moveTo (const QPoint &p)

Synonymous to moveTo(x,y).

void QPainter::moveTo (int x, int y)

Sets the current point.

See also: lineTo().

void QPainter::redirect (QPaintDevice *pdev, QPaintDevice *replacement)

Redirects all paint command for a paint device pdev to another paint device replacement.

A redirected paint device is reset if replacement is 0.

The following example draws a widget in a pixmap:

  QPixmap pm( someWidget->width(), someWidget->height() );
  pm.fill( someWidget->backgroundColor() );
  QPainter::redirect( someWidget, &pm );
  someWidget->repaint();
  QPainter::redirect( someWidget, 0 );

void QPainter::restore ()

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

See also: save().

void QPainter::save ()

Saves the current painter state (pushes the state onto a stack).

See also: restore().

void QPainter::setBackgroundColor (const QColor &c)

Sets the background color of the painter to c.

See also: backgroundColor().

void QPainter::setBackgroundMode (BGMode m)

Sets the background mode of the painter to m, which must be either

The m parameter must be one of:

See also: backgroundMode().

void QPainter::setBrush (const QBrush &brush)

Sets a new painter brush.

The brush defines how to fill shapes.

void QPainter::setBrush (BrushStyle style)

Sets a new painter brush with black color and the specified style.

The brush defines how to fill shapes.

void QPainter::setBrush (const QColor &color)

Sets a new painter brush with the style SolidPattern and the specified color.

The brush defines how to fill shapes.

void QPainter::setBrushOrigin (const QPoint &p)

Synonymous to setBrushOrigin(x,y).

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

Sets the brush origin to (x,y).

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

Clips graphics to the rectangle defined by x,\e y,w and h.

Clipping is automatically enabled.

See also: setClipRegion().

void QPainter::setClipRect (const QRect &rect)

Synonymous to setClipRect(x,y,w,h).

void QPainter::setClipRegion (const QRegion &rgn)

Clips graphics to the region rgn.

Clipping is automatically enabled.

See also: setClipRect().

void QPainter::setClipping (bool enable)

Enables clipping if enable is TRUE, or disables clipping if enable is FALSE.

void QPainter::setFont (const QFont &font)

Sets a new painter font.

void QPainter::setPen (const QPen &pen)

Sets a new painter pen.

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

void QPainter::setPen (PenStyle style)

Sets a new painter pen with black color, width 0 and the specified style.

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

void QPainter::setPen (const QColor &color)

Sets a new painter pen with style SolidLine, width 0 the specified color.

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

void QPainter::setRasterOp (RasterOp r)

Sets the raster operation to r.

The r parameter must be one of:

See also: rasterOp().

void QPainter::setTabArray (int *ta)

Set an array containing the tab stops.

The last tab stop must be 0 (terminates the array).

See also: setTabStops().

void QPainter::setTabStops (int ts)

Set the number of pixels per tab stop to a fixed number.

Tab stops are used when drawing formatted text with ExpandTabs set.

See also: setTabArray().

void QPainter::setViewXForm (bool enable)

Enables or disables view transformations.

void QPainter::setViewport (const QRect &r)

Synonymous to setViewport(x,y,w,h).

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. View transformations can be combined with world transformations.

See also: viewport(), setViewport() and setWorldMatrix().

void QPainter::setWindow (const QRect &r)

Synonymous to setWindow(x,y,w,h).

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. View transformations can be combined with world transformations.

See also: window(), setViewport() and setWorldMatrix().

void QPainter::setWorldMatrix (const Q2DMatrix &m, bool combine=FALSE)

Sets the world transformation matrix to m.

If combine is TRUE, then m is combined with the current transformation matrix.

See also: worldMatrix(), setWindow() and setViewport().

void QPainter::setWorldXForm (bool enable)

Enables or disables world transformations.

QRect QPainter::viewport () const

Returns the viewport rectangle.

See also: setViewport().

QRect QPainter::window () const

Returns the window rectangle.

See also: setWindow().

Q2DMatrix QPainter::worldMatrix () const

Returns the world transformation matrix.

QPoint QPainter::xForm (const QPoint &pv) const

Returns the point pv transformed from user coordinates to device coordinates.

QRect QPainter::xForm (const QRect &rv) const

Returns the rectangle rv transformed from user coordinates to device coordinates.

If world transformation is enabled and rotation or shearing is used, then the bounding rectangle will be returned.

QPointArray QPainter::xForm (const QPointArray &av) const

Returns the point array av transformed from user coordinates to device coordinates.

QPoint QPainter::xFormDev (const QPoint &pd) const

Returns the point pv transformed from device coordinates to user coordinates.

QRect QPainter::xFormDev (const QRect &rd) const

Returns the rectangle rv transformed from device coordinates to user coordinates.

If world transformation is enabled and rotation or shearing is used, then the bounding rectangle will be returned.

QPointArray QPainter::xFormDev (const QPointArray &ad) const

Returns the point array av transformed from device coordinates to user coordinates.


This file is part of the Qt toolkit, copyright 1995 Troll Tech, all rights reserved.

It was generated from the following files:


Generated at 16:17, 1995/06/30 by the webmaster at Troll Tech