首页 > 其他分享 >Qt-Qt之雷达扫描效果-No25-QtRadar

Qt-Qt之雷达扫描效果-No25-QtRadar

时间:2023-01-28 10:39:38浏览次数:50  
标签:drawEllipse Qt No25 QtRadar 60 cir 240 painter size

Qt-Qt之雷达扫描效果-No25-QtRadar_ci

相关资料:


实例代码:

.pro

Qt-Qt之雷达扫描效果-No25-QtRadar_QT_02

Qt-Qt之雷达扫描效果-No25-QtRadar_ci_03

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

Qt-Qt之雷达扫描效果-No25-QtRadar_QT_02

Qt-Qt之雷达扫描效果-No25-QtRadar_ci_03

#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}


 mainwindow.h

Qt-Qt之雷达扫描效果-No25-QtRadar_QT_02

Qt-Qt之雷达扫描效果-No25-QtRadar_ci_03

#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

Qt-Qt之雷达扫描效果-No25-QtRadar_QT_02

Qt-Qt之雷达扫描效果-No25-QtRadar_ci_03

<?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

Qt-Qt之雷达扫描效果-No25-QtRadar_QT_02

Qt-Qt之雷达扫描效果-No25-QtRadar_ci_03

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;//将扫描动画重置并刷新数据,后面要将数据刷新的函数写在下面。
}


 


标签:drawEllipse,Qt,No25,QtRadar,60,cir,240,painter,size
From: https://blog.51cto.com/u_15308785/6024720

相关文章

  • Qt类型转换与信号映射
    qobject_cast函数:qobject_cast<需要转换的类型>(待转换的类型->widget()); 一般要先转为部件类型(QWidget*),之后再转为需要的类型。 信号映射:https://blog.csdn.net/qq......
  • Qt七牛云视频上传
    最近有一个项目,是qt安卓下的一个客户端,有录制视频和上传七牛云的需求,录制视频,在qt5版本下,qt并没有自带录制usb摄像头为视频的控件,所以采用了qt6版本下的qt自带的录制usb摄像......
  • 如何在 pyqt 中使用动画实现平滑滚动的 QScrollArea
    前言在之前的博客《如何在pyqt中实现平滑滚动的QScrollArea》中,我们使用定时器和队列实现了平滑滚动。但是实现代码还是有一点复杂,所以这篇博客将使用Qt的动画框架Q......
  • Qt-Qt之雷达扫描效果-No25-QtRadar
    相关资料:https://blog.csdn.net/weixin_43865793/article/details/127684665  原作者实例代码:.pro1QT+=coregui23greaterThan(QT_MAJOR_VERSI......
  • Linux Debian11安装QT6开发环境
    从Qt5.14开始,官方不提供离线安装包,只提供源码包和在线安装器。但是清华为我们提供了快速的在线安装方式。一.下载清华提供的在线安装器在线安装器下载链接二、给在线安装......
  • QT sqllite操作
    //添加sql模块      //打开数据库MainWindow::MainWindow(QWidget*parent):QMainWindow(parent),ui(newUi::MainWindow){ui->setupUi(this);......
  • PyQt5 表格的一些参数设置函数
    self.tablewidget.horizontalHeader().setDefaultSectionSize(50) #设置默认列宽self.tablewidget.verticalHeader().setDefaultSectionSize(30)  #设置默认行高s......
  • Qt 一键部署脚本
    echo"begindeploying..."echoSettingupenvironmentforQtusage...setPATH=C:\Qt\Qt5.8.0\5.8\mingw53_32\bin;C:/Qt/Qt5.8.0/Tools/mingw530_32\bin;%PATH%s......
  • 在Debian 11.3.X LXQT 中搭建ESP8266 IDF 开发环境
    安装前准备VMware®Workstation16ProV16.2.3build-19376536Debian11.3.XLXQT镜像镜像下载地址列表选择debian-live-11.3.0-amd64-lxqt.iso安装过程很简单......
  • qttabbar关闭所有的标签怎么设置不关闭窗口
     ​​为什么关闭窗口,所有选项卡都被关闭?【qttabbar吧】_百度贴吧​​......