QPainter Class

QPainter Class

The QPainter class performs low-level painting on widgets and other paint devices. 

Header: #include <QPainter>
qmake: QT += gui
Inherited By:

QStylePainter

 

 

 

 

 

Public Types

class PixmapFragment
enum CompositionMode { CompositionMode_SourceOver, CompositionMode_DestinationOver, CompositionMode_Clear, CompositionMode_Source, CompositionMode_Destination, …, RasterOp_SourceOrNotDestination }
enum PixmapFragmentHint { OpaqueHint }
flags PixmapFragmentHints
enum RenderHint { Antialiasing, TextAntialiasing, SmoothPixmapTransform, HighQualityAntialiasing, NonCosmeticDefaultPen, …, LosslessImageRendering }
flags RenderHints

Public Functions

Public Functions

  QPainter(QPaintDevice *device)
  QPainter()
  ~QPainter()
const QBrush & background() const
Qt::BGMode backgroundMode() const
bool begin(QPaintDevice *device)
void beginNativePainting()
QRectF boundingRect(const QRectF &rectangle, int flags, const QString &text)
QRect boundingRect(const QRect &rectangle, int flags, const QString &text)
QRect boundingRect(int x, int y, int w, int h, int flags, const QString &text)
QRectF boundingRect(const QRectF &rectangle, const QString &text, const QTextOption &option = QTextOption())
const QBrush & brush() const
QPoint brushOrigin() const
QRectF clipBoundingRect() const
QPainterPath clipPath() const
QRegion clipRegion() const
QTransform combinedTransform() const
QPainter::CompositionMode compositionMode() const
QPaintDevice * device() const
const QTransform & deviceTransform() const
void drawArc(const QRectF &rectangle, int startAngle, int spanAngle)
void drawArc(const QRect &rectangle, int startAngle, int spanAngle)
void drawArc(int x, int y, int width, int height, int startAngle, int spanAngle)
void drawChord(const QRectF &rectangle, int startAngle, int spanAngle)
void drawChord(int x, int y, int width, int height, int startAngle, int spanAngle)
void drawChord(const QRect &rectangle, int startAngle, int spanAngle)
void drawConvexPolygon(const QPointF *points, int pointCount)
void drawConvexPolygon(const QPolygonF &polygon)
void drawConvexPolygon(const QPoint *points, int pointCount)
void drawConvexPolygon(const QPolygon &polygon)
void drawEllipse(const QRectF &rectangle)
void drawEllipse(const QRect &rectangle)
void drawEllipse(int x, int y, int width, int height)
void drawEllipse(const QPointF &center, qreal rx, qreal ry)
void drawEllipse(const QPoint &center, int rx, int ry)
void drawGlyphRun(const QPointF &position, const QGlyphRun &glyphs)
void drawImage(const QRectF &target, const QImage &image, const QRectF &source, Qt::ImageConversionFlags flags = Qt::AutoColor)
void drawImage(const QRect &target, const QImage &image, const QRect &source, Qt::ImageConversionFlags flags = Qt::AutoColor)
void drawImage(const QPointF &point, const QImage &image, const QRectF &source, Qt::ImageConversionFlags flags = Qt::AutoColor)
void drawImage(const QPoint &point, const QImage &image, const QRect &source, Qt::ImageConversionFlags flags = Qt::AutoColor)
void drawImage(const QRectF &rectangle, const QImage &image)
void drawImage(const QRect &rectangle, const QImage &image)
void drawImage(const QPointF &point, const QImage &image)
void drawImage(const QPoint &point, const QImage &image)
void drawImage(int x, int y, const QImage &image, int sx = 0, int sy = 0, int sw = -1, int sh = -1, Qt::ImageConversionFlags flags = Qt::AutoColor)
void drawLine(const QLineF &line)
void drawLine(const QLine &line)
void drawLine(int x1, int y1, int x2, int y2)
void drawLine(const QPoint &p1, const QPoint &p2)
void drawLine(const QPointF &p1, const QPointF &p2)
void drawLines(const QLineF *lines, int lineCount)
void drawLines(const QVector<QLineF> &lines)
void drawLines(const QPointF *pointPairs, int lineCount)
void drawLines(const QVector<QPointF> &pointPairs)
void drawLines(const QLine *lines, int lineCount)
void drawLines(const QVector<QLine> &lines)
void drawLines(const QPoint *pointPairs, int lineCount)
void drawLines(const QVector<QPoint> &pointPairs)
void drawPath(const QPainterPath &path)
void drawPicture(const QPointF &point, const QPicture &picture)
void drawPicture(int x, int y, const QPicture &picture)
void drawPicture(const QPoint &point, const QPicture &picture)
void drawPie(const QRectF &rectangle, int startAngle, int spanAngle)
void drawPie(int x, int y, int width, int height, int startAngle, int spanAngle)
void drawPie(const QRect &rectangle, int startAngle, int spanAngle)
void drawPixmap(const QRectF &target, const QPixmap &pixmap, const QRectF &source)
void drawPixmap(const QRect &target, const QPixmap &pixmap, const QRect &source)
void drawPixmap(int x, int y, int w, int h, const QPixmap &pixmap, int sx, int sy, int sw, int sh)
void drawPixmap(int x, int y, const QPixmap &pixmap, int sx, int sy, int sw, int sh)
void drawPixmap(const QPointF &point, const QPixmap &pixmap, const QRectF &source)
void drawPixmap(const QPoint &point, const QPixmap &pixmap, const QRect &source)
void drawPixmap(const QPointF &point, const QPixmap &pixmap)
void drawPixmap(const QPoint &point, const QPixmap &pixmap)
void drawPixmap(int x, int y, const QPixmap &pixmap)
void drawPixmap(const QRect &rectangle, const QPixmap &pixmap)
void drawPixmap(int x, int y, int width, int height, const QPixmap &pixmap)
void drawPixmapFragments(const QPainter::PixmapFragment *fragments, int fragmentCount, const QPixmap &pixmap, QPainter::PixmapFragmentHints hints = PixmapFragmentHints())
void drawPoint(const QPointF &position)
void drawPoint(const QPoint &position)
void drawPoint(int x, int y)
void drawPoints(const QPointF *points, int pointCount)
void drawPoints(const QPolygonF &points)
void drawPoints(const QPoint *points, int pointCount)
void drawPoints(const QPolygon &points)
void drawPolygon(const QPointF *points, int pointCount, Qt::FillRule fillRule = Qt::OddEvenFill)
void drawPolygon(const QPolygonF &points, Qt::FillRule fillRule = Qt::OddEvenFill)
void drawPolygon(const QPoint *points, int pointCount, Qt::FillRule fillRule = Qt::OddEvenFill)
void drawPolygon(const QPolygon &points, Qt::FillRule fillRule = Qt::OddEvenFill)
void drawPolyline(const QPointF *points, int pointCount)
void drawPolyline(const QPolygonF &points)
void drawPolyline(const QPoint *points, int pointCount)
void drawPolyline(const QPolygon &points)
void drawRect(const QRectF &rectangle)
void drawRect(int x, int y, int width, int height)
void drawRect(const QRect &rectangle)
void drawRects(const QRectF *rectangles, int rectCount)
void drawRects(const QVector<QRectF> &rectangles)
void drawRects(const QRect *rectangles, int rectCount)
void drawRects(const QVector<QRect> &rectangles)
void drawRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius, Qt::SizeMode mode = Qt::AbsoluteSize)
void drawRoundedRect(int x, int y, int w, int h, qreal xRadius, qreal yRadius, Qt::SizeMode mode = Qt::AbsoluteSize)
void drawRoundedRect(const QRect &rect, qreal xRadius, qreal yRadius, Qt::SizeMode mode = Qt::AbsoluteSize)
void drawStaticText(const QPointF &topLeftPosition, const QStaticText &staticText)
void drawStaticText(const QPoint &topLeftPosition, const QStaticText &staticText)
void drawStaticText(int left, int top, const QStaticText &staticText)
void drawText(const QRectF &rectangle, int flags, const QString &text, QRectF *boundingRect = nullptr)
void drawText(const QPointF &position, const QString &text)
void drawText(const QPoint &position, const QString &text)
void drawText(int x, int y, const QString &text)
void drawText(const QRect &rectangle, int flags, const QString &text, QRect *boundingRect = nullptr)
void drawText(int x, int y, int width, int height, int flags, const QString &text, QRect *boundingRect = nullptr)
void drawText(const QRectF &rectangle, const QString &text, const QTextOption &option = QTextOption())
void drawTiledPixmap(const QRectF &rectangle, const QPixmap &pixmap, const QPointF &position = QPointF())
void drawTiledPixmap(int x, int y, int width, int height, const QPixmap &pixmap, int sx = 0, int sy = 0)
void drawTiledPixmap(const QRect &rectangle, const QPixmap &pixmap, const QPoint &position = QPoint())
bool end()
void endNativePainting()
void eraseRect(const QRectF &rectangle)
void eraseRect(int x, int y, int width, int height)
void eraseRect(const QRect &rectangle)
void fillPath(const QPainterPath &path, const QBrush &brush)
void fillRect(const QRectF &rectangle, const QBrush &brush)
void fillRect(int x, int y, int width, int height, const QBrush &brush)
void fillRect(const QRect &rectangle, const QBrush &brush)
void fillRect(const QRectF &rectangle, const QColor &color)
void fillRect(int x, int y, int width, int height, const QColor &color)
void fillRect(const QRect &rectangle, const QColor &color)
void fillRect(int x, int y, int width, int height, Qt::GlobalColor color)
void fillRect(const QRect &rectangle, Qt::GlobalColor color)
void fillRect(const QRectF &rectangle, Qt::GlobalColor color)
void fillRect(int x, int y, int width, int height, Qt::BrushStyle style)
void fillRect(const QRect &rectangle, Qt::BrushStyle style)
void fillRect(const QRectF &rectangle, Qt::BrushStyle style)
void fillRect(int x, int y, int width, int height, QGradient::Preset preset)
void fillRect(const QRect &rectangle, QGradient::Preset preset)
void fillRect(const QRectF &rectangle, QGradient::Preset preset)
const QFont & font() const
QFontInfo fontInfo() const
QFontMetrics fontMetrics() const
bool hasClipping() const
bool isActive() const
Qt::LayoutDirection layoutDirection() const
qreal opacity() const
QPaintEngine * paintEngine() const
const QPen & pen() const
QPainter::RenderHints renderHints() const
void resetTransform()
void restore()
void rotate(qreal angle)
void save()
void scale(qreal sx, qreal sy)
void setBackground(const QBrush &brush)
void setBackgroundMode(Qt::BGMode mode)
void setBrush(const QBrush &brush)
void setBrush(Qt::BrushStyle style)
void setBrushOrigin(const QPointF &position)
void setBrushOrigin(int x, int y)
void setBrushOrigin(const QPoint &position)
void setClipPath(const QPainterPath &path, Qt::ClipOperation operation = Qt::ReplaceClip)
void setClipRect(const QRectF &rectangle, Qt::ClipOperation operation = Qt::ReplaceClip)
void setClipRect(const QRect &rectangle, Qt::ClipOperation operation = Qt::ReplaceClip)
void setClipRect(int x, int y, int width, int height, Qt::ClipOperation operation = Qt::ReplaceClip)
void setClipRegion(const QRegion &region, Qt::ClipOperation operation = Qt::ReplaceClip)
void setClipping(bool enable)
void setCompositionMode(QPainter::CompositionMode mode)
void setFont(const QFont &font)
void setLayoutDirection(Qt::LayoutDirection direction)
void setOpacity(qreal opacity)
void setPen(const QPen &pen)
void setPen(const QColor &color)
void setPen(Qt::PenStyle style)
void setRenderHint(QPainter::RenderHint hint, bool on = true)
void setRenderHints(QPainter::RenderHints hints, bool on = true)
void setTransform(const QTransform &transform, bool combine = false)
void setViewTransformEnabled(bool enable)
void setViewport(const QRect &rectangle)
void setViewport(int x, int y, int width, int height)
void setWindow(const QRect &rectangle)
void setWindow(int x, int y, int width, int height)
void setWorldMatrixEnabled(bool enable)
void setWorldTransform(const QTransform &matrix, bool combine = false)
void shear(qreal sh, qreal sv)
void strokePath(const QPainterPath &path, const QPen &pen)
bool testRenderHint(QPainter::RenderHint hint) const
const QTransform & transform() const
void translate(const QPointF &offset)
void translate(const QPoint &offset)
void translate(qreal dx, qreal dy)
bool viewTransformEnabled() const
QRect viewport() const
QRect window() const
bool worldMatrixEnabled() const
const QTransform & worldTransform() const

Detailed Description

QPainter provides highly optimized functions to do most of the drawing GUI programs require. It 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. QPainter can operate on any object that inherits the QPaintDevice class.

The common use of QPainter is inside a widget's paint event: Construct and customize (e.g. set the pen or the brush) the painter. Then draw. Remember to destroy the QPainter object after drawing. For example:

 

void SimpleExampleWidget::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setPen(Qt::blue);
    painter.setFont(QFont("Arial", 30));
    painter.drawText(rect(), Qt::AlignCenter, "Qt");
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

参考:https://doc.qt.io/qt-5/qpainter.html

################################

posted @ 2021-05-10 08:50  西北逍遥  阅读(166)  评论(0编辑  收藏  举报