• 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信号,那么超时后就会触发

  • 相关阅读:
    ps怎么替换颜色,自学ps软件photoshop2022,ps一张图片的一种颜色全部替换成另外一种颜色
    复杂度(7.23)
    【博学谷学习记录】超强总结,用心分享|架构师-nacos功能应用
    进程概念[上]
    STC89C52单片机 启动!!!(三)
    无代码开发重新指派负责人入门教程
    Vue2+elementui项目导出el-table的数据为xlsx表格
    理解Go语言延迟执行语句defer关键字
    Python中的进程池及进程池锁:multiprocessing.Pool及multiprocessing.Manager().Lock()
    012 C++ AVL_tree
  • 原文地址:https://blog.csdn.net/u013351233/article/details/132982979