Qt实现大转盘
效果图
图片资源
代码
mainwindows.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
#include <QMouseEvent>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
Q_PROPERTY(int rotate READ getRotate WRITE setRotate)
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
int getRotate();
void setRotate(int rotate);
private:
void initControl();
void paintEvent(QPaintEvent *event);
void mousePressEvent(QMouseEvent *event);
public:
QString LotterData[18] = {QStringLiteral("奖品1"),QStringLiteral("奖品2"),QStringLiteral("奖品2"),
QStringLiteral("奖品3"),QStringLiteral("奖品3"),QStringLiteral("奖品4"),
QStringLiteral("奖品4"),QStringLiteral("奖品5"),QStringLiteral("奖品5"),
QStringLiteral("奖品6"),QStringLiteral("奖品6"),QStringLiteral("奖品7"),
QStringLiteral("奖品7"),QStringLiteral("奖品8"),QStringLiteral("奖品8"),
QStringLiteral("奖品9"),QStringLiteral("奖品9"),QStringLiteral("奖品1")};
private:
Ui::MainWindow *ui;
private slots:
void onRotateFinished();
private:
int m_rotation;
QRect m_pointerRect;
};
#endif // MAINWINDOW_H
mainwindows.c
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>
#include <QPixmap>
#include <QMouseEvent>
#include <QTime>
#include <QDebug>
#include <QPropertyAnimation>
#include <QMessageBox>
#include <QRandomGenerator>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, m_rotation(0)
{
ui->setupUi(this);
this->resize(800,480);
initControl();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::initControl()
{
setMouseTracking(true);
}
int MainWindow::getRotate()
{
return m_rotation;
}
void MainWindow::setRotate(int rotate)
{
m_rotation = rotate;
update();
}
void MainWindow::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::SmoothPixmapTransform, true); //使绘制平滑
painter.save();
//绘制背景图
QPixmap pixmap = QPixmap(":/image/chassis.png");
//中心偏移至中心位置
painter.translate(this->width()/2, this->height()/2);
//旋转m_rotation角度
painter.rotate(-1*m_rotation);
painter.drawPixmap(QRect(0-pixmap.width()/2, 0-pixmap.height()/2, pixmap.width(), pixmap.height()), pixmap);
painter.restore();
//绘制指针
QPixmap pointer = QPixmap(":/image/pointer.png");
m_pointerRect = QRect((this->width()-pointer.width()+40)/2, (this->height()-pointer.height()+40)/2, pointer.width()-40,pointer.height()-40);
painter.drawPixmap(m_pointerRect, pointer);
QMainWindow::paintEvent(event);
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
if(qApp->mouseButtons() == Qt::LeftButton)
{
QRegion ellipseRegion(m_pointerRect,QRegion::Ellipse);
if(ellipseRegion.contains(event->pos()))
{
quint32 randomNumber = QRandomGenerator::global()->bounded(360);
int rotateRand = randomNumber + 360*5;
m_rotation = 0;
QPropertyAnimation *animation = new QPropertyAnimation(this, "rotate");
animation->setEasingCurve(QEasingCurve::OutCubic);
animation->setDuration(5000);
animation->setStartValue(0);
animation->setEndValue(rotateRand);
connect(animation, SIGNAL(finished()), this, SLOT(onRotateFinished()));
animation->start(QAbstractAnimation::DeleteWhenStopped);
}
}
QMainWindow::mousePressEvent(event);
}
void MainWindow::onRotateFinished()
{
float rotation = m_rotation - 360*5;
int currentIndex = 0;
for(int index=0; index<18; index++)
{
rotation -= 20;
if(rotation <=0)
{
currentIndex = index;
break;
}
}
QMessageBox msgBox;
QString message = QStringLiteral("恭喜中奖<%1>").arg(LotterData[currentIndex]);
msgBox.setText(message);
msgBox.exec();
}
main.c
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
记录
主要运用QPropertyAnimation类实现底盘旋转效果