I'm trying to use QtConcurrent
for not having to create a new slot for a simple QNetworkAccessManager::get
request:
QNetworkAccessManager *am = new QNetworkAccessManager();
QFuture<QNetworkReply*> future = QtConcurrent::run(am, &QNetworkAccessManager::get, QNetworkRequest(QUrl("https://api.ipify.org/?format=json")));
future.waitForFinished();
but what i get is a warning from Qt:
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QNetworkAccessManager(0x7fc973f3d100), parent's thread is QThread(0x7fc973c14ec0), current thread is QThread(0x7fc973dc2090)
If code like:
QObject *obj = new QObject();
QFuture<void> future = QtConcurrent::run(obj, &QObject::setParent, new QObject);
future.waitForFinished();
works fine and with no warnings and errors, can anyone explain to me what's the difference between the two approaches and what happens behind the scenes of the QNetworkAccessManager
approach?
Is there a solution for the QNetworkAccessManager
approach?