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, Q3Painter, and QStylePainter.

Public Types

Public Functions

Related Non-Members


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:

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:

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 while drawImage() may be faster on QPrinter and other devices.

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::RTLright to left
QPainter::LTRleft 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:

FlagMeaning
Qt::AlignAutoaligns according to the language, usually left.
Qt::AlignLeftaligns to the left border.
Qt::AlignRightaligns to the right border.
Qt::AlignHCenteraligns horizontally centered.
Qt::AlignTopaligns to the top border.
Qt::AlignBottomaligns to the bottom border.
Qt::AlignVCenteraligns vertically centered.
Qt::AlignCenter(== Qt::AlignHCenter | Qt::AlignVCenter).
Qt::TextSingleLineignores newline characters in the text.
Qt::TextExpandTabsexpands tabs.
Qt::TextShowMnemonicinterprets "&x" as "<u>x</u>".
Qt::TextWordBreakbreaks 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.

QRectF QPainter::boundingRect ( const QRectF & bounds, int flags, const QString & str, int len = -1 )

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

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().

QPainterPath QPainter::clipPath () const

Returns the currently clip as a path. Note that the clip path is given in logical coordinates and subject to coordinate transformation

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().

const QMatrix & QPainter::deviceMatrix () const

Returns the matrix that transforms from logical coordinates to device coordinates of the platform dependent paintdevice.

This function is ONLY needed when using platform painting commands on the platform dependent handle, and the platform does not do transformations nativly.

See also matrix() and QPaintEngine::hasFeature().

void QPainter::drawArc ( const QRectF & 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 ( const QRect & , int a, int alen )

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

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 QRectF & 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 ( const QRect & , int a, int alen )

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

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 QPolygon & polygon, int index = 0, int npoints = -1 )

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

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

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::drawEllipse ( const QRectF & r )

Draws the ellipse that fits inside the rectangle r.

A filled ellipse has a size of r.size(). An stroked ellipse has a size of r.size() plus the pen width.

void QPainter::drawEllipse ( const QRect & r )

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

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 that fits inside the rectangle r.

void QPainter::drawImage ( const QRectF & targetRect, const QImage & image, const QRectF & sourceRect, Qt::ImageConversionFlags flags = Qt::AutoColor )

Draws the rectanglular portion sourceRect, of image image, into rectangle targetRect in the paint device.

If the image needs to be modified to fit in a lower-resolution result (e.g. converting from 32-bit to 8-bit), use the conversionFlags to specify how you'd prefer this to happen.

See also Qt::ImageConversionFlags.

void QPainter::drawImage ( const QPointF & p, const QImage & image, const QRectF & sr, Qt::ImageConversionFlags conversionFlags = Qt::AutoColor )

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

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

If the image needs to be modified to fit in a lower-resolution result (e.g. converting from 32-bit to 8-bit), use the conversionFlags to specify how you'd prefer this to happen.

See also Qt::ImageConversionFlags.

void QPainter::drawImage ( const QRectF & rectangle, const QImage & image )

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

Draws image into rectangle.

void QPainter::drawImage ( const QPoint & p, const QImage & image )

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

Draws the image i at point p.

void QPainter::drawImage ( const QPointF & p, const QImage & image )

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

void QPainter::drawImage ( const QPoint & p, const QImage & image, const QRect & sr, Qt::ImageConversionFlags conversionFlags = Qt::AutoColor )

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

void QPainter::drawImage ( const QRect & rectangle, const QImage & image )

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

void QPainter::drawImage ( int x, int y, const QImage & image, int sx = 0, int sy = 0, int sw = -1, int sh = -1, Qt::ImageConversionFlags conversionFlags = Qt::AutoColor )

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

Draws an image at (x, y) by copying a part of image 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 image that is to be drawn. The default is (0, 0).

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

void QPainter::drawImage ( const QRect & targetRect, const QImage & image, const QRect & sourceRect, Qt::ImageConversionFlags flags = Qt::AutoColor )

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

void QPainter::drawLine ( const QLineF & l )

Draws a line from point p1 to point p2.

