• Qt超时机制设计


    解决的问题:

    上位机软件开发过程中,你难免不得等待下位机上传的数据,当没有信息上传,超时机制就能保证上位机不会卡死。那么在实际使用中,一般我们怎么添加呢?


    如何实现:

    Qt中自带QTimer类,如果你只是简单的做个定时器,直接到主线程GUI线程中进行定时炒作即可,但是经常会卡主界面,那么我们需要怎么在子线程中进行定时器开启呢?


    代码块解析:

    timeoutmannager.h

    1. /*********************************************************************************
    2. *Author:zhouFuLiang
    3. *Date: 2023-09-14 20:50
    4. *Description: 超时机制功能
    5. **********************************************************************************/
    6. #ifndef TIMEOUTMANAGER_H
    7. #define TIMEOUTMANAGER_H
    8. #include
    9. #include
    10. #include
    11. #define Max_Count 2
    12. class TimeoutManager : public QObject
    13. {
    14. Q_OBJECT
    15. public:
    16. explicit TimeoutManager(QObject *parent = 0);
    17. private:
    18. void initConnect();
    19. signals:
    20. void commandException(int, int); //命令执行超时
    21. public slots:
    22. void timerOutSlot();
    23. void threadStartRun();
    24. void threadStopRun();
    25. void destoryResource();
    26. void onSetCurrentCommand(int cm, int cpr);
    27. private:
    28. QThread *m_pThread;
    29. QTimer *m_pTimer;
    30. int m_nCount;
    31. int m_currProcess;
    32. int m_currCommand;
    33. };
    34. #endif // TIMEOUTMANAGER_H

    timeoutmannager.cpp

    1. #include "timeoutmanager.h"
    2. #include
    3. TimeoutManager::TimeoutManager(QObject *parent) : QObject(parent)
    4. ,m_pThread(nullptr)
    5. ,m_nCount(0)
    6. ,m_currCommand(-1)
    7. ,m_currProcess(-1)
    8. {
    9. m_pThread = new QThread;
    10. m_pTimer = new QTimer;
    11. moveToThread(m_pThread);
    12. m_pTimer->moveToThread(m_pThread);
    13. m_pThread->start();
    14. initConnect();
    15. }
    16. void TimeoutManager::threadStartRun()
    17. {
    18. m_pTimer->start(1000);
    19. }
    20. void TimeoutManager::threadStopRun()
    21. {
    22. m_pTimer->stop();
    23. m_pTimer->deleteLater();
    24. }
    25. void TimeoutManager::destoryResource()
    26. {
    27. m_pThread->quit();
    28. m_pThread->wait();
    29. }
    30. void TimeoutManager::onSetCurrentCommand(int cm, int cpr)
    31. {
    32. this->m_currCommand = cm;
    33. this->m_currProcess = cpr;
    34. }
    35. void TimeoutManager::initConnect()
    36. {
    37. connect(qApp, &QApplication::aboutToQuit, this, &TimeoutManager::destoryResource, Qt::DirectConnection);
    38. connect(m_pTimer, &QTimer::timeout, this, &TimeoutManager::timerOutSlot, Qt::DirectConnection);
    39. connect(m_pThread, &QThread::started, this, &TimeoutManager::threadStartRun, Qt::DirectConnection);
    40. connect(m_pThread, &QThread::finished, this, &TimeoutManager::threadStopRun, Qt::DirectConnection);
    41. }
    42. void TimeoutManager::timerOutSlot()
    43. {
    44. if(-1 == m_currCommand)
    45. return;
    46. m_nCount++;
    47. if (m_nCount > Max_Count) {
    48. m_nCount = 0;
    49. emit commandException(m_currCommand,m_currProcess);
    50. qDebug() << "trigger signal command: " <<QString("0x%1").arg(m_currCommand ,2, 16, QChar('0')) << " process: " << QString("0x%1").arg(m_currProcess ,2, 16, QChar('0'));
    51. m_currCommand = -1;
    52. m_currProcess = -1;
    53. }
    54. }

    main函数中

    connect 这个onSetCurrentCommand 槽函数,在启动时候,进行触发

    connect这个commandException信号,那么超时后就会触发

  • 相关阅读:
    CodeTON Round 3 (Div. 1 + Div. 2, Rated, Prizes!) A-D
    总结:Linux --help使用
    四川“828 B2B企业节”正式启动,助力四川中小企业成就好生意
    总结前端高并发优化方案
    Xilinx FPGA 使用Microblaze实现串口命令行
    网工内推 | 南天软件,base北京,需持有CCIE认证,最高25k
    SpringCloud Ribbon--负载均衡 原理及应用实例
    注册表的增删改查
    Prometheus 查询持久化
    《3D编程模式》写书-第1次记录
  • 原文地址:https://blog.csdn.net/u013351233/article/details/132982979