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

46 lines
1.0 KiB
C++
Executable File

#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;
if(guessNum == randNum){
ui->resaultShowLabel->setMovie(yanhuagif);
yanhuagif->start();
} else {
if(yanhuagif->state() == QMovie::Running)
yanhuagif->stop();
ui->resaultShowLabel->setText("你猜错了, 正确的结果是:"+ QString::number(randNum));
}
}