#include <QPainter>
#include <QWidget>
#include <QTimer>
#include <qmath.h>
#include <QApplication>
class Radar : public QWidget {
public:
Radar(QWidget *parent = 0);
protected:
void paintEvent(QPaintEvent *event);
private:
QTimer *timer;
int angle;
int alpha=0;
double cir_size=5;
};
Radar::Radar(QWidget *parent)
: QWidget(parent), angle(0) {
this->setStyleSheet("QWidget{background-color:rgb(25,25,112)}");
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(10); // 每10毫秒进行一次更新
}
void Radar::paintEvent(QPaintEvent * /*event*/ ) {
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
// 绘制雷达扫描区域
painter.setPen(QColor("#00ff00"));
int i=0;
while ((50+(width()/8.00*i))*2<width()&&(50+(height()/8.00*i))*2<height()) {
painter.drawEllipse(50+(width()/8.00*i), 50+(height()/8.00*i), width() - (50+(width()/8.00*i))*2, height() - (50+(height()/8.00*i))*2);
i++;
}
for (int i=0;i<12;i++) {
painter.drawLine(width() / 2, height() / 2,
width() / 2 + (width() - 20) / 2 * qCos(qDegreesToRadians(30.00*i)),
height() / 2 + (height() - 20) / 2 * qSin(qDegreesToRadians(30.00*i)));
}
painter.translate(width() / 2,height() / 2);
painter.setPen(QPen(qRgba(255, 255, 0,250)));
painter.setFont(QFont("Calibri",10));
painter.rotate(20);
painter.setFont(QFont("Calibri",10));
for(int i=1;i<=360;i++){
painter.rotate(1);
painter.drawLine(0,-width() / 2+10,0,-width() / 2+13);
}
for(int i=1;i<=72;i++){
painter.rotate(5);
painter.drawLine(0,-width() / 2+10,0,-width() / 2+15);
}
for(int i=1;i<=36;i++){
painter.setRenderHint(QPainter::Antialiasing);
painter.rotate(10);
painter.drawLine(0,-width() / 2+10,0,-width() / 2+20);
}
//}
painter.translate(-width() / 2,-height() / 2);
qreal pi = 3.1415926535;
qreal arc = pi/180;
qreal anglr = pi/360;
// 绘制雷达扫描线
QConicalGradient gradient(width() / 2, height() / 2,angle);
gradient.setColorAt(0.875, QColor(0, 255, 0, 0));
gradient.setColorAt(1, QColor(0, 255, 0, 155));//尾部
painter.setBrush(gradient);
painter.setPen(QPen(Qt::NoPen));//去掉外框线
painter.drawPie(10,10, width()-20, height()-20,angle*16,-45.00*16);
// 绘制雷达扫描中心点
angle = (angle + 1)%(360*16) ;// 更新雷达扫描角度
alpha = (alpha+2)%256;
cir_size = cir_size+0.01;
if(cir_size>10)
{
cir_size = 5;
}
painter.setBrush(QBrush(QColor(50, 255, 200,alpha)));
painter.drawEllipse(200,200,cir_size,cir_size);
painter.drawEllipse(100,100,cir_size,cir_size);
painter.drawEllipse(60+120,60+161,cir_size,cir_size);
painter.drawEllipse(60+240,60+161,cir_size,cir_size);
}
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
Radar radar;
radar.setWindowTitle("radar");
radar.resize(400, 400);
radar.show();
return app.exec();
}