⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qpainter.cpp

📁 qt-x11-opensource-src-4.1.4.tar.gz源码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
    painter. Then draw. Remember to destroy the QPainter object after    drawing. For example:    \code    void SimpleExampleWidget::paintEvent(QPaintEvent *)    {        QPainter painter(this);        painter.setPen(Qt::blue);        painter.setFont(QFont("Arial", 30));        painter.drawText(rect(), Qt::AlignCenter, "Qt");    }    \endcode    The core functionality of QPainter is drawing, but the class also    provide several functions that allows you to customize QPainter's    settings and its rendering quality, and others that enable    clipping. In addition you can control how different shapes are    merged together by specifying the painter's composition mode.    The isActive() function indicates whether the painter is active. A    painter is activated by the begin() function and the constructor    that takes a QPaintDevice argument. The end() function, and the    destructor, deactivates it.    Together with the QPaintDevice and QPaintEngine classes, QPainter    form the basis for Qt's paint system. QPainter is the class used    to perform drawing operations. QPaintDevice represents a device    that can be painted on using a QPainter. QPaintEngine provides the    interface that the painter uses to draw onto different types of    devices. If the painter is active, device() returns the paint    device on which the painter paints, and paintEngine() returns the    paint engine that the painter is currently operating on. For more    information, see \l {The Paint System} documentation.    Sometimes it is desirable to make someone else paint on an unusual    QPaintDevice. QPainter supports a static function to do this,    setRedirected().    \warning Unless a widget has the Qt::WA_PaintOutsidePaintEvent attribute    set. A QPainter can only be used on a widget inside a paintEvent() or a    function called by a paintEvent(). On Mac OS X, you can only paint on a    widget in a paintEvent() regardless of this attribute's setting.    \tableofcontents    \section1 Settings    There are several settings that you can customize to make QPainter    draw according to your preferences:    \list    \o font() is the font used for drawing text. If the painter        isActive(), you can retrieve information about the currently set        font, and its metrics, using the fontInfo() and fontMetrics()        functions respectively.    \o brush() defines the color or pattern that is used for filling       shapes.    \o pen() defines the color or stipple that is used for drawing       lines or boundaries.    \o backgroundMode() defines whether there is a background() or       not, i.e it is either \l {Qt::BGMode}{Qt::Opaque} or \l       {Qt::BGMode}{Qt::TransparentMode}.    \o background() only applies when backgroundMode() is \l       {Qt::BGMode}{Qt::Opaque} and pen() is a stipple. In that case, it       describes the color of the background pixels in the stipple.    \o brushOrigin() defines the origin of the tiled brushes, normally       the origin of widget's background.    \o viewport(), window(), matrix() make up the painter's coordinate        transformation system. For more information, see the \l        {Coordinate Transformations} section and the \l {The Coordinate        System} documentation.    \o hasClipping() tells whether the painter clips at all. (The paint       device clips, too.) If the painter clips, it clips to clipRegion().    \o layoutDirection() defines the layout direction used by the       painter when drawing text.    \o matrixEnabled() tells whether world transformation is enabled.    \o viewTransformEnabled() tells whether view transformation is        enabled.    \endlist    Note that some of these settings mirror settings in some paint    devices, e.g.  QWidget::font(). The QPainter::begin() function (or    equivalently the QPainter constructor) copies these attributes    from the paint device.    You can at any time save the QPainter's state by calling the    save() function which saves all the available settings on an    internal stack. The restore() function pops them back.    \section1 Drawing    QPainter provides functions to draw most primitives: drawPoint(),    drawPoints(), drawLine(), drawRect(), drawRoundRect(),    drawEllipse(), drawArc(), drawPie(), drawChord(), drawPolyline(),    drawPolygon(), drawConvexPolygon() and drawCubicBezier().  The two    convenience functions, drawRects() and drawLines(), draw the given    number of rectangles or lines in the given array of \l    {QRect}{QRects} or \l {QLine}{QLines} using the current pen and    brush.    The QPainter class also provides the fillRect() function which    fills the given QRect, with the given QBrush, and the eraseRect()    function that erases the area inside the given rectangle.    All of these functions have both integer and floating point    versions.    \table 100%    \row    \o \inlineimage qpainter-basicdrawing.png    \o    \bold {Basic Drawing Example}    The \l {painting/basicdrawing}{Basic Drawing} example shows how to    display basic graphics primitives in a variety of styles using the    QPainter class.    \endtable    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().    \table 100%    \row    \o    \bold {Painter Paths example}    The QPainterPath class provides a container for painting    operations, enabling graphical shapes to be constructed and    reused.    The \l {painting/painterpaths}{Painter Paths} example shows how    painter paths can be used to build complex shapes for rendering.    \o \inlineimage qpainter-painterpaths.png    \endtable    QPainter also provides the fillPath() function which fills the    given QPainterPath with the given QBrush, and the strokePath()    function that draws the outline of the given path (i.e. strokes    the path).    See also the \l {demos/deform}{Vector Deformation} demo which    shows how to use advanced vector techniques to draw text using a    QPainterPath, the \l {demos/gradients}{Gradients} demo which shows    the different types of gradients that are available in Qt, and the \l    {demos/pathstroke}{Path Stroking} demo which shows Qt's built-in    dash patterns and shows how custom patterns can be used to extend    the range of available patterns.    \table    \row    \o \inlineimage qpainter-vectordeformation.png    \o \inlineimage qpainter-gradients.png    \o \inlineimage qpainter-pathstroking.png    \header    \o \l {demos/deform}{Vector Deformation}    \o \l {demos/gradients}{Gradients}    \o \l {demos/pathstroke}{Path Stroking}    \endtable    There are functions to draw pixmaps/images, namely drawPixmap(),    drawImage() and drawTiledPixmap(). Both drawPixmap() and drawImage()    produce the same result, except that drawPixmap() is faster    on-screen while drawImage() may be faster on a QPrinter or other    devices.    Text drawing is done using drawText(). When you need    fine-grained positioning, boundingRect() tells you where a given    drawText() command will draw.    There is a drawPicture() function that draws the contents of an    entire QPicture. The drawPicture() function is the only function    that disregards all the painter's settings as QPicture has its own    settings.    \section1 Rendering Quality    To get the optimal rendering result using QPainter, you should use    the platform independent QImage as paint device; i.e. using QImage    will ensure that the result has an identical pixel representation    on any platform.    The QPainter class also provides a means of controlling the    rendering quality through its RenderHint enum and the support for    floating point precision: All the functions for drawing primitives    has a floating point version. These are often used in combination    with the \l {RenderHint}{QPainter::AntiAliasing} render hint.    \table 100%    \row    \o \inlineimage qpainter-concentriccircles.png    \o    \bold {Concentric Circles Example}    The \l {painting/concentriccircles}{Concentric Circles} example    shows the improved rendering quality that can be obtained using    floating point precision and anti-aliasing when drawing custom    widgets.    The application's main window displays several widgets which are    drawn using the various combinations of precision and    anti-aliasing.    \endtable    The RenderHint enum specifies flags to QPainter that may or may    not be respected by any given engine.  \l    {RenderHint}{QPainter::AntiAliasing} indicates that the engine    should antialias edges of primitives if possible, \l    {RenderHint}{QPainter::TextAntialiasing} indicates that the engine    should antialias text if possible, and finally the \l    {RenderHint}{QPainter::SmoothPixmapTransform} indicates that the    engine should use a smooth pixmap transformation algorithm.    The renderHints() function returns a flag that specifies the    rendering hints that are set for this painter.  Use the    setRenderHint() function to set or clear the currently set    RenderHints.    \section1 Coordinate Transformations    Normally, the QPainter operates on the device's own coordinate    system (usually pixels), but QPainter has good support for    coordinate transformations.    \table    \row    \o \inlineimage qpainter-clock.png    \o \inlineimage qpainter-rotation.png    \o \inlineimage qpainter-scale.png    \o \inlineimage qpainter-translation.png    \header    \o  nop \o rotate() \o scale() \o translate()    \endtable    The most commonly used transformations are scaling, rotation,    translation and shearing. Use the scale() function to scale the    coordinate system by a given offset, the rotate() function to    rotate it clockwise and translate() to translate it (i.e. adding a    given offset to the points). You can also twist the coordinate    system around the origin using the shear() function. See the \l    {demos/affine}{Affine Transformations} demo for a visualization of    a sheared coordinate system.    See also the \l {painting/transformations}{Transformations}    example which shows how transformations influence the way that    QPainter renders graphics primitives. In particular it shows how    the order of transformations affects the result.    \table 100%    \row    \o    \bold {Affine Transformations Demo}    The \l {demos/affine}{Affine Transformations} demo show Qt's    ability to perform affine transformations on painting    operations. The demo also allows the user to experiment with the    transformation operations and see the results immediately.    \o \inlineimage qpainter-affinetransformations.png    \endtable    All the tranformation operations operate on the transformation    matrix(). A matrix transforms a point in the plane to another    point. For more information about the transformation matrix, see    the \l {The Coordinate System} and QMatrix documentation.    The setMatrix() function can replace or add to the currently set    matrix(). The resetMatrix() function resets any transformations    that were made using translate(), scale(), shear(), rotate(),    setMatrix(), setViewport() and setWindow() functions. The    deviceMatrix() returns the matrix that transforms from logical    coordinates to device coordinates of the platform dependent paint    device. The latter function is only needed when using platform    painting commands on the platform dependent handle, and the    platform does not do transformations nativly.    When drawing with QPainter, we specify points using logical    coordinates which then are converted into the physical coordinates    of the paint device. The mapping of the logical coordinates to the    physical coordinates are handled by QPainter's viewport() and    window() and world matrix().  The viewport() represents the    physical coordinates specifying an arbitrary rectangle, the    window() describes the same rectangle in logical coordinates, and    the world matrix() is identical with the transformation matrix.    See also \l {The Coordinate System} documentation.    \section1 Clipping    QPainter can clip any drawing operation to a rectangle, a region,    or a vector path. The current clip is available using the    functions clipRegion() and clipPath(). Whether paths or regions are    preferred (faster) depends on the underlying paintEngine(). For    example, the QImage paint engine prefers paths while the X11 paint    engine prefers regions. Setting a clip is done in the painters    logical coordinates.    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().    \section1 Composition Modes    \target Composition Modes    QPainter provides the CompositionMode enum which defines the    Porter-Duff rules for digital image compositing; it describes a    model for combining the pixels in one image, the source, with the    pixel in another image, the destination.    The two most common forms of composition are \l    {QPainter::CompositionMode}{Source} and \l    {QPainter::CompositionMode}{SourceOver}.  \l    {QPainter::CompositionMode}{Source} is used to draw opaque objects    onto a paint device. In this mode, each pixel in the source    replaces the corresponding pixel in the destination. In \l    {QPainter::CompositionMode}{SourceOver} composition mode, the    source object is transparent and is drawn on top of the    destination.    Note that composition transformation operates pixelwise. For that    reason, there is a difference between using the grahic primitive    itself and its bounding rectangle: The bounding rect contains    pixels with alpha == 0 (i.e the pixels surrounding the    primitive). These pixels will overwrite the other image's pixels,    affectively clearing those, while the primitive only overwrites    its own area.    \table 100%    \row    \o \inlineimage qpainter-compositiondemo.png    \o    \bold {Composition Modes Demo}    The \l {demos/composition}{Composition Modes} demo, available in    Qt's demo directory, allows you to experiment with the various    composition modes and see the results immediately.    \endtable    \sa QPaintDevice, QPaintEngine, {QtSvg Module}*//*!    \enum QPainter::RenderHint    Renderhints are used to specify flags to QPainter that may or    may not be respected by any given engine.    \value Antialiasing Indicates that the engine should antialias    edges of primitives if possible.    \value TextAntialiasing Indicates that the engine should antialias    text if possible.    \value SmoothPixmapTransform Indicates that the engine should use    a smooth pixmap transformation algorithm (such as bilinear) rather    than nearest neighbor.    \sa renderHints(), setRenderHint(), {QPainter#Rendering    Quality}{Rendering Quality}*//*!    Constructs a painter.    \sa begin(), end()*/QPainter::QPainter(){    d_ptr = new QPainterPrivate(this);    d_ptr->init();}/*!    \fn QPainter::QPainter(QPaintDevice *device)    Constructs a painter that begins painting the paint \a device    immediately.    This constructor is convenient for short-lived painters, e.g. in a    QWidget::paintEvent() 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():    \code        void MyWidget::paintEvent(QPaintEvent *)        {            QPainter p;            p.begin(this);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -