Please nominate your Qt Champions for 2021! https://forum.qt.io/topic/132134/looking-for-the-2021-qt-champions
Error QObject::setParent: Cannot set parent, new parent is in a different thread
developerNancy last edited by
error while uploading file on server in qt application crashed and got error
QObject::setParent: Cannot set parent, new parent is in a different thread
thesourcehim last edited by
It means exactly what is says. The entire object hierarchy has to be in the same thread. Object's thread is defined when the object is created inside that thread. You can't create an object with parent that was created from different thread and you can't reparent object if the parent is in different thread. In this case you should move one of these objects (parent or child) to the same thread with QObject::moveToThread or use new object without parent at all. Note a few things if you decide to move object:
- If object has parent, you can't move it.
- If object has children, moving it to the different thread will move the entire hierarchy too.
- You can push object to the different thread, but you can't pull it, i.e. you must call moveToThread from the current object's thread.
- When using signal-slot queued connections, note that object's slots are executed within the event loop of current object's thread, so if you move the object, it's slots will be executed in a new thread (assuming you have event loop there).