Qt利用QPainter自绘实现扫描雷达功能scanneritem - 疯狂delphi

时间:2024-03-03 08:05:28

Qt利用QPainter自绘实现扫描雷达功能scanneritem

相关资料:

忘记了,艹,对不起原作者了。

 

.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     scanneritem.cpp
22 
23 HEADERS += \
24     mainwindow.h \
25     scanneritem.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
View Code

main.cpp

 1 #include "mainwindow.h"
 2 
 3 #include <QApplication>
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     QApplication a(argc, argv);
 8     MainWindow w;
 9     w.show();
10     return a.exec();
11 }
View Code

mainwindow.h

 1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 #include <QMainWindow>
 5 #include "scanneritem.h"
 6 
 7 QT_BEGIN_NAMESPACE
 8 namespace Ui { class MainWindow; }
 9 QT_END_NAMESPACE
10 
11 class MainWindow : public QMainWindow
12 {
13     Q_OBJECT
14 
15 public:
16     MainWindow(QWidget *parent = nullptr);
17     ~MainWindow();
18 
19 private:
20     Ui::MainWindow *ui;
21 };
22 #endif // MAINWINDOW_H
View Code

mainwindow.cpp

 1 #include "mainwindow.h"
 2 #include "ui_mainwindow.h"
 3 
 4 MainWindow::MainWindow(QWidget *parent)
 5     : QMainWindow(parent)
 6     , ui(new Ui::MainWindow)
 7 {
 8     ui->setupUi(this);
 9 
10     Widget *oWidget = new Widget(this);
11 
12     oWidget->setGeometry(10, 10, this->width(), this->height());
13 }
14 
15 MainWindow::~MainWindow()
16 {
17     delete ui;
18 }
View Code
scanneritem.h
 1 #ifndef SCANNERITEM_H
 2 #define SCANNERITEM_H
 3 
 4 #include "QWidget"
 5 #include "QList"
 6 #include "QTimer"
 7 #include "QPainter"
 8 #include "QPixmap"
 9 #include "QTime"
10 #include "QDebug"
11 
12 class Widget : public QWidget
13 {
14     Q_OBJECT
15 
16 public:
17     explicit Widget(QWidget *parent = 0);
18     ~Widget();
19     QPixmap paintWidget();
20 
21 protected:
22     void paintEvent(QPaintEvent *event);
23     void resizeEvent(QResizeEvent *event);
24 protected slots:
25     void timerTimeOut();
26 
27 private:
28     QTimer *timer;
29     QPoint point;
30     int i_diameter=0;
31     double d_angle=0;
32     QList<QPixmap> list_pixmap;
33 };
34 
35 #endif // SCANNERITEM_H
View Code
scanneritem.cpp
 1 #include "scanneritem.h"
 2 
 3 Widget::Widget(QWidget *parent) :
 4     QWidget(parent)
 5 {
 6     this->setStyleSheet("background-color:black");//设置窗口背景色为黑色
 7     timer = new QTimer();
 8     connect(timer,SIGNAL(timeout()),this,SLOT(timerTimeOut()));
 9 
10     timer->start(10);
11 }
12 
13 Widget::~Widget()
14 {
15 
16 }
17 
18 void Widget::resizeEvent(QResizeEvent *event)
19 {
20     if(this->width() > this->height()){
21         point = QPoint((this->width()-this->height())/2+(this->height()-12)/2,this->height()/2);
22         i_diameter = this->height()-12;
23     }else{
24         point = QPoint(this->width()/2,(this->height()-this->width())/2+(this->width()-12)/2);
25         i_diameter = this->width()-12;
26     }
27 }
28 
29 QPixmap Widget::paintWidget()
30 {
31     QPixmap pixmap(this->width(),this->height());
32     QPainter p_painter(&pixmap);
33     QPen pen;
34     QTime timedebug;
35     timedebug.start();
36     //反锯齿
37     p_painter.setRenderHint(QPainter::Antialiasing);
38     pixmap.fill(Qt::black);
39 
40     pen.setColor(Qt::green);
41     p_painter.setPen(pen);
42 
43     pen.setColor(Qt::gray);
44     p_painter.setPen(pen);
45     p_painter.drawLine(point.x(),point.y()-i_diameter/2-10,point.x(),point.y()+i_diameter/2+10);//画坐标
46     p_painter.drawLine(point.x()-i_diameter/2-10,point.y(),point.x()+i_diameter/2+10,point.y());
47     pen.setColor(Qt::green);
48     p_painter.setPen(pen);
49     for(int i=0; i<6; i++){
50         p_painter.drawEllipse(point.x()-i_diameter*0.2*i/2,point.y()-i_diameter*0.2*i/2,i_diameter*0.2*i,i_diameter*0.2*i);
51     }
52 
53     QConicalGradient conical_gradient(point,(6.28-d_angle)/6.28*720);//定义圆心和渐变的角度
54     conical_gradient.setColorAt(0,Qt::green);
55     conical_gradient.setColorAt(0.2,QColor(255,255,255,0));
56     p_painter.setBrush(conical_gradient);
57     p_painter.drawEllipse(point.x()-i_diameter/2,point.y()-i_diameter/2,i_diameter,i_diameter);
58 
59 
60     qDebug()<<"time_end="<<timedebug.elapsed()<<"ms";
61 
62     return pixmap;
63 
64 }
65 
66 void Widget::timerTimeOut()
67 {
68     timer->start(10);
69     d_angle += 3.14/720;
70     if(d_angle >= 6.28){
71         d_angle = 0;
72     }
73     update();
74 
75 }
76 
77 void Widget::paintEvent(QPaintEvent *event)
78 {
79     QPainter p_painter(this);
80     //反锯齿
81     p_painter.setRenderHint(QPainter::Antialiasing, true);
82 
83     p_painter.drawPixmap(0,0,this->width(),this->height(),paintWidget());
84 
85     QWidget::paintEvent(event);
86 }
View Code