QT报错QObject: Cannot create children for a parent that is in a different thread

完整报错:
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QSerialPort(0x17db1b8b820), parent’s thread is QThread(0x17dace5e740), current thread is SolarWorkerThread(0x17db02ee0f0)

  • 原因为在不同线程访问QSerialPort
  • 将原本的工作流程为在主线程中读,在子线程中写,将子线程的写修改为发送信号,让管理QSSerialPort的类接收信号,再调用槽函数去写即可

原本完整的工作流程为:
1. 在主线程中创建了管理QSerialPort的对象A,创建了一个继承QThread
的对象,在此对象中重写run方法,在run方法中定时去向串口中写数据,将写的行为修改为信号,将命令使用信号发给 A即可。

  1. 使用子线程访问QSerialPort的写方法,结束线程时还会输出和,修改完后也好了
    QObject::killTimer: Timers cannot be stopped from another thread
    QObject::~QObject: Timers cannot be stopped from another thread
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值