• 掷骰子的多线程应用程序1(复现《Qt C++6.0》)


    说明:复现的代码来自《Qt C++6.0》P496-P500。在复现时完全按照代码,出现了两处报错:

    (1)ui指针(2)按钮的响应函数。下面程序对以上问题进行了修改。除了图片、清空、关闭功能外,其他功能实现复现。


    主线程:

    .h文件

    1. #pragma once
    2. #include
    3. #include "ui_ThreadTest_one.h"
    4. #include "QtClass.h"
    5. class ThreadTest_one : public QMainWindow//主线程
    6. {
    7. Q_OBJECT
    8. private:
    9. QtClass* threadA;
    10. protected:
    11. void closeEvent(QCloseEvent* event);
    12. public:
    13. ThreadTest_one(QWidget* parent = nullptr);
    14. ~ThreadTest_one();
    15. private:
    16. Ui::ThreadTest_oneClass ui;
    17. public slots:
    18. void do_threadA_started();
    19. void do_threadA_finished();
    20. void do_threadA_newValue(int seq, int diceValue);
    21. void on_actThread_Run_clicked();
    22. void on_actDice_Run_clicked();
    23. void on_actThread_Quit_clicked();
    24. void on_actDict_Pause_clicked();
    25. };

    .cpp文件

    1. #include "ThreadTest_one.h"
    2. #include <QCloseEvent>
    3. #include <QThread>
    4. ThreadTest_one::ThreadTest_one(QWidget *parent)
    5. : QMainWindow(parent)
    6. {
    7. ui.setupUi(this);
    8. threadA = new QtClass(this);//创建工作线程
    9. connect(threadA, &QtClass::started, this, &ThreadTest_one::do_threadA_started);
    10. connect(threadA, &QtClass::finished, this, &ThreadTest_one::do_threadA_finished);
    11. connect(threadA, &QtClass::newValue, this, &ThreadTest_one::do_threadA_newValue);
    12. }
    13. void ThreadTest_one::do_threadA_started() {
    14. ui.statusBar->showMessage("Thread状态:thread start");
    15. ui.actThread_Run->setEnabled(false);
    16. ui.actThread_Quit->setEnabled(true);
    17. ui.actDice_Run->setEnabled(true);
    18. }
    19. void ThreadTest_one::do_threadA_finished() {
    20. ui.statusBar->showMessage("Thread状态:thread finished");
    21. ui.actThread_Run->setEnabled(true);
    22. ui.actThread_Quit->setEnabled(false);
    23. ui.actDice_Run->setEnabled(false);
    24. ui.actDict_Pause->setEnabled(false);
    25. }
    26. void ThreadTest_one::do_threadA_newValue(int seq, int diceValue) {//与线程的newValue()信号相关联
    27. QString str = QString::asprintf("第%d次投骰子,点数为%d", seq, diceValue);
    28. ui.plainTextEdit->appendPlainText(str);
    29. }
    30. //按键的槽函数
    31. void ThreadTest_one::on_actThread_Run_clicked() {//要用clicked才能得到响应
    32. threadA->start();
    33. }
    34. void ThreadTest_one::on_actThread_Quit_clicked() {
    35. threadA->stopThread();
    36. }
    37. void ThreadTest_one::on_actDice_Run_clicked() {
    38. threadA->diceBegin();
    39. ui.actDice_Run->setEnabled(false);
    40. ui.actDict_Pause->setEnabled(true);
    41. }
    42. void ThreadTest_one::on_actDict_Pause_clicked() {
    43. threadA->dicePause();
    44. ui.actThread_Run->setEnabled(true);
    45. ui.actDict_Pause->setEnabled(false);
    46. }
    47. //重定义事件处理函数,确保窗口关闭时线程被停止
    48. void ThreadTest_one::closeEvent(QCloseEvent* event) {
    49. if (threadA->isRunning()) {
    50. threadA->terminate();
    51. threadA->wait();
    52. }
    53. event->accept();
    54. }
    55. ThreadTest_one::~ThreadTest_one()
    56. {}

    工作线程:

    .h文件

    1. #pragma once
    2. #include
    3. class QtClass : public QThread
    4. {
    5. Q_OBJECT
    6. public:
    7. QtClass(QObject *parent);
    8. ~QtClass();
    9. private:
    10. int m_seq = 0; //掷骰子次数的序号
    11. int m_diceValue;//骰子的点数
    12. bool m_paused = true;//暂停投骰子
    13. bool m_stop = false;//停止线程
    14. protected:
    15. void run();//线程的任务
    16. public:
    17. void diceBegin();//开始掷骰子
    18. void dicePause();//暂停投骰子
    19. void stopThread();//停止线程
    20. signals:
    21. void newValue(int seq, int diceValue);//产生新点数的信号
    22. };

    .cpp文件

    1. #include "QtClass.h"
    2. #include<QRandomGenerator>
    3. #include<QThread>
    4. QtClass::QtClass(QObject *parent)
    5. : QThread(parent)
    6. {}
    7. void QtClass::diceBegin() {//开始掷骰子
    8. m_paused = false;
    9. }
    10. void QtClass::dicePause() {//停止掷骰子
    11. m_paused = true;
    12. }
    13. void QtClass::stopThread() {//停止线程
    14. m_stop = true;
    15. }
    16. void QtClass::run() {//run函数处理事件循环
    17. m_stop = false;
    18. m_paused = true;
    19. m_seq = 0;
    20. while (!m_stop) {
    21. if (!m_paused) {
    22. m_diceValue = QRandomGenerator::global()->bounded(1, 7);
    23. m_seq++;
    24. emit newValue(m_seq, m_diceValue);
    25. }
    26. msleep(500);
    27. }
    28. quit();//退出线程
    29. }
    30. QtClass::~QtClass()
    31. {}

  • 相关阅读:
    tomcat自动启动的问题
    Web服务(Web Service)
    照片相似性搜索引擎Embed-Photos;赋予大型语言模型(LLMs)视频和音频理解能力;OOTDiffusion的基础上可控制的服装驱动图像合成
    python解析xml遇到的问题分享(命名空间有关)
    迷宫_随机实验_边做边学深度强化学习:PyTorch程序设计实践(1)
    669. 修剪二叉搜索树 ●●
    第22章_瑞萨MCU零基础入门系列教程之DMA控制器
    OpenCV“迷雾”车道识别的反思
    TS第一讲-----基础类型
    Linux命令行管理文件(练习题)
  • 原文地址:https://blog.csdn.net/black_procedure/article/details/132984423