I'm rather new in QT but used multi-threading methods in some other platforms. I'm doing polling in a QThread that I create in main. To do that I subclassed it. I need to pass some parameters (QObjects) to the thread for it to do some work. But when I try to use them my program crashes. So my question is how I can use the same QObjects in both threads? I'll use mutex for synchronization but I can't get rid of this error "Cannot create children for a parent that is in a different thread."
There are four static functions which do work on a QSerialPort object.
QSerialPort serial; // this is in MyObject's class definition (instance variable)
void MyQObject::func1(void *objData)
{
MyQObject *obj = static_cast<MyQObject*>(objData);
obj->serial.clear(QSerialPort::Input);
}
int MyQObject::func2(void *objData)
{
MyQObject *obj = static_cast<MyQObject*>(objData);
obj->serial.waitForReadyRead(0);
return obj->serial.bytesAvailable();
}
void MyQObject::func3(ivoid *objData)
{
MyQObject *obj = static_cast<MyQObject*>(objData);
// Read data using serial.read()
}
void MyQObject::func4(void *objData)
{
MyQObject *obj = static_cast<MyQObject*>(objData);
// Read data using serial.write()
}
And in MyThread's run method I call above functions.. This causes the crash.
Error:
QObject: Cannot create children for a parent that is in a different thread. (Parent is QSerialPort, parent's thread is QThread, current thread is OtherThread)