QPainter绘制方法

效果

QPainter绘制方法_第1张图片

1 绘制点

	QPainter painter(this);
	painter.setPen(QPen(QColor(255,255,255),2));	//后面参数2为点的大小
	QPoint dotPos = QPoint(100,100);
	painter.drawPoint(dotPos);

2 绘制直线

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing); //抗锯齿,绘制出来看起来不会有楼梯形状的锯齿感
    painter.setPen(QPen(QColor(255,255,255),2));   //后面参数2为线条的粗细
    painter.drawLine(QPoint(0,0),QPoint(100,100));

3 绘制矩形

 	paint->drawRect(20,20,160,160); 

4 绘制圆角矩形

 	QRectF rectangle(10.0, 20.0, 80.0, 60.0);
  	painter.drawRoundedRect(rectangle, 20.0, 15.0);

5 绘制圆

	painter.drawEllipse(QPoint(100,100),100,100);

6 绘制文字

	QPainter painter(this);
	QRectF rect(0, 0, 100, 50);		//文字显示区域
    painter.drawText(rect, Qt::AlignHCenter, QString::number(1234));//字体居中

7 绘制多边形

    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());    //绘制多边形

8 绘制扇型

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度)

9 形状填充

对于绘制闭合的图像,可以设置填充,如圆、多边形等,代码如下:

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绘制方法_第2张图片

10 绘制图片

    QPainter painter(this);
    QRect rect(0,0,200,200);
    painter.drawImage(rect,*mImage);

11 绘制圆弧

	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);

在这里插入图片描述

12 绘制弦

 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);

在这里插入图片描述

13 绘制路径

QPainterPath path;
  path.moveTo(20, 80);
  path.lineTo(20, 30);
  path.cubicTo(80, 0, 50, 50, 80, 80);

  QPainter painter(this);
  painter.drawPath(path);

在这里插入图片描述

你可能感兴趣的:(C++\QT,QPainter,qt绘制,painter)