Qt实现汽车仪表盘

时间:2024-07-05 10:24:28
/* * 画刻度值 */ void Widget::drawScaleValue(QPainter &painter, int radius) { //设置字体类型和大小 QFont textFont("Arial",15); //设置粗体 textFont.setBold(true); painter.setFont(textFont); int text_r = radius - 49; for(int i=0; i<=60; i++) { if(i%5 == 0) { if(i>=40) { painter.setPen(QPen(Qt::red, 5)); } //保存当前坐标系 painter.save(); int delX = qCos((210-angle*i)*M_PI/180) * text_r; int delY = qSin(qDegreesToRadians(210-angle*i)) * text_r; //平移坐标系 painter.translate(QPoint(delX,-delY)); //旋转坐标系 painter.rotate(-120+angle*i); //写上刻度值,文字居中 painter.drawText(-25,-25,50,30,Qt::AlignCenter,QString::number(i*4)); //恢复坐标系 painter.restore(); } } painter.setPen(QPen(Qt::white, 5)); }