画箭头需要注意:
计算箭头两个线的位置和长度与直线或弧线间的位置关系。
1.画直线箭头
关键代码
const int length = 10;//箭头斜着的投影到线上的长度
QVector<QLineF> lines;
lines.append(QLineF(20,height()/2,width()/2,height()/2));
lines.append(QLineF(width()/2-length,height()/2+length,width()/2,height()/2));
lines.append(QLineF(width()/2-length,height()/2-length,width()/2,height()/2));
QPen pen;
pen.setColor(Qt::blue);
pen.setWidthF(3.5);
painter.setPen(pen);
painter.drawLines(lines);
2.画弧线箭头
关键代码
painter.translate(width()/2,height()/2);
painter.setRenderHint(QPainter::Antialiasing);
const int radius = 150;
const int startAngle = 45;
const int spanAngle = 90;
const float PI = 3.14;
const float ratio = 0.5;
QPen pen;
pen.setWidth(3);
pen.setColor(Qt::red);
pen.setCapStyle(Qt::RoundCap);
painter.setPen(pen);
painter.drawArc(QRectF(-radius,-radius,radius*2,radius*2),startAngle*16,spanAngle*16);
QVector<QLineF> lines;
lines.append(QLineF(radius*cos(startAngle*PI/180),-radius*sin(startAngle*PI/180),radius*cos(startAngle*PI/180)-10,-radius*sin(startAngle*PI/180)));
lines.append(QLineF(radius*cos(startAngle*PI/180),-radius*sin(startAngle*PI/180),radius*cos(startAngle*PI/180),-radius*sin(startAngle*PI/180)-10));
painter.drawLines(lines);
painter.translate(width()/2,height()/2);
painter.setRenderHint(QPainter::Antialiasing);
const int radius = 150;
const int startAngle = 45;
const int spanAngle = 90;
const float PI = 3.14;
const float ratio = 0.5;
QPen pen;
pen.setWidth(3);
pen.setColor(Qt::red);
pen.setCapStyle(Qt::RoundCap);
painter.setPen(pen);
painter.drawArc(QRectF(-radius,-radius,radius*2,radius*2),startAngle*16,spanAngle*16);
QVector<QLineF> lines;
// lines.append(QLineF(radius*cos(startAngle*PI/180),-radius*sin(startAngle*PI/180),radius*cos(startAngle*PI/180)-10,-radius*sin(startAngle*PI/180)));
// lines.append(QLineF(radius*cos(startAngle*PI/180),-radius*sin(startAngle*PI/180),radius*cos(startAngle*PI/180),-radius*sin(startAngle*PI/180)-10));
lines.append(QLineF(radius*cos((startAngle+spanAngle)*PI/180),-radius*sin((startAngle+spanAngle)*PI/180),radius*cos((startAngle+spanAngle)*PI/180)+10,-radius*sin((startAngle+spanAngle)*PI/180)));
lines.append(QLineF(radius*cos((startAngle+spanAngle)*PI/180),-radius*sin((startAngle+spanAngle)*PI/180),radius*cos((startAngle+spanAngle)*PI/180),-radius*sin((startAngle+spanAngle)*PI/180)-10));
painter.drawLines(lines);
今天的文章QPainter画箭头分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/6825.html