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

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

相关推荐
xy345316 小时前
Axure 9.0 中继器创建与设置步骤
前端·ui·html·axure·原型·产品设计
大蓝头1 天前
安装包UI美化之路-借助AI快速使用nsNiuniuSkin制作安装包
ui·安装包·nsis·安装包美化·nsniuniuskin
兰亭妙微UI设计公司1 天前
兰亭妙微APP界面设计公司分享: iPhone Duo 登场|3 大核心要点,拆解折叠屏 UI 适配设计思路
ui·交互
智购科技自动售货机工厂2 天前
2026自动售货机端侧AI降本逻辑:从云端API到本地推理的成本重构~YH
人工智能·python·ui·面试·交互
HwJack202 天前
【HarmonyOS开发小实践】ArkUI 应用级状态AppStorage 与跨页面共享、持久化
ui·华为·性能优化·harmonyos
undsky_3 天前
传统色 × 现代和弦:专为 UI/UX 打造的国风配色 Skill
ui·ai·aigc·ai编程·ux
Wang's Blog4 天前
Java框架快速入门: Spring Security+OAuth2之多因子认证与TOTP实战
java·spring·ui
Huanzhi_Lin5 天前
UI业务模块化随笔
ui·mvc·mvp·ecs·ui框架·模块化开发
Quor5 天前
Zorv AI GenUI 技术架构深度解析:从双面设计到安全边界
人工智能·ui·架构