General xmlhttprequest element

  • Hi,
    I am new to the subject of HTTP, so my questions might seem trivial for some of you.

    I need an element which handles all the http requests in my app.
    It shold be able to handle GET and POST requests
    The requests come from various elements, which require different requests and replies.
    Some elements send several requests in a row.

    1. How can I assure, that all requests from one element are returned to the same element (is there something like QNetworkRequest's originatingObject?) and is there something like an ID that I can attach to the requests?

    2. How can I upload binary-data (with HTTP-POST?)

    3. How can I make sure that the responses of old requests (that are not needed anymore) are ignored.


  • You just have to create a C++ QObject class (myHttp in the example) and using QNetworkAccessManager to make your Http request !get() and post() are avaible from there.
    Then, you need to add your class in the QML context as a global object accessible from everywhere :

    @class MyHttp : public QObject {
    Q_INVOKABLE void getMyRequest();

    QNetworkAccessManager * mNetManager:


    void MyHttp::getMyRequest(){

    Then from main.cpp :

    MyHttp myAttp;
    view.rootContext()->setContextProperty("myHttp", &myHttp);

    Then, from QML, you should be able to call invokable method from everywhere :

    onClicked: myHttp.getMyRequest()

    Upload binary : "take a look here": :

  • Thanks for the answer.
    How can I assigin the replies to the sender of the request from qml and how to merge several replies (text-snippets) to the same origin?

  • @
    QNetworkReply * reply = mNetManager->get(QNetworkRequest(QUrl("http://......"));

    void MyHttp::parseResult()
    QNetworkReply * reply = qobject_cast<QNetworkReply*>(sender());

    QString dataReceive = reply->readAll();



  • Thank you very much.
    Things are getting clearer for me now. :)

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.