在编写代码时,无意弄出来了一个这个东西,觉得挺有意思的记录一下,类似乒乓球在矩形内一直运动碰撞反弹demo
头文件
#ifndef MYPROJECT_H
#define MYPROJECT_H
#include <QMainWindow>
#include <QPainter>
#include "form.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MyProject; }
QT_END_NAMESPACE
class MyProject : public QMainWindow
{
Q_OBJECT
public:
MyProject(QWidget *parent = nullptr);
~MyProject();
Form *m_form;
public:
QTimer *timer;
int m_x;
int m_y;
int m_type_x;
int m_type_y;
private slots:
void updateProgressBar();
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
private:
Ui::MyProject *ui;
};
#endif // MYPROJECT_H
#include "myproject.h"
#include "ui_myproject.h"
#include <QDebug>
MyProject::MyProject(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MyProject)
,m_y(1)
,m_x(1)
,m_type_x(0)
,m_type_y(0)
{
ui->setupUi(this);
// 初始化定时器
timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &MyProject::updateProgressBar);
timer->setInterval(100);
timer->start();
ui->label->setGeometry(this->rect());
ui->label->setStyleSheet("background:transparent");
QPixmap pixmap(ui->label->size()); // 创建一个与QLabel大小相同的QPixmap
QPainter painter(&pixmap);
painter.setRenderHint(QPainter::Antialiasing); // 抗锯齿
painter.setPen(QPen(Qt::black, 2)); // 设置画笔
painter.setBrush(Qt::lightGray); // 设置画刷
QRect rect(100, 100, ui->label->width()-200, ui->label->height()-200); // 矩形的位置和大小
// 定义一个线段,从(50, 50)到(200, 200)
int startX = (ui->label->width() / 20.0) * 1;
int startY = ((ui->label->height()) / 10.0) * 1;
int endX = (ui->label->width() / 20.0) * 1;
int endY = ((ui->label->height()) / 10.0) * 9;
painter.drawLine(startX, startY, endX, endY);
startX = (ui->label->width() / 20.0) * 1;
startY = ((ui->label->height()) / 10.0) * 9;
endX = (ui->label->width() / 20.0) * 19;
endY = ((ui->label->height()) / 10.0) * 9;
painter.drawLine(startX, startY, endX, endY);
painter.setPen(QPen(Qt::black, 2)); // 设置画笔
for(int i = 1; i < 10; i++)
{
startX = (ui->label->width() / 20.0) * 1;
startY = ((ui->label->height()) / 10.0) * i;
endX = (ui->label->width() / 20.0) * 19;
endY = ((ui->label->height()) / 10.0) * i;
if(i == 1 || i == 9)
painter.drawLine(startX, startY, endX, endY);
}
for(int i = 1; i < 20; i++)
{
startX = (ui->label->width() / 20.0) * i;
startY = ((ui->label->height()) / 10.0) * 1;
endX = (ui->label->width() / 20.0) * i;
endY = ((ui->label->height()) / 10.0) * 9;
if(i == 1 || i == 19)
painter.drawLine(startX, startY, endX, endY);
}
ui->label->setPixmap(pixmap); // 将QPixmap设置为QLabel的内容
}
MyProject::~MyProject()
{
delete ui;
}
void MyProject::updateProgressBar()
{
ui->label->setGeometry(this->rect());
QPixmap pixmap(ui->label->size()); // 创建一个与QLabel大小相同的QPixmap
QPainter painter(&pixmap);
painter.setRenderHint(QPainter::Antialiasing); // 抗锯齿
painter.setPen(QPen(Qt::black, 2)); // 设置画笔
painter.setBrush(Qt::lightGray); // 设置画刷
QRect rect(100, 100, ui->label->width()-200, ui->label->height()-200); // 矩形的位置和大小
// 定义一个线段,从(50, 50)到(200, 200)
int startX = (ui->label->width() / 20.0) * 1;
int startY = ((ui->label->height()) / 10.0) * 1;
int endX = (ui->label->width() / 20.0) * 1;
int endY = ((ui->label->height()) / 10.0) * 9;
painter.drawLine(startX, startY, endX, endY);
startX = (ui->label->width() / 20.0) * 1;
startY = ((ui->label->height()) / 10.0) * 9;
endX = (ui->label->width() / 20.0) * 19;
endY = ((ui->label->height()) / 10.0) * 9;
painter.drawLine(startX, startY, endX, endY);
painter.setPen(QPen(Qt::black, 2)); // 设置画笔
for(int i = 1; i < 10; i++)
{
startX = (ui->label->width() / 20.0) * 1;
startY = ((ui->label->height()) / 10.0) * i;
endX = (ui->label->width() / 20.0) * 19;
endY = ((ui->label->height()) / 10.0) * i;
if(i == 1 || i == 9)
painter.drawLine(startX, startY, endX, endY);
}
for(int i = 1; i < 20; i++)
{
startX = (ui->label->width() / 20.0) * i;
startY = ((ui->label->height()) / 10.0) * 1;
endX = (ui->label->width() / 20.0) * i;
endY = ((ui->label->height()) / 10.0) * 9;
if(i == 1 || i == 19)
painter.drawLine(startX, startY, endX, endY);
}
painter.setPen(QPen(Qt::red, 5)); // 设置画笔
painter.setBrush(Qt::lightGray); // 设置画刷
startX = (ui->label->width() / 20.0) * m_x;
startY = ((ui->label->height()) / 10.0) * m_y;
endX = (ui->label->width() / 20.0) * (m_x+1);
endY = ((ui->label->height()) / 10.0) * (m_y+1);
if(m_y >= 8)
{
m_type_y = 1;
}
else if(m_y <= 1)
{
m_type_y = 0;
}
if(m_x >= 18)
{
m_type_x = 1;
}
else if(m_x <= 1)
{
m_type_x = 0;
}
if(m_type_y)
{
m_y--;
}
else
{
m_y++;
}
if(m_type_x)
{
m_x--;
}
else
{
m_x++;
}
QBrush redBrush(Qt::red);
painter.fillRect(startX, startY, (ui->label->width() / 20.0), (ui->label->height()) / 10.0, redBrush);
ui->label->setPixmap(pixmap);
}
void MyProject::on_pushButton_clicked()
{
timer->start();
}
void MyProject::on_pushButton_2_clicked()
{
timer->stop();
}
void MyProject::on_pushButton_3_clicked()
{
}
运行效果: