qfedu-basic-level/day12/qtdemo4/widget.cpp

48 lines
1.1 KiB
C++
Raw Normal View History

2023-06-28 23:02:58 +08:00
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
// 加载动画资源
yanhuagif = new QMovie(":/movies/yanhua");
srand(time(NULL));
randNum = rand() % 10; // 0-9的数
// 开启定时器, 用于定时换随机数
timer = new QTimer();
connect(timer,&QTimer::timeout,[&](){
randNum = rand() % 10;
});
timer->start(5*1000); // 5秒
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked()
{
guessNum = ui->numEdit->text().toInt();
qDebug() << "随机数:" << randNum << "输入的数" << guessNum << endl;
QString resStr = "随机数: "+ QString::number(randNum)+ ", 你输入的数: "+QString::number(guessNum);
ui->tipesLable->setText("本轮结果: " + resStr);
if(guessNum == randNum){
ui->resaultShowLabel->setMovie(yanhuagif);
yanhuagif->start();
} else {
if(yanhuagif->state() == QMovie::Running)
yanhuagif->stop();
ui->resaultShowLabel->setText(" ");
}
}