qt波位图

时间:2024-04-16 20:37:03

在这里插入图片描述
1,QPainter 绘制,先绘制这一堆蓝色的东西,
2,在用定时器:QTimer,配合绘制棕色的圆。用到取余,取整

#pragma once

#include <QWidget>
#include <QPaintEvent>

#include <QTimer>

QT_BEGIN_NAMESPACE
namespace Ui { class RadarSearchBeam; };
QT_END_NAMESPACE

class RadarSearchBeam : public QWidget
{
	Q_OBJECT

public:
	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);
    }
}