A external component has a callback which executes in its internal thread start with std::thread, I want to create a qt component(not UI) in this thread, connect other qt components signal to this qt component, and let the slot function execute in this internal thread. I expect to execute to run the event loop once callback triggerd to process pending slot functions invoking.
// call in a thread start with std::thread in ExternalComponent, this method invoke periodically.
void ExternalComponent::InternalProcessing() {
//do other thing...
//invoke callback
callback();
}
void CustomQtComponent::Init() {
externalComponent.SetCallback([]() {
// first time, create a Worker
if (worker_ == nullptr) {
worker_ = new Worker();
}
// process pending signals(invoke worker_ slot methods) in this thread
// ...
// do other things.
});
}
// call by ui thread
void CustomQtComponent::DoSomething() {
// do xxxx
// ...
// emit a signal, to let something process in callback threads
// emit cutstomSignalWhichConnectToWokerSlots();
}
Because external threads not start by QThread, so though we can get the QThread object in its thread(in callback), but it has no event loop. Could I construct a QEventLoop in callback thread, and let it receive and process signals sending to worker_?