QObject: Cannot create children for a parent that is in a different thread.
bool SerialPort::OpenPort(const QString com, signed int bautrate)
{
serial_mutex->lock();
if (port->isOpen() || (com.isEmpty())) {
serial_mutex->unlock(); return true;
}
//初始化串口参数
port->setPortName(com);
port->setBaudRate(bautrate);
port->setParity(QSerialPort::NoParity);
port->setDataBits(QSerialPort::Data8);
port->setStopBits(QSerialPort::OneStop);
port->setFlowControl(QSerialPort::NoFlowControl);
port->setReadBufferSize(ReadBufferSize);
bool r_v = port->open(QSerialPort::ReadWrite);
//port->close();
serial_mutex->unlock(); return r_v;
}
这个函数是在线程中调用的,只要添加了“port->close();”这行代码,就会出现标题所示的错误,实在费解,打开串口没问题,关闭有。
(Parent is QSerialPort(0x2ded2874fe0), parent's thread is QThread(0x2decf6437b0), current thread is QThread(0x2ded28fdb30)