QT线程之掷色子游戏

墨蓝 2022-03-18 09:50 333阅读 0赞

所有文件
在这里插入图片描述
dialog.h文件

  1. #ifndef DIALOG_H
  2. #define DIALOG_H
  3. #include <QDialog>
  4. #include "qdicethread.h"
  5. namespace Ui {
  6. class Dialog;
  7. }
  8. class Dialog : public QDialog
  9. {
  10. Q_OBJECT
  11. private:
  12. QDiceThread threadA;
  13. protected:
  14. void closeEvent(QCloseEvent *event);
  15. public:
  16. explicit Dialog(QWidget *parent = 0);
  17. ~Dialog();
  18. private slots:
  19. void onthreadA_start();
  20. void onthreadA_finish();
  21. void onThreadA_newValue(int seq,int diceValue);
  22. void on_btnThreadBegin_clicked();
  23. void on_btnThreadStop_clicked();
  24. void on_btnDicestart_clicked();
  25. void on_btnDiceStop_clicked();
  26. void on_btnClear_clicked();
  27. private:
  28. Ui::Dialog *ui;
  29. };
  30. #endif // DIALOG_H

qdicethread.h文件

  1. #ifndef QDICETHREAD_H
  2. #define QDICETHREAD_H
  3. #include <QThread>
  4. class QDiceThread : public QThread
  5. {
  6. Q_OBJECT
  7. private:
  8. int m_seq=0;//掷色子次数序号
  9. int m_diceValue;//色子点数
  10. bool m_Paused=true;//暂停
  11. bool m_stop=false;//停止
  12. protected:
  13. void run() Q_DECL_OVERRIDE;//线程任务
  14. public:
  15. QDiceThread();
  16. void diceBegin();//掷色子
  17. void dicePause();//暂停
  18. void stopThread();//结束线程
  19. signals:
  20. void newValue(int seq,int diceValue);//产生新点数的信号
  21. };
  22. #endif // QDICETHREAD_H

dialog.cpp文件

  1. #include "dialog.h"
  2. #include "ui_dialog.h"
  3. #include <QDebug>
  4. Dialog::Dialog(QWidget *parent) :
  5. QDialog(parent),
  6. ui(new Ui::Dialog)
  7. {
  8. ui->setupUi(this);
  9. if(threadA.isRunning())
  10. {
  11. qDebug("thread is running");
  12. }
  13. else
  14. {
  15. qDebug("thread is stopped");
  16. }
  17. connect(&threadA,SIGNAL(started()),this,SLOT(onthreadA_start()));
  18. connect(&threadA,SIGNAL(finished()),this,SLOT(onthreadA_finish()));
  19. connect(&threadA,SIGNAL(newValue(int,int)),this,SLOT(onThreadA_newValue(int,int)));
  20. }
  21. Dialog::~Dialog()
  22. {
  23. delete ui;
  24. }
  25. void Dialog::onthreadA_start()
  26. {
  27. ui->label_2->setText("Thread状态:thread start.");
  28. if(threadA.isRunning())
  29. {
  30. qDebug("thread is running");
  31. }
  32. else
  33. {
  34. qDebug("thread is stopped");
  35. }
  36. }
  37. void Dialog::onthreadA_finish()
  38. {
  39. ui->label_2->setText("Thread状态:thread finish.");
  40. if(threadA.isRunning())
  41. {
  42. qDebug("thread is running");
  43. }
  44. else
  45. {
  46. qDebug("thread is stopped");
  47. }
  48. }
  49. void Dialog::onThreadA_newValue(int seq, int diceValue)
  50. {
  51. QString str=QString::asprintf("第%d次,点数为%d",seq,diceValue);
  52. ui->plainTextEdit->appendPlainText(str);
  53. QPixmap pic;
  54. QString fileName=QString::asprintf(":/dice/images/d%d.jpg",diceValue);
  55. pic.load(fileName);
  56. ui->label->setPixmap(pic);
  57. }
  58. void Dialog::on_btnThreadBegin_clicked()
  59. {
  60. threadA.start();
  61. ui->btnThreadBegin->setEnabled(false);
  62. ui->btnThreadStop->setEnabled(true);
  63. ui->btnDiceStop->setEnabled(false);
  64. ui->btnDicestart->setEnabled(true);
  65. }
  66. void Dialog::on_btnThreadStop_clicked()
  67. {
  68. threadA.stopThread();
  69. threadA.wait();
  70. ui->btnThreadBegin->setEnabled(true);
  71. ui->btnThreadStop->setEnabled(false);
  72. ui->btnDiceStop->setEnabled(false);
  73. ui->btnDicestart->setEnabled(false);
  74. }
  75. void Dialog::on_btnDicestart_clicked()
  76. {
  77. threadA.diceBegin();
  78. ui->btnDicestart->setEnabled(false);
  79. ui->btnDiceStop->setEnabled(true);
  80. }
  81. void Dialog::on_btnDiceStop_clicked()
  82. {
  83. threadA.dicePause();
  84. ui->btnDicestart->setEnabled(true);
  85. ui->btnDiceStop->setEnabled(false);
  86. }
  87. void Dialog::on_btnClear_clicked()
  88. {
  89. ui->plainTextEdit->clear();
  90. }
  91. void Dialog::closeEvent(QCloseEvent *event)
  92. {
  93. if(threadA.isRunning())
  94. {
  95. threadA.stopThread();
  96. threadA.wait();
  97. }
  98. event->accept();
  99. }

qdicethread.cpp文件

  1. #include "qdicethread.h"
  2. #include <QTime>
  3. QDiceThread::QDiceThread()
  4. {
  5. }
  6. void QDiceThread::diceBegin()
  7. {
  8. m_Paused=false;
  9. }
  10. void QDiceThread::dicePause()
  11. {
  12. m_Paused=true;
  13. }
  14. void QDiceThread::stopThread()
  15. {
  16. m_stop=true;
  17. }
  18. void QDiceThread::run()
  19. {
  20. m_stop=false;
  21. m_seq=0;
  22. qsrand(QTime::currentTime().msec());
  23. while(!m_stop)
  24. {
  25. if (!m_Paused)
  26. {
  27. m_seq++;
  28. m_diceValue=qrand();
  29. m_diceValue=m_diceValue%6+1;
  30. emit newValue(m_seq,m_diceValue);
  31. }
  32. msleep(500);
  33. }
  34. quit();
  35. }

dialog.ui

在这里插入图片描述运行结果

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,333人围观)

还没有评论,来说两句吧...

相关阅读

    相关 jquery实现筛子小游戏

    本篇给大家分享一个很好玩的掷筛子游戏,当点击“掷筛子”按钮时张三和李四两人同时掷出筛子,在各自的文本框中会显示出各自掷出筛子的大小,并且会在第三个文本框中比较出两人点数的大小,