耗时IO对象移到子线程,防止UI卡顿

调用QObject::moveToThread,把m_SerialPort的 "线程归属" 从主线程改为m_serialThread;

此后m_SerialPort的所有槽函数(如果连接了信号)都会在m_serialThread中执行,而非主线程;

注意:移线程的前提是m_SerialPort无父对象(如果有父对象,moveToThread会失效),这也是代码中new QSerialPort()不指定父对象的原因。

cpp 复制代码
Q_DECLARE_METATYPE(CheckInfoList)
CommSerialPort::CommSerialPort()
{
    m_SerialPort = new QSerialPort();//不指定父对象
    //注册自定义元类型(跨线程信号槽传递数据用)
    qRegisterMetaType<QList<std::pair<int, int>>>("QList<std::pair<int,int>>");
    qRegisterMetaType<CheckInfoList>("QList<std::pair<int,int>>");

    // 创建工作线程
    m_serialThread = new QThread();
    // 注意:不要连接 finished 到 deleteLater,因为我们在析构函数中手动管理线程生命周期
    
    // 只将串口对象移到工作线程(不要移动 this)
    m_SerialPort->moveToThread(m_serialThread);
    m_serialThread->start();
    
    // 初始化发送时间记录器
    m_lastSendTime.start();

    // 自动重连:定时器在主线程中(因为 this 在主线程)
    m_reconnectTimer = new QTimer(this);
    m_reconnectTimer->setInterval(m_reconnectIntervalMs);
    connect(m_reconnectTimer, &QTimer::timeout, this, &CommSerialPort::onReconnectTimeout);
    
    // 串口错误信号连接(跨线程连接,使用 Qt::QueuedConnection)
    connect(m_SerialPort, &QSerialPort::errorOccurred, this, &CommSerialPort::onErrorOccurred, Qt::QueuedConnection);
    connect(m_SerialPort, &QSerialPort::errorOccurred, this, &CommSerialPort::handleSerialTimeout, Qt::QueuedConnection);

    // m_sendTimer = new QTimer(this);
    // m_sendTimer->setInterval(m_sendIntervalMs);
    // connect(m_sendTimer, &QTimer::timeout, this, &CommSerialPort::SendBufferData);
    // m_sendTimer->start();

    m_commAlertManager = new AlertDialogManager();
}

qt官方:QTimer is a subclass of QObject and as such, it must live in the thread where it was created. It is not safe to access a QTimer from another thread. (QTimer是QObject子类,必须归属创建它的线程;从其他线程访问QTimer是不安全的)

m_reconnectTimer自动重连定时器的onReconnectTimeout重连逻辑已经通过connect将m_reconnectTimer和CommSerialPort绑定了 ,如果后续出现串口错误信号连接没有使用QueuedConnection,会让导致不在同一个线程访问QTimer ,qt没有对访问不同线程中的QTimer设置线程安全 ,可能会导致定时器定时不准,导致后续定时器可能失效

Qt 对这种跨线程操作QTimer的行为不做线程安全保护:

轻量问题:定时器定时不准、启动 / 停止指令无响应(比如调用stop()但定时器还在跑);

严重问题:主线程和子线程同时操作定时器的内部状态,引发内存竞态,程序崩溃。

相关推荐
youtootech5 小时前
HarmonyOS《柚兔学伴》项目实战09-待办列表 UI——List+ForEach+滑动删除
数据结构·ui·华为·list·harmonyos
ndsc_d15 小时前
前端实战复盘:用AI直接生成响应式官网落地页与React源码
前端·人工智能·react.js·ui·前端框架·aigc·paico
monsion18 小时前
AG-UI 协议详解:Agent 与用户之间缺失的那一层
人工智能·ui·个人开发
Full Stack Developme19 小时前
flowable-spring-boot-starter 与 flowable-ui
ui
天天进步201520 小时前
UI-TARS 源码解析 #5:prompt.py 解析:COMPUTER_USE、MOBILE_USE、GROUNDING 三种提示词模板
ui·prompt
一键生成网站20 小时前
AI生成UI原型支持复杂逻辑交互工具对比与场景适配分析
人工智能·ui·交互·
糯米导航1 天前
AI 视觉回归实战:截图对比不是“找不同”,如何让智能差异分析真正服务 UI 质量
人工智能·ui·回归
CedarQR2 天前
VS Code Copilot Chat 配置 MCP Server 全攻略(以 Next AI Draw.io 为例)
人工智能·ui·json
console.log('npc')2 天前
网页移动端UI/UX Pro Max - 设计智能skill
前端·人工智能·ui·ux