1,QPainter 绘制,先绘制这一堆蓝色的东西,
2,在用定时器:QTimer,配合绘制棕色的圆。用到取余,取整
#pragma once#include <QWidget>
#include <QPaintEvent>#include <QTimer>QT_BEGIN_NAMESPACE
namespace Ui { class RadarSearchBeam; };
QT_END_NAMESPACEclass RadarSearchBeam : public QWidget
{Q_OBJECTpublic:RadarSearchBeam(QWidget *parent = nullptr);~RadarSearchBeam();//绘图事件void paintEvent(QPaintEvent* event);//画横着一排圆圈void drawARow(QPainter &painter, QPointF center,int width, int height);//画竖着的几排void drawAColumn(QPainter& painter, QPointF center, int width, int height);.//把上面两个方法封装了void drawSearchBeam(QPainter& painter, QPointF center, int width,int height);//画棕色的圆圈void drawAbearColorCircle(QPainter& painter, QPointF center, int width, int height);private:Ui::RadarSearchBeam *ui;int R = 10;int diameter = 2 * R;QTimer* timer; //定时器int timer_count;//定时器计数
};
cpp文件
#include "RadarSearchBeam.h"
#include "ui_RadarSearchBeam.h"
#include<QPainter>
#include<QDebug>
RadarSearchBeam::RadarSearchBeam(QWidget *parent): QWidget(parent), ui(new Ui::RadarSearchBeam())
{ui->setupUi(this);//使用定时器timer_count = 0;timer = new QTimer(this);connect(timer, &QTimer::timeout, [=] {timer_count++;qDebug() << timer_count << "dfddd";});timer->start(100);
}
RadarSearchBeam::~RadarSearchBeam()
{delete ui;
}
void RadarSearchBeam::paintEvent(QPaintEvent* event)
{Q_UNUSED(event);QPainter painter(this);// 设置画笔颜色painter.setPen(QColor(0, 160, 230));//把坐标原点移动到正中间painter.translate(width()/2,height()/2);drawSearchBeam(painter, QPointF(0,0),200,200);drawAbearColorCircle(painter, QPointF(0, 0), 200, 200);
}
void RadarSearchBeam::drawSearchBeam(QPainter& painter, QPointF center, int width, int height)
{//把坐标原点(正中间)移动到矩形左上角painter.translate(center.x() - width / 2, center.y() - height / 2);//把所有蓝色圆圈画完drawARow(painter, QPointF(0,0),width, height);drawAColumn(painter, QPointF(0, 0), width, height);
}
//配合定时器画移动的棕色圆圈
void RadarSearchBeam::drawAbearColorCircle(QPainter& painter, QPointF center, int width, int height)
{//一排几个圆=宽/直径int row_circle_num = width / diameter;//一竖几个圆=高/直径int column_circle_num = height / diameter;//总共多少个圆int all_circle_um = row_circle_num * column_circle_num;//这两个是配合定时器用int move_distance_x = diameter;int move_distance_y = diameter;//计时数小于圆圈数if (timer_count < all_circle_um){int current_column_num = timer_count % row_circle_num;;int current_row_num = timer_count / row_circle_num;画棕色的圆圈painter.setPen(QColor(185, 122, 87));painter.drawEllipse(QPoint(center.x() + move_distance_x*current_column_num, center.y()+ current_row_num* move_distance_y), R, R);//刷新绘图事件update();}else{// //计时数大于圆圈数,把计时数清零,从新计数timer_count = 0;update();}}//画一排
void RadarSearchBeam::drawARow(QPainter& painter, QPointF center,int width, int height)
{//一排int row_num = width /diameter;for (int i = 0; i < row_num; i++){painter.drawEllipse(QPoint(center.x()+i*diameter, center.y()), R, R);}
}//竖着画几排
void RadarSearchBeam::drawAColumn(QPainter& painter,QPointF center, int width, int height)
{int num = height / diameter;for (int i = 0; i < num; i++){drawARow(painter, QPointF(center.x(), center.y()+ i* diameter), width, height);qDebug() << QPointF(center.x(), center.y() + i * diameter);}
}