您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“怎么用Qt繪制雷達掃描效果”,內容詳細,步驟清晰,細節處理妥當,希望這篇“怎么用Qt繪制雷達掃描效果”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
scan.h的代碼如下
#ifndef SCAN_H #define SCAN_H #include <QWidget> #include <QPainter> #include <QTime> #include <QTimer> #include <QDebug> #include <QPoint> #include <QtMath> namespace Ui { class Scan; } class Scan : public QWidget { Q_OBJECT public: explicit Scan(QWidget *parent = nullptr); ~Scan(); public slots: void timerTimeOut(); protected: void paintEvent(QPaintEvent *event) override; private slots: void on_pushButton_clicked(); private: Ui::Scan *ui; }; #endif // SCAN_H
scan.cpp的代碼如下
#include "scan.h" #include "ui_scan.h" QTimer *timer = new QTimer(); int m_angle=0; int alpha=0; double cir_size=5; Scan::Scan(QWidget *parent) : QWidget(parent), ui(new Ui::Scan) { ui->setupUi(this); setPalette(Qt::black); setAutoFillBackground(true);//強制設置背景顏色為黑色 connect(timer,SIGNAL(timeout()),this,SLOT(timerTimeOut()));//用槽函數控制雷達掃描效果。 timer->start(10); } Scan::~Scan() { delete ui; } void Scan::timerTimeOut() { timer->start(10); if(m_angle<-360){ m_angle=0; }//實現無縫連接,且保證m_angle的值不會溢出 if(alpha>=255){ alpha = 0; } if(cir_size>10){ cir_size=5; } update(); } void Scan::paintEvent(QPaintEvent *event)//此函數自動執行,實現掃描動畫效果 { Q_UNUSED(event); QPainter painter(this); QPen pen; painter.setRenderHint(QPainter::Antialiasing); painter.setPen(QPen(qRgba(128, 255, 0,250))); //網格改成坐標系 int x=280;//原本為240,右移40,下移60 int y=300; /** 直徑按照480處理 每隔40px畫一個圓 繪制雷達圖**/ QPoint center(x,y); painter.drawEllipse(center,240,240); painter.drawEllipse(center,240-48,240-48); painter.drawEllipse(center,240-96,240-96); painter.drawEllipse(center,240-144,240-144); painter.drawEllipse(center,240-192,240-192); /** 畫外圈刻度值,將坐標系移動到圓心 **/ painter.translate(x,y); painter.setPen(QPen(qRgba(255, 255, 0,250))); painter.setFont(QFont("Calibri",10)); for(int i=1;i<=360;i++){ painter.rotate(1); painter.drawLine(0,-240,0,-235); } for(int i=1;i<=72;i++){ painter.rotate(5); painter.drawLine(0,-240,0,-232); } for(int i=1;i<=36;i++){ painter.setRenderHint(QPainter::Antialiasing); painter.rotate(10); painter.drawLine(0,-240,0,-228); } for(int i=1;i<=4;i++){ painter.rotate(90); painter.drawLine(0,-240,0,-168); } painter.translate(-x,-y);//恢復坐標系 /** 繪制掃描效果 **/ QConicalGradient gradient(x, y, m_angle + 360); gradient.setColorAt(0.1, QColor(128, 255, 0, 150)); gradient.setColorAt(0.7, QColor(0, 255, 0, 0));//尾部 painter.setBrush(gradient); painter.setPen(QPen(Qt::NoPen));//去掉外框線 if (width() > height()){ painter.drawPie(40, 60, 480, 480, m_angle * 16, 360 * 16); } else{ painter.drawPie(40, 60, 480, 480, m_angle * 16, 360 * 16);} m_angle -= 1;//每次旋轉1度 alpha +=2; cir_size+=0.01; //模擬畫出可疑點 painter.setBrush(QBrush(QColor(50, 255, 200,alpha))); painter.drawEllipse(0,0,15,15); painter.drawEllipse(60+120,60+161,cir_size,cir_size); painter.drawEllipse(60+240,60+161,cir_size,cir_size); painter.drawEllipse(60+300,60+300,cir_size,cir_size); painter.setBrush(QBrush(QColor(50, 255, 200,(alpha+50)%255))); painter.drawEllipse(60+160,60+121,cir_size,cir_size); painter.drawEllipse(60+60,60+191,cir_size,cir_size); painter.drawEllipse(60+260,60+221,cir_size,cir_size); painter.setBrush(QBrush(QColor(50, 255, 200,(alpha+100)%255))); painter.drawEllipse(60+210,60+181,cir_size,cir_size); painter.drawEllipse(60+10,60+281,cir_size,cir_size); painter.drawEllipse(60+100,60+81,cir_size,cir_size); } void Scan::on_pushButton_clicked() { m_angle=0;//將掃描動畫重置并刷新數據,后面要將數據刷新的函數寫在下面。 }
效果圖,雷達右上角的圖標對應著on_pushButton的點擊事件
讀到這里,這篇“怎么用Qt繪制雷達掃描效果”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。