http request without signals and slots
hi all.
i'm a newbie in qt programmin, so please be patient.
i'm writing a qt library and i need to perform an http request to a remote server and, of course, read the response.
i don't need at all to use the signals and slots system, because i have to wait for the http call to finish before going on with the code.
is there a way to do that? i prefer to use the new network api, if possible
thanks
christian
Re: http request without signals and slots
Re: http request without signals and slots
thanks for the answer.
my "download" method is this (code found on the forum, i think it's posted by you):
Code:
QNetworkAccessManager* manager = new QNetworkAccessManager();
QNetworkReply *reply = manager->get(QNetworkRequest(url));
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit())); // just in case
timer.start(5000);
loop.exec();
if ( reply->error() != QNetworkReply::NoError ) {
qDebug() << "Request failed:" << reply->errorString();
return;
}
qDebug() << "Reply from the server: ";
qDebug() << result;
}
but when i run it i get:
Quote:
QEventLoop: Cannot be used without QApplication
QObject::startTimer: QTimer can only be used with threads started with QThread
and the program loops
i'm writing a shared library, so there is not a QApplication. is there another way, then?
Re: http request without signals and slots
No, you need a Q(Core)Application object. Someone has to create it - either you when initializing your library or the application that uses your library.
Re: http request without signals and slots
thanks, declaring a QCoreApplication did the trick.
last quetion, is there a way to esatablish if a QCoreApplication exists before creating a new one? in that case i could create a qcoreapplication only if needed
Re: http request without signals and slots
QCoreApplication is a singleton, you can query for its instance through QCoreApplication::instance().
Re: http request without signals and slots
well wysota, thanks again
you have a beer payed if you pass here :)