See also pen().

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

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

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

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

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::drawLines ( const QList<QLineF> lines, int index = 0, int nlines = -1 )

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 QPointF & 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 ( const QPoint & p, const QPicture & picture )

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

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 QRectF & 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 ( const QRect & , int a, int alen )

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

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 QRectF & r, const QPixmap & pm, const QRectF & 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 ( const QRect & targetRect, const QPixmap & pixmap, const QRect & sourceRect, Qt::PixmapDrawingMode mode = Qt::ComposePixmap )

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

void QPainter::drawPixmap ( const QPointF & p, const QPixmap & pm, const QRectF & sr, Qt::PixmapDrawingMode mode = Qt::ComposePixmap )

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

void QPainter::drawPixmap ( const QPointF & 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.

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 QPointF & p )

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

See also QPen.

void QPainter::drawPoint ( const QPoint & p )

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

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 QList<QPointF> & points )

void QPainter::drawPoints ( const QPointArray & pa )

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

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

See also QPen.

void QPainter::drawPolygon ( const QPolygon & polygon, Qt::FillRule fillRule = Qt::OddEvenFill, int index = 0, int npoints = -1 )

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 fillRule is Qt::WindingFill, the polygon is filled using the winding fill algorithm. If fillRule is Qt::OddEvenFill, the polygon is filled using the odd-even fill algorithm. See Qt::FillRule for a more detailed description of these fill rules.

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

void QPainter::drawPolygon ( const QPointArray & pa, Qt::FillRule fillRule = Qt::OddEvenFill, int index = 0, int npoints = -1 )

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

void QPainter::drawPolyline ( const QPolygon & 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::drawPolyline ( const QPointArray & pa, int index = 0, int npoints = -1 )

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

void QPainter::drawRect ( const QRectF & r )

Draws the rectangle \r with the current pen and brush.

A filled rectangle has a size of r.size(). A stroked rectangle has a size of r.size() plus the pen width.

void QPainter::drawRect ( const QRect & rect )

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

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 QRectF & 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.

A filled rectangle has a size of r.size(). A stroked rectangle has a size of r.size() plus the pen width.

See also drawRect() and QPen.

void QPainter::drawRoundRect ( const QRect & r, int xround = 25, int yround = 25 )

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

void QPainter::drawRoundRect ( int x, int y, int w, int h, int xround = 25, int yround = 25 )

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

void QPainter::drawText ( const QRectF & , int flags, const QString & , int len = -1, QRectF * br = 0 )

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

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

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

See also QPainter::TextDirection.

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

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

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::drawTextItem ( const QPoint & p, const QTextItem & ti )

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

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 & , const QPixmap & , const QPoint & = QPoint(), Qt::PixmapDrawingMode mode = Qt::ComposePixmap )

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

void QPainter::drawTiledPixmap ( const QRectF & r, const QPixmap & pixmap, const QPointF & sp = QPointF(), 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 QRectF & r )

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

void QPainter::eraseRect ( const QRect & )

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

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 QRectF & 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 ( const QRect & , const QBrush & )

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

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().

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().

QPaintEngine * QPainter::paintEngine () const

Returns the paint engine that the painter is currently operating on, if the painter is active; otherwise 0.

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

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 & )

void QPainter::setBrushOrigin ( const QPointF & p )

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

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, Qt::ClipOperation op = Qt::ReplaceClip )

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 ( const QRect & , Qt::ClipOperation op = Qt::ReplaceClip )

void QPainter::setClipRect ( int x, int y, int w, int h, Qt::ClipOperation op = Qt::ReplaceClip )

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

void QPainter::setClipRect ( const QRectF & rect, Qt::ClipOperation op = Qt::ReplaceClip )

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, Qt::ClipOperation op = Qt::ReplaceClip )

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:

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::setRenderHint ( RenderHint hint, bool on = true )

Sets the render hint hint on this painter if on is true; otherwise clears the render hint.

void QPainter::setViewTransformEnabled ( bool enable )

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

See also viewTransformEnabled(), 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(), setViewTransformEnabled(), 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(), setViewTransformEnabled(), 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 ( const QPointF & offset )

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

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

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.

bool QPainter::viewTransformEnabled () const

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

See also setViewTransformEnabled() and matrix().

QRect QPainter::viewport () const

Returns the viewport rectangle.

See also setViewport() and setViewTransformEnabled().

QRect QPainter::window () const

Returns the window rectangle.

See also setWindow() and setViewTransformEnabled().


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().


Copyright © 2004 Trolltech Trademarks
Qt 4.0.0-b1