0

我正在使用 Qt 进行黑莓 10 开发。我正在使用 QFuture,QtConcurent::run。所以这是我的代码:

应用程序ui.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),parent的线程是QThread(0x8082440),当前线程是QThread(0x82eac68)

为什么会这样?如何解决这个问题?

4

1 回答 1

1

您首先将新创建的对象移动到正确的线程,然后重新父

QNetworkAccessManager* netManager = new QNetworkAccessManager();
netManager->moveToThread(this->thread());
netManager->setParent(this);
于 2013-11-14T15:40:46.720 回答