QPainter
Section: Misc. Reference Manual Pages (3qt)
Updated: 2 February 2007
Index
Return to Main Contents
NAME
QPainter - Does low-level painting e.g. on widgets
SYNOPSIS
#include <qpainter.h>
Inherits Qt.
Inherited by QDirectPainter.
Public Members
enum CoordinateMode { CoordDevice, CoordPainter }
QPainter ()
QPainter ( const QPaintDevice * pd, bool unclipped = FALSE )
QPainter ( const QPaintDevice * pd, const QWidget * copyAttributes, bool unclipped = FALSE )
~QPainter ()
bool begin ( const QPaintDevice * pd, bool unclipped = FALSE )
bool begin ( const QPaintDevice * pd, const QWidget * copyAttributes, bool unclipped = FALSE )
bool end ()
QPaintDevice * device () const
bool isActive () const
void flush ( const QRegion & region, CoordinateMode cm = CoordDevice )
void flush ()
void save ()
void restore ()
QFontMetrics fontMetrics () const
QFontInfo fontInfo () const
const QFont & font () const
void setFont ( const QFont & font )
const QPen & pen () const
void setPen ( const QPen & pen )
void setPen ( PenStyle style )
void setPen ( const QColor & color )
const QBrush & brush () const
void setBrush ( const QBrush & brush )
void setBrush ( BrushStyle style )
void setBrush ( const QColor & color )
QPoint pos () const (obsolete)
const QColor & backgroundColor () const
void setBackgroundColor ( const QColor & c )
BGMode backgroundMode () const
void setBackgroundMode ( BGMode m )
RasterOp rasterOp () const
void setRasterOp ( RasterOp r )
const QPoint & brushOrigin () const
void setBrushOrigin ( int x, int y )
void setBrushOrigin ( const QPoint & p )
bool hasViewXForm () const
bool hasWorldXForm () const
void setViewXForm ( bool enable )
QRect window () const
void setWindow ( const QRect & r )
void setWindow ( int x, int y, int w, int h )
QRect viewport () const
void setViewport ( const QRect & r )
void setViewport ( int x, int y, int w, int h )
void setWorldXForm ( bool enable )
const QWMatrix & worldMatrix () const
void setWorldMatrix ( const QWMatrix & m, bool combine = FALSE )
void saveWorldMatrix () (obsolete)
void restoreWorldMatrix () (obsolete)
void scale ( double sx, double sy )
void shear ( double sh, double sv )
void rotate ( double a )
void translate ( double dx, double dy )
void resetXForm ()
QPoint xForm ( const QPoint & pv ) const
QRect xForm ( const QRect & rv ) const
QPointArray xForm ( const QPointArray & av ) const
QPointArray xForm ( const QPointArray & av, int index, int npoints ) const
QPoint xFormDev ( const QPoint & pd ) const
QRect xFormDev ( const QRect & rd ) const
QPointArray xFormDev ( const QPointArray & ad ) const
QPointArray xFormDev ( const QPointArray & ad, int index, int npoints ) const
void setClipping ( bool enable )
bool hasClipping () const
QRegion clipRegion ( CoordinateMode m = CoordDevice ) const
void setClipRect ( const QRect & r, CoordinateMode m = CoordDevice )
void setClipRect ( int x, int y, int w, int h, CoordinateMode m = CoordDevice )
void setClipRegion ( const QRegion & rgn, CoordinateMode m = CoordDevice )
void drawPoint ( int x, int y )
void drawPoint ( const QPoint & p )
void drawPoints ( const QPointArray & a, int index = 0, int npoints = -1 )
void moveTo ( int x, int y ) (obsolete)
void moveTo ( const QPoint & p ) (obsolete)
void lineTo ( int x, int y ) (obsolete)
void lineTo ( const QPoint & p ) (obsolete)
void drawLine ( int x1, int y1, int x2, int y2 )
void drawLine ( const QPoint & p1, const QPoint & p2 )
void drawRect ( int x, int y, int w, int h )
void drawRect ( const QRect & r )
void drawWinFocusRect ( int x, int y, int w, int h )
void drawWinFocusRect ( int x, int y, int w, int h, const QColor & bgColor )
void drawWinFocusRect ( const QRect & r )
void drawWinFocusRect ( const QRect & r, const QColor & bgColor )
void drawRoundRect ( int x, int y, int w, int h, int xRnd = 25, int yRnd = 25 )
void drawRoundRect ( const QRect & r, int xRnd = 25, int yRnd = 25 )
void drawEllipse ( int x, int y, int w, int h )
void drawEllipse ( const QRect & r )
void drawArc ( int x, int y, int w, int h, int a, int alen )
void drawArc ( const QRect & r, int a, int alen )
void drawPie ( int x, int y, int w, int h, int a, int alen )
void drawPie ( const QRect & r, int a, int alen )
void drawChord ( int x, int y, int w, int h, int a, int alen )
void drawChord ( const QRect & r, int a, int alen )
void drawLineSegments ( const QPointArray & a, int index = 0, int nlines = -1 )
void drawPolyline ( const QPointArray & a, int index = 0, int npoints = -1 )
void drawPolygon ( const QPointArray & a, bool winding = FALSE, int index = 0, int npoints = -1 )
void drawConvexPolygon ( const QPointArray & pa, int index = 0, int npoints = -1 )
void drawCubicBezier ( const QPointArray & a, int index = 0 )
void drawPixmap ( int x, int y, const QPixmap & pixmap, int sx = 0, int sy = 0, int sw = -1, int sh = -1 )
void drawPixmap ( const QPoint & p, const QPixmap & pm, const QRect & sr )
void drawPixmap ( const QPoint & p, const QPixmap & pm )
void drawPixmap ( const QRect & r, const QPixmap & pm )
void drawImage ( int x, int y, const QImage & image, int sx = 0, int sy = 0, int sw = -1, int sh = -1, int conversionFlags = 0 )
void drawImage ( const QPoint &, const QImage &, const QRect & sr, int conversionFlags = 0 )
void drawImage ( const QPoint & p, const QImage & i, int conversion_flags = 0 )
void drawImage ( const QRect & r, const QImage & i )
void drawTiledPixmap ( int x, int y, int w, int h, const QPixmap & pixmap, int sx = 0, int sy = 0 )
void drawTiledPixmap ( const QRect & r, const QPixmap & pm, const QPoint & sp )
void drawTiledPixmap ( const QRect & r, const QPixmap & pm )
void drawPicture ( const QPicture & pic ) (obsolete)
void drawPicture ( int x, int y, const QPicture & pic )
void drawPicture ( const QPoint & p, const QPicture & pic )
void fillRect ( int x, int y, int w, int h, const QBrush & brush )
void fillRect ( const QRect & r, const QBrush & brush )
void eraseRect ( int x, int y, int w, int h )
void eraseRect ( const QRect & r )
enum TextDirection { Auto, RTL, LTR }
void drawText ( int x, int y, const QString &, int len = -1, TextDirection dir = Auto )
void drawText ( const QPoint &, const QString &, int len = -1, TextDirection dir = Auto )
void drawText ( int x, int y, const QString &, int pos, int len, TextDirection dir = Auto )
void drawText ( const QPoint & p, const QString &, int pos, int len, TextDirection dir = Auto )
void drawText ( int x, int y, int w, int h, int flags, const QString &, int len = -1, QRect * br = 0, QTextParag ** internal = 0 )
void drawText ( const QRect & r, int tf, const QString & str, int len = -1, QRect * brect = 0, QTextParag ** internal = 0 )
QRect boundingRect ( int x, int y, int w, int h, int flags, const QString &, int len = -1, QTextParag ** intern = 0 )
QRect boundingRect ( const QRect & r, int flags, const QString & str, int len = -1, QTextParag ** internal = 0 )
int tabStops () const
void setTabStops ( int ts )
int * tabArray () const
void setTabArray ( int * ta )
HDC handle () const
Static Public Members
void redirect ( QPaintDevice * pdev, QPaintDevice * replacement )
RELATED FUNCTION DOCUMENTATION
void qDrawShadeLine ( QPainter * p, int x1, int y1, int x2, int y2, const QColorGroup & g, bool sunken, int lineWidth, int midLineWidth )
void qDrawShadeRect ( QPainter * p, int x, int y, int w, int h, const QColorGroup & g, bool sunken, int lineWidth, int midLineWidth, const QBrush * fill )
void qDrawShadePanel ( QPainter * p, int x, int y, int w, int h, const QColorGroup & g, bool sunken, int lineWidth, const QBrush * fill )
void qDrawWinButton ( QPainter * p, int x, int y, int w, int h, const QColorGroup & g, bool sunken, const QBrush * fill )
void qDrawWinPanel ( QPainter * p, int x, int y, int w, int h, const QColorGroup & g, bool sunken, const QBrush * fill )
void qDrawPlainRect ( QPainter * p, int x, int y, int w, int h, const QColor & c, int lineWidth, const QBrush * fill )
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(), AlignCenter, "The Text" );
}
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.
-
-
- rasterOp() is how pixels drawn interact with the pixels already there.
-
-
- brushOrigin() is the origin of the tiled brushes, normally the origin of the window.
-
-
- viewport(), window(), worldMatrix() 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(), drawWinFocusRect(), drawRoundRect(), drawEllipse(), drawArc(), drawPie(), drawChord(), drawLineSegments(), 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.
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 worldMatrix(). setWorldMatrix() can replace or add to the currently set worldMatrix().
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: Note that QPainter does not attempt to work around coordinate limitations in the underlying window system. Some platforms may behave incorrectly with coordinates as small as +/-4000.
See also QPaintDevice, QWidget, QPixmap, QPrinter, QPicture, Application Walkthrough, Coordinate System Overview, Graphics Classes, and Image Processing Classes.
Member Type Documentation
QPainter::CoordinateMode
- QPainter::CoordDevice
-
- QPainter::CoordPainter
-
See also clipRegion().
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 ( const QPaintDevice * pd, bool unclipped = FALSE )
Constructs a painter that begins painting the paint device pd immediately. Depending on the underlying graphic system the painter will paint over children of the paintdevice if unclipped is TRUE.
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 ( const QPaintDevice * pd, const QWidget * copyAttributes, bool unclipped = FALSE )
Constructs a painter that begins painting the paint