#ifndef MYCOIN_H
#define MYCOIN_H
#include <QPushButton>
#ifdef WIN32
#pragma execution_character_set("utf-8")
#endif
class mycoin : public QPushButton
{
Q_OBJECT
public:
//explicit myCoin(QWidget *parent = nullptr);
mycoin(QString butImg);
void mousePressEvent(QMouseEvent *);
int posX; //x坐标
int posY; //y坐标
bool flag; //正反标志
void changeFlag();//改变标志,执行翻转效果
QTimer *timer1; //正面翻反面 定时器
QTimer *timer2; //反面翻正面 定时器
int min = 1; //最小图片
int max = 8; //最大图片
bool isAnimation = false;
bool isWin = false;//是否成功
signals:
public slots:
};
#endif // MYCOIN_H
#include "mycoin.h"
#include<QDebug>
#include<QTimer>
//myCoin::myCoin(QWidget *parent) : QWidget(parent)
//{
//}
mycoin::mycoin(QString butImg)
{
QPixmap pixmap;
bool ret = pixmap.load(butImg);
if(!ret)
{
qDebug() << butImg << "加载图片失败!";
}
this->setFixedSize( pixmap.width(), pixmap.height() );
this->setStyleSheet("QPushButton{border:0px;}");
this->setIcon(pixmap);
this->setIconSize(QSize(pixmap.width(),pixmap.height()));
//初始化定时器
timer1 = new QTimer(this);
timer2 = new QTimer(this);
//监听正面翻转的信号槽
connect(timer1,&QTimer::timeout,[=](){
QPixmap pixmap;
QString str = QString(":/res/Coin000%1.png").arg(this->min++);
pixmap.load(str);
this->setFixedSize(pixmap.width(),pixmap.height() );
this->setStyleSheet("QPushButton{border:0px;}");
this->setIcon(pixmap);
this->setIconSize(QSize(pixmap.width(),pixmap.height()));
if(this->min > this->max) //如果大于最大值,重置最小值,并停止定时器
{
this->min = 1;
timer1->stop();
this->isAnimation = false;
}
});
connect(timer2,&QTimer::timeout,[=](){
QPixmap pixmap;
QString str = QString(":/res/Coin000%1.png").arg((this->max)-- );
pixmap.load(str);
this->setFixedSize(pixmap.width(),pixmap.height() );
this->setStyleSheet("QPushButton{border:0px;}");
this->setIcon(pixmap);
this->setIconSize(QSize(pixmap.width(),pixmap.height()));
if(this->max < this->min) //如果小于最小值,重置最大值,并停止定时器
{
this->max = 8;
timer2->stop();
this->isAnimation = false;
}
});
}
void mycoin::changeFlag()
{
if(this->flag) //如果是正面
{
timer1->start(30);
this->flag = false;
this->isAnimation = true;
}
else
{
timer2->start(30);
this->flag = true;
this->isAnimation = true;
}
}
void mycoin::mousePressEvent(QMouseEvent *e)
{
if(this->isAnimation||isWin == true )
{
return;
}
else
{
return QPushButton::mousePressEvent(e);
}
}