Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. General xmlhttprequest element
Forum Updated to NodeBB v4.3 + New Features

General xmlhttprequest element

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 2 Posters 2.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    samjam
    wrote on last edited by
    #1

    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.

    Thanks

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dridk
      wrote on last edited by
      #2

      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 {
      public:
      Q_INVOKABLE void getMyRequest();

      private:
      QNetworkAccessManager * mNetManager:

      }

      void MyHttp::getMyRequest(){
      mNetManager->get(QNetworkRequest(QUrl("http://......"));
      }
      @

      Then from main.cpp :

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

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

      @
      MousArea{
      onClicked: myHttp.getMyRequest()
      }@

      Upload binary : "take a look here":http://qt-project.org/doc/qt-4.8/qnetworkaccessmanager.html#post :

      Nothing in Biology Makes Sense Except in the Light of Evolution

      1 Reply Last reply
      0
      • S Offline
        S Offline
        samjam
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dridk
          wrote on last edited by
          #4

          @
          QNetworkReply * reply = mNetManager->get(QNetworkRequest(QUrl("http://......"));
          connect(reply,SIGNAL(finished()),this,SLOT(parseResult()));

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

          QString dataReceive = reply->readAll();

          }

          @

          Nothing in Biology Makes Sense Except in the Light of Evolution

          1 Reply Last reply
          0
          • S Offline
            S Offline
            samjam
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved