耗时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()但定时器还在跑);

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

相关推荐
ZC跨境爬虫17 小时前
跟着 MDN 学CSS day_44:响应式设计——让网页适配所有屏幕的完整指南
前端·css·ui·html·tensorflow
ZC跨境爬虫20 小时前
跟着 MDN 学CSS day_43:CSS布局挑战——从浮动到弹性盒与栅格的综合实践
前端·css·ui·html·tensorflow
夜空孤狼啸21 小时前
Vue Data UI:这不是图表库,是数据可视化 UI 平台
vue.js·ui·信息可视化
ZC跨境爬虫2 天前
跟着 MDN 学CSS day_37:(从文档流到粘性定位的底层原理)
前端·javascript·css·ui·html
G_dou_2 天前
Flutter三方库适配OpenHarmony【compass】罗盘 UI 项目完整实战
flutter·ui
ZC跨境爬虫2 天前
跟着 MDN 学CSS day_40:(Flexbox实战技能测试)
前端·css·ui·html·tensorflow
ZC跨境爬虫2 天前
跟着 MDN 学CSS day_36:(float、clear与BFC深度解析)
前端·javascript·css·ui·交互
豆豆2 天前
2026实测:AI生成UI设计稿后,如何优雅集成到PageAdmin CMS?(附标签替换代码)
人工智能·ui·cms·建站系统·ai工具·ai建站
爱吃大芒果2 天前
鸿蒙 ArkUI 架构蓝图:MoodLite 的 UI 渲染与数据逻辑解耦实践
ui·架构·harmonyos