QHttp in not available in Qt5
Asked Answered
A

3

5

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?

Antibaryon answered 3/10, 2014 at 13:54 Comment(5)
Yes you need to either use QNetworkAccessManager (preferred) or use the compatibility add-on QtHttp which provides the QHttp class as it was in Qt4.Aetna
Thanks but what I don't understand is "how to do it". Am just a learner.Antibaryon
It depends on what you want to do. You need to try to code something and then submit the code in order to get help here.Aetna
Ok but if I wanted to use the compatibility add-on QtHttp which provides the QHttp class as it was in Qt4. Is it like an included header file or is it added to the user.pro file.Antibaryon
Check this topic. It explains how to install the compatibility add-on.Goar
Z
4

This is a simple HTTP post (I am using Qt 5.3.2)

int Connection::postRequest(QString requestType, QUrl params){
    QString params_array = params.query();

    QNetworkRequest request(user->url);
    request.setHeader(QNetworkRequest::ContentLengthHeader, QByteArray::number(params_array.size()));
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");

    QEventLoop waitLoop;
    QNetworkAccessManager* connection = new QNetworkAccessManager(/*`this` can also be passed*/);
    QNetworkReply* reply = connection->post(request, params_array.toUtf8());
    QObject::connect(reply, SIGNAL(finished()), &waitLoop, SLOT(quit()));
    waitLoop.exec();

    int errorCode = reply->error();
    if (errorCode != 0){
        // Show Error Message
    }
    else{
        // Parse "reply"
    }

    delete reply;
    delete connection;
    return errorCode;
}
Zebadiah answered 3/10, 2014 at 16:3 Comment(1)
Could you post your waitloop as well?Schmuck
A
6

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

       ...
   }
}
Artis answered 4/10, 2014 at 5:1 Comment(0)
Z
4

This is a simple HTTP post (I am using Qt 5.3.2)

int Connection::postRequest(QString requestType, QUrl params){
    QString params_array = params.query();

    QNetworkRequest request(user->url);
    request.setHeader(QNetworkRequest::ContentLengthHeader, QByteArray::number(params_array.size()));
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");

    QEventLoop waitLoop;
    QNetworkAccessManager* connection = new QNetworkAccessManager(/*`this` can also be passed*/);
    QNetworkReply* reply = connection->post(request, params_array.toUtf8());
    QObject::connect(reply, SIGNAL(finished()), &waitLoop, SLOT(quit()));
    waitLoop.exec();

    int errorCode = reply->error();
    if (errorCode != 0){
        // Show Error Message
    }
    else{
        // Parse "reply"
    }

    delete reply;
    delete connection;
    return errorCode;
}
Zebadiah answered 3/10, 2014 at 16:3 Comment(1)
Could you post your waitloop as well?Schmuck
L
1

Assuming that you don't need to maintain a huge code base, the right way is to use new QNetworkAccessManager class instead.

Lashonlashond answered 3/10, 2014 at 16:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.