相关资料:
实例代码:
.pro
1 QT += core gui
2
3 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
4
5 CONFIG += c++11
6
7 # The following define makes your compiler emit warnings if you use
8 # any Qt feature that has been marked deprecated (the exact warnings
9 # depend on your compiler). Please consult the documentation of the
10 # deprecated API in order to know how to port your code away from it.
11 DEFINES += QT_DEPRECATED_WARNINGS
12
13 # You can also make your code fail to compile if it uses deprecated APIs.
14 # In order to do so, uncomment the following line.
15 # You can also select to disable deprecated APIs only up to a certain version of Qt.
16 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
17
18 SOURCES += \
19 main.cpp \
20 mainwindow.cpp \
21 scan.cpp
22
23 HEADERS += \
24 mainwindow.h \
25 scan.h
26
27 FORMS += \
28 mainwindow.ui
29
30 # Default rules for deployment.
31 qnx: target.path = /tmp/$${TARGET}/bin
32 else: unix:!android: target.path = /opt/$${TARGET}/bin
33 !isEmpty(target.path): INSTALLS += target
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "scan.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
Scan *m_pScan;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle(QStringLiteral("Qt之雷达扫描效果"));
m_pScan = new Scan(this);
m_pScan->setGeometry(0,0,600,600);
}
MainWindow::~MainWindow()
{
delete ui;
}
mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>600</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget"/>
</widget>
<resources/>
<connections/>
</ui>
scan.h
1 #ifndef SCAN_H
2 #define SCAN_H
3
4 #include <QWidget>
5 #include <QPainter>
6 #include <QTime>
7 #include <QTimer>
8 #include <QDebug>
9 #include <QPoint>
10 #include <QtMath>
11
12 class Scan : public QWidget
13 {
14 Q_OBJECT
15 public:
16 explicit Scan(QWidget *parent = nullptr);
17 ~Scan();
18 public slots:
19 void timerTimeOut();
20 protected:
21 void paintEvent(QPaintEvent *event) override;
22 private slots:
23 void on_pushButton_clicked();
24 private:
25
26 };
27
28 #endif // SCAN_H
#include "scan.h"
QTimer *timer = new QTimer();
int m_angle=0;
int alpha=0;
double cir_size=5;
Scan::Scan(QWidget *parent) :
QWidget(parent)
{
setPalette(Qt::black);
setAutoFillBackground(true);//强制设置背景颜色为黑色
connect(timer,SIGNAL(timeout()),this,SLOT(timerTimeOut()));//用槽函数控制雷达扫描效果。
timer->start(10);
}
Scan::~Scan()
{
}
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;//将扫描动画重置并刷新数据,后面要将数据刷新的函数写在下面。
}