В примере, предоставленном Qt doc, подкласс ответа изображения , во время построения создает задачу QRunnable (которая также является QObject), подключает пользовательский сигнал Done задачи к ее пользовательскому слоту handleDone (который будет выдавать Finished< /code>) и, наконец, передает задачу в QThreadPool для выполнения.
Мой вопрос: возможно ли, что ответ очень быстро завершит свою работу быстро, и сигнал завершения генерируется до того, как метод requestImageResponse вернет указатель? Как гарантируется, что движок сможет перехватить и обработать все готовые сигналы?
......Если подумать, возможно, это механизм цикла событий qt, который гарантирует это:
Код: Выделить всё
{
// this may be how the engine uses the provider
auto response = provider->requestImageResonse(...);
// suppose now the runnable has done the job and emits "done"
// but the signal is just queued up to the current event-loop
// and the "handleDone" slot would not get executed
// until the event-loop finishes handling this piece of code
QObject::connect(r, &QQuickImageResponse::finished, ...);
// now the response is well-connected to the engine
}
// finally the event-loop can execute the "handleDone" slot of the response
{
......
// in the image response subclass
// void handleDone(QImage image)
{
this->image = image;
emit finished();
// the signal is guaranted to be caught by the engine
}
}
Подробнее здесь: https://stackoverflow.com/questions/782 ... -too-early