Reputation: 28
I want to design a single base class for both controlling the thread and executing slots of the class in thread itself via qobject::connect or invokemethod.
When start is called, I call this->movetothread(memthread) and memthread->start to move this into member thread's context and start the eventloop. when stop is called, qthread's quit is called to stop the event loop. Problem is that, when thread quits, it is impossible to deallocate "this" via deletelater later on since deletelater needs a running eventloop to delete the object. Object's thread context could already be stopped via call to quit before.
I can't connect object->deletelater to thread::finished since object would be unusuable then and I can't start/stop thread again. My aim in the design is to accomplish this actually. Being able to stop the thread, start later, stop again and so on.
I'm not sure if the design is doable with the way qt is but want to at least try.
P.S. My first question, please kindly let me know about any mistakes.
Upvotes: 0
Views: 158
Reputation: 9986
I am not sure I understand the question completely, and also there are very few details. However, why stopping the thread in the first place? Anyway, depending on the specific context, you could start the thread when you want to delete your object and then delete it, then stop the thread and delete the QThread. Otherwise you could simply delete your object. Anther option is to move your object to the main thread when stopping your thread:
QMetaObject::invokeMethod(this, [this] {
moveToThread(qApp->thread());
});
and then simply deleteLater() when you feel you are ready. These are two options, but I think there are others, it depends on your context.
Upvotes: 1