我正在尝试从服务器加载一些数据并填写Qt列表。 我想在一个线程中运行 dowloanding。 所以有以下代码:
-
应用程序中的主要功能.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(); } }
-
加载数据.cpp文件:这是启动借出函数的代码:
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; }
最后在插槽内回复完成我解析数据并填写列表。但这里的问题是,QFuture 在下载之前完成,因此列表始终为空。我怎样才能在执行 onReplyFinish 后返回填写的列表?
你可能会使这比你需要的更复杂。以下是我从网络上获取数据的方法。就我而言,我正在下载一个大的 ZIP 文件,将其写出然后解压缩,但基本步骤是相同的(为了清楚起见,我省略了很多特定代码):
void HtmlDownloader::startDownload() {
// Creates the network request and sets the destination URL
QNetworkRequest request = QNetworkRequest();
request.setUrl(mUrl);
// Creates the network access manager and connects a custom slot to its
// finished signal. Checks the return value for errors.
QNetworkAccessManager *networkAccessManager =
new QNetworkAccessManager(this);
bool c = connect(networkAccessManager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(requestFinished(QNetworkReply*)));
Q_ASSERT(c);
// Indicate that the variable res isn't used in the rest of the app, to prevent
// a compiler warning
Q_UNUSED(c);
// Sends the request
QNetworkReply *reply = networkAccessManager->get(request);
c = connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(onDownloadProgress(qint64,qint64)));
Q_ASSERT(c);
}
void HtmlDownloader::requestFinished(QNetworkReply *reply) {
// Handle the reply data...
// Check the network reply for errors
if (reply->error() == QNetworkReply::NoError) {
// use reply->readAll() or reply->read() to get the returned data
// and process into a list. The list can't be returned but could
// be sent as a payload of a signal, or stored in a data member and
// a signal sent to indicate it is available.
}
reply->deleteLater();
}
void HtmlDownloader::onDownloadProgress(qint64 bytesRecieved, qint64 bytesTotal) {
emit downloadProgress(bytesRecieved, bytesTotal);
}