QPainter painter(this);
painter.setPen(QPen(QColor(255,255,255),2)); //后面参数2为点的大小
QPoint dotPos = QPoint(100,100);
painter.drawPoint(dotPos);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing); //抗锯齿,绘制出来看起来不会有楼梯形状的锯齿感
painter.setPen(QPen(QColor(255,255,255),2)); //后面参数2为线条的粗细
painter.drawLine(QPoint(0,0),QPoint(100,100));
paint->drawRect(20,20,160,160);
QRectF rectangle(10.0, 20.0, 80.0, 60.0);
painter.drawRoundedRect(rectangle, 20.0, 15.0);
painter.drawEllipse(QPoint(100,100),100,100);
QPainter painter(this);
QRectF rect(0, 0, 100, 50); //文字显示区域
painter.drawText(rect, Qt::AlignHCenter, QString::number(1234));//字体居中
QPointF points[4];
points[0] = m_drawArea.topRight();
points[1] = m_drawArea.topLeft();
points[2] = m_drawArea.bottomRight();
points[3] = m_drawArea.bottomLeft();
painter.drawPolygon(points, angleMapLen.size()); //绘制多边形
painter.drawPie(QRect(0,0,100,100), 45*16, 90*16);
函数原型:void QPainter::drawPie(const QRect &, int a, int alen)
参数1: 圆弧的圆心所处于矩形 (扇型圆心为矩形中心)
参数2: 圆弧的开始始的角度°(单位1/16度)
参数3: 圆弧的 转动 的角度°(单位1/16度)
对于绘制闭合的图像,可以设置填充,如圆、多边形等,代码如下:
QBrush brush(QColor(0, 0, 255, 50), Qt::Dense1Pattern); //后面参数为填充样式
painter.setBrush(brush); //使用画刷
怎么使用?以圆为例子,把上面代码加在绘制之前就行了,如下:
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing); //抗锯齿
painter.setPen(QPen(QColor(255,255,255),2)); //后面参数2为线条的粗细
QBrush brush(QColor(0, 0, 255, 50), Qt::Dense1Pattern); //后面参数为填充样式
painter.setBrush(brush); //使用画刷
painter.drawEllipse(QPoint(100,100),100,100);
对于填充,可以选择多种填充样式
QPainter painter(this);
QRect rect(0,0,200,200);
painter.drawImage(rect,*mImage);
QRectF rectangle(10.0, 20.0, 80.0, 60.0);
int startAngle = 30 * 16;
int spanAngle = 120 * 16;
QPainter painter(this);
painter.drawArc(rectangle, startAngle, spanAngle);
QRectF rectangle(10.0, 20.0, 80.0, 60.0);
int startAngle = 30 * 16;
int spanAngle = 120 * 16;
QPainter painter(this);
painter.drawChord(rect, startAngle, spanAngle);
QPainterPath path;
path.moveTo(20, 80);
path.lineTo(20, 30);
path.cubicTo(80, 0, 50, 50, 80, 80);
QPainter painter(this);
painter.drawPath(path);