#include
#include
1 基本知识
Painter 画图设备
pen :笔
brush:刷子
通过改变笔和刷子实现多个样式
//例如
QPainter painter(this);
QPen pen(Qt::DotLine);
QBrush brush(Qt::blue);
brush.setStyle(Qt::HorPattern);
painter.setPen(pen);
painter.setBrush(brush);
painter.drawRect(50,50,200,100);
在paintevent 事件实现
效果如下:
2 基本常用方法
会发现 大部分函数都是内联函数
//填充矩形
void fillRect(const QRectF &, const QBrush &);
inline void fillRect(int x, int y, int w, int h, const QBrush &);
void fillRect(const QRect &, const QBrush &);
void fillRect(const QRectF &, const QColor &color);
inline void fillRect(int x, int y, int w, int h, const QColor &color);
void fillRect(const QRect &, const QColor &color);
inline void fillRect(int x, int y, int w, int h, Qt::GlobalColor c);
inline void fillRect(const QRect &r, Qt::GlobalColor c);
inline void fillRect(const QRectF &r, Qt::GlobalColor c);
inline void fillRect(int x, int y, int w, int h, Qt::BrushStyle style);
inline void fillRect(const QRect &r, Qt::BrushStyle style);
inline void fillRect(const QRectF &r, Qt::BrushStyle style);
//画线
inline void QPainter::drawLine(const QLineF &)
inline void QPainter::drawLine(const QLine &line)
inline void QPainter::drawLine(int x1, int y1, int x2, int y2)
inline void QPainter::drawLine(const QPoint &p1, const QPoint &p2)
inline void QPainter::drawLine(const QPointF &p1, const QPointF &p2)
//画矩形
inline void QPainter::drawRect(const QRectF &rect)
inline void QPainter::drawRect(int x, int y, int w, int h)
inline void QPainter::drawRect(const QRect &r)
//画圆
inline void QPainter::drawEllipse(int x, int y, int w, int h)
inline void QPainter::drawEllipse(const QPointF ¢er, qreal rx, qreal ry)
inline void QPainter::drawEllipse(const QPoint ¢er, int rx, int ry)
//画弧线
inline void QPainter::drawArc(const QRect &r, int a, int alen)
inline void QPainter::drawArc(int x, int y, int w, int h, int a, int alen)
3 源码
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::paintEvent(QPaintEvent *)
{
QPainter painter(this);
QPen pen(Qt::DotLine);
QBrush brush(Qt::blue);
brush.setStyle(Qt::HorPattern);
painter.setPen(pen);
painter.setBrush(brush);
painter.drawRect(50,50,200,100);
}
Demo16 地址:https://gitee.com/codemaner/qt_learning_record/tree/master