Im мой mainthread, у меня есть окно Qt, которое вызывает мой фоновый поток (услуга сети) и в конечном итоге ожидает ответов, которые должны быть отражены в UI:Вызов lambda/std :: function в основном потоке пользовательского интерфейса из фоновой темы
// runs in main (GUI) thread
void QTServer::onButtonClick(){
srv->request(msg,
[this]
(std::shared_ptr<message> msg){
this->ui.txtResponse->setText("received a response" + msg.data());
});
}
сеть обслуживания выглядит следующим образом:
std::function<void(std::shared_ptr<message>)> delegate_;
void NetworkService::request(message& msg,std::function<void(std::shared_ptr<message> msg)> fn)
{
// send the request to the socket and store the callback for later
// ...
this->delegate_ = fn;
}
void NetworkService::onResponseReceived(std::shared_ptr<message> responseMsg)
{
// is called from the background thread (service)
// Here I would like to call the lambda function that the service stored somewhere (currently as an std::func member)
// psuedo: call In Main Thread: delegate_(responseMsg);
}
Как это работает? Это даже работает?
Я знаю, что вы можете использовать QMetaObject::invokeMethod(this, "method", Qt::QueuedConnection
для вызова функции в mainthread, поэтому я попытался следующее:
void NetworkService::onResponseReceived(std::shared_ptr<message> responseMsg)
{
QMetaObject::invokeMethod(this, "runInMainThread", Qt::QueuedConnection, QGenericArgument(), Q_ARG(std::function<void(std::shared_ptr<message>)>, delegate_));
}
Как пройти responseMsg здесь в качестве аргумента для _delegate?
void QTServer::runInMainThread(std::function<void(std::shared_ptr<message>)> f) {
f();
}
Как избавиться от ошибки «Нет функции с этими аргументами»?
хмм, он говорит "нет конструктора доступны", если я пытаюсь 'Q_ART (пустота *, delegate_)' – netik
передачи адрес Q_ARG (пустота *, & delegate_), а затем использовать reinterpret_cast <аннулируется (станд :: shared_ptr) *> –
jonezq