I noticed that the QHttp
class is no longer available in Qt5 and I keep getting an error message which says that I need to use the QNetworkAccessManager
to do this.
Is there a way to access this class in Qt5?
Use QNetworkAccessManager
in Qt 5. You can use an event loop to wait until the reply is finished and then read the available bytes :
QString My_class::My_Method()
{
QNetworkAccessManager manager;
QNetworkReply *reply = manager.get(QNetworkRequest(QUrl(myURL)));
QEventLoop loop;
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), &loop, SLOT(quit()));
loop.exec();
QByteArray bts = reply->readAll();
QString str(bts);
delete reply;
return str;
}
You can also do it in an asynchronous way by connecting the finished
signal of the QNetworkAccessManager
to a slot :
connect(&manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(onFinished(QNetworkReply*)));
And read data there :
void onFinished(QNetworkReply* reply)
{
if (reply->error() == QNetworkReply::NoError)
{
QByteArray bts = reply->readAll();
...
}
}