Qt实现抽奖小游戏的三种方式

  #include "mainwindow.h"

  #include "ui_mainwindow.h"

  #include "DlgSetting.h"

  #include

  #include

  #include

  MainWindow::MainWindow(QWidget *parent) :

  QMainWindow(parent),

  ui(new Ui::MainWindow)

  {

  ui->setupUi(this);

  // m_pDlgSetting = new C_DlgSetting(this);

  // 信号和槽

  {

  connect(ui->action_quit, &QAction::triggered, this, &MainWindow::slot_actQuit_triggered);

  connect(ui->action_setting, &QAction::triggered, this, &MainWindow::slot_actSetting_triggered);

  connect(ui->btn_start, &QPushButton::clicked, this, &MainWindow::slot_btnStart_clicked);

  connect(ui->btn_stop, &QPushButton::clicked, this, &MainWindow::slot_btnStop__clicked);

  }

  // 声明随机数种子,不然就是伪随机(每次产生的随机数都一样)

  qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));

  }

  MainWindow::~MainWindow()

  {

  delete ui;

  }

  void MainWindow::slot_actQuit_triggered()

  {

  close();

  }

  void MainWindow::slot_actSetting_triggered()

  {

  // m_pDlgSetting->exec();

  }

  void MainWindow::slot_btnStart_clicked()

  {

  if(m_bFlag)

  {

  return;

  }

  QStringList sl;

  sl << "一等奖" << "二等奖" << "三等奖" << "四等奖" << "五等奖";

  m_bFlag = true;

  while (m_bFlag) {

  int nRange = qrand() % 5;

  ui->label_turn->setText(sl.at(nRange));

  // 100ms转一次

  QThread::msleep(100);

  // 防止界面卡死

  QCoreApplication::processEvents();

  }

  }

  void MainWindow::slot_btnStop__clicked()

  {

  m_bFlag = false;

  // 显示最终获奖结果

  QString strRes = QString("最终结果: %1").arg(ui->label_turn->text());

  ui->label_res->setText(strRes);

  }