Cannot create children for a parent that is in a different thread
-
I've creating an object (which inherits from QObject) in my main and passing this object to another thread i.e.
main.cpp
QGuiApplication app(argc, argv); ClientListWrapper clients; if (!clients.initialize()) return -1; args2thread->Pointer2clients = &clients; // Passing this object to thread std::thread Listening_thread(thread_function, args2thread); // Creating a thread
Now in my thread function I'm modifying client object i.e.
myThread.cpp
//Receive data from client here . args2thread->clients->addClient(data); // Store the received data .
Why QT complains about it ? And how do i solve this
-
Hi
Well you create ClientListWrapper clients; in the Main thread and simply
assigning its adress to a variable won't move it.Have you considered using QThread and
https://doc.qt.io/qt-5/qobject.html#moveToThread -
I'm actually passing the address of clients variable to my thread. In that thread, I'm putting some data using that address. This approach works well in C++ but QT has some problem with this approach. It maybe because ClientListWrapper inherets from QObject (?)
-
-
You create a QObject derived class (and pass a
parent
) in the secondary thread which is not allowed. All objects of an object tree must live in the same thread. So you have two options - either move the whole object tree to the other thread or don't put the newly created QObject in the object tree from the main thread.