如何从线程的结果返回Qt对象(Qtfutur)
how to return an Qt object from the result of an thread (Qtfutur)
我正在尝试从服务器加载一些数据并填写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);
}
相关文章:
- 如何在QT Creator上将QWidget声明为继承类的对象
- 如何在qt中将信号和插槽与另一个对象连接 --解决了
- Qt:从另一个窗口访问公共对象
- 通过插槽和信号在不同线程中的两个qt对象之间进行通信
- 如何在qt中将对象添加到现有布局中?--已解决
- 如何注册Qt C++对象以在QML中使用它
- 如何在Qt中连接来自不同窗口的两个对象?
- 将对象传递给多线程对象 Qt
- Qt:如何通知对象已建立涉及它的信号槽连接
- Qt访问每个类中的查看器对象?
- Qt调试:我如何知道QCoreApplication::notifyInternal2正在向哪个对象发送消息?
- Qt c++不会为所有对象调用move_slot.为什么?
- Qt对象所有权内存泄漏
- CMake + Qt,moc 编译失败,无法实现 QMetaObject 方法(编译器找不到基本 ui 对象的标头?
- 在Qt中执行完所有插槽后,如何释放对象
- 两个对象QT之间的示例插槽/信号
- 使用派生的类对象 Qt 访问基类信号
- 堆栈对象Qt信号和参数作为参考
- 重用QProcess对象(Qt 4.8)
- QWebEngine & QWebChannel:传输对象"qt.webChannelTransport"在页面重新加载后消失