完整报错:
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即可。
- 使用子线程访问QSerialPort的写方法,结束线程时还会输出和,修改完后也好了
QObject::killTimer: Timers cannot be stopped from another thread
QObject::~QObject: Timers cannot be stopped from another thread