i'm trying to load some data from a server and fill a Qt list. i want to run the dowloanding in a thread. so there is the code of:
principal function in the App.cpp
loadInterestPlaces(QString& urlInterestPlaces) { LoadData* Data = new LoadData(urlInterestPlaces); QFuture< list <InterestPlace *> > future = QtConcurrent::run(Data, &LoadData::startLoading); // Invoke our onLoadingFinished slot after the loading has finished. bool ok = connect(&m_watcher, SIGNAL(finished()), this, SLOT(onLoadingFinished())); Q_ASSERT(ok); Q_UNUSED(ok); // starts watching the given future m_watcher.setFuture(future); } void ApplicationUI::onLoadingFinished() { qDebug() << "Loading finished"; interestPlacesList = m_watcher.future().result(); qDebug() << "List size= " << interestPlacesList.size(); } }
the LoadData.cpp file : this is the code of the startloanding function :
std::list<InterestPlace *> LoadData::startLoading() { QNetworkAccessManager* netManager = new QNetworkAccessManager(this); const QUrl url(_URL); QNetworkRequest request(url); QNetworkReply* reply = netManager->get(request); netManager->moveToThread(this->thread()); netManager->setParent(this); bool ok = connect(reply, SIGNAL(finished()), this, SLOT(onReplyFinished())); qDebug() << reply->isFinished(); Q_ASSERT(ok); Q_UNUSED(ok); qDebug() << "load data: liste size" <<interestPlacesList.size(); return interestPlacesList; }
Finally inside the SLOT onreplyfinished i parse the data and fill the list. But the problem here, the QFuture is finished before the downloading so that the list is always empty. How could i return the list filled just after the execution of the onReplyFinished ?