支持0到360°,360到0°的过度。
直接上代码,可以直接用,使用的paintevent事件实现的,没啥好讲的。
.cpp
void Widget::drawCourse(QPainter& p,QPen pen,QFont font)
{
double currentNumber = m_ang;
p.setBrush(Qt::black);
p.drawRect(rect());
//航向刻度图
pen.setColor("#ffffff");
pen.setWidth(2);
p.setPen(pen);
font.setPointSize(20);
p.setFont(font);
p.drawLine(0,0,width(),0);
int mainScale = 360;
double movePos = currentNumber*35-width()/2;
//360前半截
for (int i = 0; i < mainScale+1; ++i) {
if (i%5 == 0)
{
pen.setWidth(2);
p.setPen(pen);
p.drawLine(0+i*35-movePos,0,0+i*35-movePos,26);
p.drawText(0+i*35-30-movePos,33,60,30,Qt::AlignCenter,QString::number(i%360));
}
else
{
pen.setWidth(1);
p.setPen(pen);
p.drawLine(0+i*35-movePos,0,0+i*35-movePos,20);
}
}
//0后半截,360前半截
double startPt = 0;
double showSpan = width()/35.00;
if (currentNumber > 360 - showSpan || currentNumber < 360 - showSpan)
{
if (currentNumber > 360 - showSpan)
startPt = (360 - currentNumber + showSpan/2)*35;
else
startPt = (showSpan/2 - currentNumber)*35 - 35*mainScale;
for (int i = 0; i < mainScale+1; ++i) {
if (i%5 == 0)
{
pen.setWidth(2);
p.setPen(pen);
p.drawLine(0+i*35+startPt,0,0+i*35+startPt,26);
p.drawText(0+i*35-30+startPt,33,60,30,Qt::AlignCenter,QString::number(i%360));
}
else
{
pen.setWidth(1);
p.setPen(pen);
p.drawLine(0+i*35+startPt,0,0+i*35+startPt,20);
}
}
}
//航向中线
p.setPen(QPen(QColor("#009af7")));
p.setBrush(QBrush(QColor("#009af7")));
QPolygon pol;
pol<<QPoint(width()/2,72)<<QPoint(width()/2-17,103)<<QPoint(width()/2+16,103);
p.drawPolygon(pol);
}