1. Qt进行绘图时,要重写绘图函数
void paintEvent(QPaintEvent *);
2. 定义绘图对象
QPainter painter(this);
2.将坐标系的该点变成原点坐标
painter.translate(100,100);
3.设置画笔颜色
QPen pen(Qt::black,3,Qt::SolidLine,Qt::RoundCap,Qt::RoundJoin);
painter.setPen(pen);
4. 画下面直线,画从(0,0)点到(60,0)的
painter.drawLine(0,0,60,0);
5.填充颜色,填充(0,5)到(30,20)到(60,5)这个三角形之间的颜色,并设置画刷为青色
QPolygon polyGon;
polyGon.clear();
polyGon<<QPoint(0,5);
polyGon<<QPoint(30,20);
polyGon<<QPoint(60,5);
painter.setBrush(Qt::cyan);
painter.drawConvexPolygon(polyGon);
6.在刻度线上写字
painter.drawText(pointLeft,QString::number(num));
7.设置渐变,在y坐标为sliderHigh*30/valueSlider上设置渐变,比例为0,0.3,0.7,1.0,用矩形区域,0,0,60, sliderHigh来表达渐变
QLinearGradient linerGradient=QLinearGradient(0,0,0,sliderHigh*30/valueSlider);
linerGradient.setColorAt(0.0,Qt::cyan);
linerGradient.setColorAt(0.3,Qt::green);
linerGradient.setColorAt(0.7,Qt::yellow);
linerGradient.setColorAt(1.0,Qt::red);
QBrush qsh(linerGradient);
painter.setBrush(qsh);
QPen pen(Qt::red,3,Qt::SolidLine,Qt::RoundCap,Qt::RoundJoin);
painter.drawRect(0, 0, 60, sliderHigh);
8.在函数的最后设置更新
update(); //刷新
9.加载图片,并且放大缩小图片
QImage img; //图像
QMatrix matrix;
matrix.scale(0.5,0.5); //长宽缩小为图片的0.5倍
img.load(":/new/prefix1/RES/car.png");
img=img.transformed(matrix);
painter.drawImage(-60,-120,img);