赞
踩
本示例主要利用 QPainter实现雷达图及余晖扫描效果,不依赖其他第三方库。显示效果流畅、平滑,不卡顿,可以直接跨平台应用Windows、Android、iMac及iOS系统。
如下是在Android手机上的界面效果图:
源代码工程链接:
https://download.csdn.net/download/u011275054/10867488
1、总体说明
该雷达模拟器主要通过QPainter分别绘制刻度线、刻度值、余晖扫描以及障碍物区域几个主要部分。
图中的红色区域为扫描到的障碍物部分,绿色区域为实时扫描部分,设定能够探测到的最大距离为180cm,
检测到障碍物后,显示雷达实时的旋转角度和探测到的距离。
下面逐一进行上代码说明
2、雷达主体部分的绘制
- void RadarSimulator::drawRadar(QPainter *painter)
- {
- painter->save();
- painter->setPen(pen_draw);
-
- painter->translate(width/2,0);
- double dOrigpos = height-height*0.074;
- painter->drawArc(-(width-width*0.0825)/2, dOrigpos-(width-width*0.0825)/2,(width-width*0.0825),(width-width*0.0825),0*16,180* 16);
- painter->drawArc(-(width-width*0.27)/2,dOrigpos-(width-width*0.27)/2,(width-width*0.27),(width-width*0.27),0*16,180* 16);
- painter->drawArc(-(width-width*0.479)/2,dOrigpos-(width-width*0.479)/2,(width-width*0.479),(width-width*0.479),0*16,180* 16);
- painter->drawArc(-(width-width*0.687)/2,dOrigpos-(width-width*0.687)/2,(width-width*0.687),(width-width*0.687),0*16,180* 16);
- painter->drawArc(-(width-width*0.887)/2,dOrigpos-(width-width*0.887)/2,(width-width*0.887),(width-width*0.887),0*16,180* 16);
- painter->restore();
-
- painter->save();
- painter->setPen(pen_draw);
- painter->translate(width/2,height-height*0.074);
-
- painter->drawLine(-width/2,0,width/2,0);
- double dRadius = width/2*0.96;
-
- painter->drawLine(0,0,(-dRadius)*qCos(qDegreesToRadians(30.0)), (-dRadius)*qSin(qDegreesToRadians(30.0)));
- painter->drawLine(0,0,(-dRadius)*qCos(qDegreesToRadians(60.0)), (-dRadius)*qSin(qDegreesToRadians(60.0)));
- painter->drawLine(0,0,(-dRadius)*qCos(qDegreesToRadians(90.0)), (-dRadius)*qSin(qDegreesToRadians(90.0)));
- painter->drawLine(0,0,(-dRadius)*qCos(qDegreesToRadians(120.0)),(-dRadius)*qSin(qDegreesToRadians(120.0)));
- painter->drawLine(0,0,(-dRadius)*qCos(qDegreesToRadians(150.0)),(-dRadius)*qSin(qDegreesToRadians(150.0)));
- painter->drawLine((-dRadius)*qCos(qDegreesToRadians(30.0)),0,dRadius,0);
- painter->restore();
- }
3、雷达实时扫描线部分:
- void RadarSimulator::drawLine()
- {
- QPainter *painter = new QPainter(this);
- painter->save();
-
- QPen pen;
- QColor color(30,250,60);
- pen.setWidthF(16);
- pen.setColor(color); //添加线的颜色
- painter->setPen(pen);
- painter->translate(width/2,height-height*0.074);
- // draws the line according to the angle
- //painter->drawLine(0,0,(height-height*0.12)*qCos(qDegreesToRadians((double)iAngle)),-(height-height*0.12)*qSin(qDegreesToRadians((double)iAngle)));
-
- if(bRecyle){
- veclines.clear();
- bRecyle = false;
- bNeedErase = true;
- }
- if(veclines.size() >= 30){
- veclines.pop_front();
- }
- QLineF line(0, 0,(height-height*0.12)*qCos(qDegreesToRadians((double)iAngle)),
- -(height-height*0.12)*qSin(qDegreesToRadians((double)iAngle)));
- veclines.push_back(line);
- //qDebug("***DrawLine:Angle:%d", iAngle);
- int line_cnt = veclines.size();
- for(int num = 0; num < line_cnt; num++)
- {
- float opc = 0.3;
- if(num != 0){
- opc = (float)2.0*num / line_cnt;
- if(opc <= 0.3)
- opc = 0.3;
- }
- painter->setOpacity(opc);
- painter->drawLine(veclines[num]);
- }
-
- // painter->drawLines(veclines);
-
- painter->restore();
- delete(painter);
- }
4、绘制扫描障碍物区域部分:
- void RadarSimulator::drawObject()
- {
- QPainter *painter = new QPainter(this);
- painter->save();
- QPen pen;
- QColor color(255,10,10);
- pen.setWidthF(16);
- pen.setColor(color); //添加线的颜色
- painter->setPen(pen);
-
- painter->translate(width/2,height-height*0.074); // moves the starting coordinats to new location
- float pixsDistance = iDistance*((height-height*0.1666)*1/180); // covers the distance from the sensor from cm to pixels
- // limiting the range to 40 cms
-
- if(iDistance<180 && iDistance > 0)
- {
- if(bNeedErase){
- vecObjlines.clear();
- bNeedErase = false;
- }
-
- QLineF line(pixsDistance*qCos(qDegreesToRadians((double)iAngle)),
- -pixsDistance*qSin(qDegreesToRadians((double)iAngle)),
- (width-width*0.538)*qCos(qDegreesToRadians((double)iAngle)),
- -(width-width*0.538)*qSin(qDegreesToRadians((double)iAngle)));
- vecObjlines.push_back(line);
- }
-
- int line_cnt = vecObjlines.size();
- for(int num = 0; num < line_cnt; num++)
- {
- float opc = 0.3;
- if(num != 0){
- opc = (float)2.0*num / line_cnt;
- if(opc <= 0.3)
- opc = 0.3;
- }
- painter->setOpacity(opc);
- painter->drawLine(vecObjlines[num]);
- }
-
- // painter->drawLines(vecObjlines);
- painter->restore();
- delete(painter);
- }
5、绘制检测到的障碍物距离、角度数值部分
- void RadarSimulator::drawText(QPainter *painter)
- {
- painter->save();
- painter->setBrush(Qt::black);
- painter->drawRect(0,height-height*0.070,width,height*0.070);
- painter->restore();
-
- painter->save();
- QString noObject;
- if(iDistance>180 || iDistance <= 2) {
- noObject = "超出范围";
- }
- else {
- noObject = "已探测到";
- }
-
- painter->setPen(pen_draw);
- painter->setFont(font_txt);
- painter->drawText(width-width*0.4754, height-height*0.0833, "20cm");
- painter->drawText(width-width*0.3854, height-height*0.0833, "60cm");
- painter->drawText(width-width*0.281, height-height*0.0833, "100cm");
- painter->drawText(width-width*0.177, height-height*0.0833, "140cm");
- painter->drawText(width-width*0.0799, height-height*0.0833, "180cm");
-
- painter->setFont(font_Reslt);
- painter->drawText(width-width*0.915, height-height*0.0260, "目标: " + noObject);
- QString angle_ = QString::number(iAngle);
- painter->drawText(width-width*0.53, height-height*0.0260, "角度: " + angle_ +" °");
- QString dis = QString::number(iDistance);
- painter->drawText(width-width*0.30, height-height*0.0260, "距离: " + dis + " cm");
- painter->restore();
-
- painter->save();
- painter->setPen(pen_draw);
- painter->setFont(font_txt);
- painter->translate((width-width*0.5094)+width/2*qCos(qDegreesToRadians(30.0)),(height-height*0.0700)-width/2*qSin(qDegreesToRadians(30.0)));
- painter->rotate(-qDegreesToRadians(-60.0));
- painter->drawText(0, 0, "30°");
- painter->restore();
-
- painter->save();
- painter->setPen(pen_draw);
- painter->setFont(font_txt);
- painter->translate((width-width*0.509)+width/2*qCos(qDegreesToRadians(60.0)),(height-height*0.0650)-width/2*qSin(qDegreesToRadians(60.0)));
- painter->rotate(-qDegreesToRadians(-30.0));
- painter->drawText(0,0, "60°");
- painter->restore();
-
- painter->save();
- painter->setPen(pen_draw);
- painter->setFont(font_txt);
- painter->translate((width-width*0.507)+width/2*qCos(qDegreesToRadians(90.0)),(height-height*0.0433)-width/2*qSin(qDegreesToRadians(90.0)));
- painter->rotate(qDegreesToRadians(0.0));
- painter->drawText(0,0, "90°");
- painter->restore();
-
- painter->save();
- painter->setPen(pen_draw);
- painter->setFont(font_txt);
- painter->translate(width-width*0.503+width/2*qCos(qDegreesToRadians(120.0)),(height-height*0.06129)-width/2*qSin(qDegreesToRadians(120.0)));
- painter->rotate(qDegreesToRadians(-30.0));
- painter->drawText(0,0, "120°");
- painter->restore();
-
- painter->save();
- painter->setPen(pen_draw);
- painter->setFont(font_txt);
- painter->translate((width-width*0.5104)+width/2*qCos(qDegreesToRadians(150.0)),(height-height*0.0574)-width/2*qSin(qDegreesToRadians(150.0)));
- painter->rotate(qDegreesToRadians(-60.0));
- painter->drawText(0,0, "150°");
- painter->restore();
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。