0

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)

frei blau
  • 13
  • 3
  • Could it just be a problem of mutual exclusion ? – Claudio Dec 09 '15 at 11:59
  • You should use signals and slots to communicate between threads. Also note that you can't create GUI elements from another thread. – Pavel Parshin Dec 09 '15 at 12:02
  • You are not allowed to pass a `QObject` to another thread and then create children of that object in the other thread. This is the reason for your last message. What are you trying to accomplish by passing `QObject`s instead of some other data? – RobbieE Dec 09 '15 at 12:13
  • Well, that 'data' is a QObject in the program. I wonder what causes creation of those children. I never state a parent(I don't even explicitly create an object in that thread) – frei blau Dec 09 '15 at 12:18
  • Also make sure you do not pass the QObjects by value. Those are not meant to be copied. – Aaron Dec 09 '15 at 12:35
  • Just give us a sample code that contains your problem. – cassandrad Dec 09 '15 at 12:37
  • 1
    Possible duplicate of [QObject: Cannot create children for a parent that is in a different thread](http://stackoverflow.com/questions/3268073/qobject-cannot-create-children-for-a-parent-that-is-in-a-different-thread) – cassandrad Dec 09 '15 at 12:45
  • I added code. That answer you linked was specific to that question I think. – frei blau Dec 09 '15 at 12:59
  • Still, you have not showed us how do you pass the object to thread. Could you please give us a working short example that we could run on our machines and get the same error? – cassandrad Dec 09 '15 at 13:07
  • @freiblau Welcome to SO. With respect, this is a very poor question, which can only lead to guesses as to the problem. The code examples provided provide no context and are likely nothing to do with the error produced. To help you receive a good answer, I suggest you create an [MCVE](http://stackoverflow.com/help/mcve) and edit your question appropriately. – TheDarkKnight Dec 09 '15 at 13:29

1 Answers1

0

There is a good chance, that in obj->serial.waitForReadyRead(0); a QObject is created (e.g. a QTimer) which gets QSerialPort as parent. That could cause the problem. So the solution could be to find a way to create the QSerialPort object in your thread.

Aaron
  • 1,181
  • 7
  • 15