oumaimadev
oumaimadev

Reputation: 69

QFuture Cannot create children for a parent that is in a different thread

i'm using Qt for blackberry 10 developing. i'm using the QFuture, QtConcurent::run. So this is my code:

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);

and in the 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;
}

but when i run the application these is the error in the console:

QObject: Cannot create children for a parent that is in a different thread. (Parent is LoadData(0x82ea9b0), parent's thread is QThread(0x8082440), current thread is QThread(0x82eac68)

Why does this happen? How to solve this?

Upvotes: 0

Views: 1011

Answers (1)

ratchet freak
ratchet freak

Reputation: 48196

you first move the newly created object to the correct thread and then reparent

QNetworkAccessManager* netManager = new QNetworkAccessManager();
netManager->moveToThread(this->thread());
netManager->setParent(this);

Upvotes: 1

Related Questions