我正在使用Qt开发黑莓10。我正在使用QFuture,QtConcurent :: run。 所以这是我的代码:
applicationui.cpp
LoadData* Data = new LoadData(url);
// Invoke our onLoading Finished slot after the loding has finished.
bool ok = connect(&m_watcher, SIGNAL(finished()),SLOT(onLoadingFinished()));
Q_ASSERT(ok);
Q_UNUSED(ok);
// starts watching the given future
m_watcher.setFuture(future);
并在 LoadData.cpp
中int LoadData::startLoading()
{
QNetworkAccessManager* netManager = new QNetworkAccessManager(this);
const QUrl url(_URL);
QNetworkRequest request(url);
QNetworkReply* reply = netManager->get(request);
bool ok = connect(reply, SIGNAL(finished()), this, SLOT(onReplyFinished()));
Q_ASSERT(ok);
Q_UNUSED(ok);
return 0;
}
但是当我运行应用程序时,这些是控制台中的错误:
QObject:无法为位于其他线程中的父级创建子级。 (Parent是LoadData(0x82ea9b0),父线程是QThread(0x8082440),当前线程是QThread(0x82eac68)
为什么会这样?如何解决这个问题?
答案 0 :(得分:1)
首先将新创建的对象移动到正确的线程,然后重新显示
QNetworkAccessManager* netManager = new QNetworkAccessManager();
netManager->moveToThread(this->thread());
netManager->setParent(this);