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. NetworkAccessManager like interface in QMl
Forum Updated to NodeBB v4.3 + New Features

NetworkAccessManager like interface in QMl

Scheduled Pinned Locked Moved QML and Qt Quick
9 Posts 4 Posters 2.3k 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.
  • Z Offline
    Z Offline
    zerocom
    wrote on last edited by
    #1

    Hi,

    I want to use a C++ class from QML, which has a functionality like the QNetworkAccessManager, I call get() function which then returns a reply object with a finished signal. If signal was triggered, I will process the result.

    Ideally it would look like this:
    @accessManager.get("http://myserver/data").onFinished: console.log("request done")@

    Right now I can access the object returned by get(), but I cannot connect to the signal.

    Any hints are welcome...

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

      Hmmm have you implement this in your reply object? Take a look at "this":http://qt-project.org/doc/qt-4.8/declarative-tutorials-extending-chapter2-methods.html

      **Sorry for my english :)

      PLEASE ADD [SOLVED] TO YOUR THREAD TITLE IF IT'S SOLVED.**

      1 Reply Last reply
      0
      • p3c0P Offline
        p3c0P Offline
        p3c0
        Moderators
        wrote on last edited by
        #3

        Hi,

        You could use "Connections":http://qt-project.org/doc/qt-5/qml-qtqml-connections.html to handle this. Just set the proper target.

        157

        1 Reply Last reply
        0
        • Z Offline
          Z Offline
          zerocom
          wrote on last edited by
          #4

          Connections wont work because they are static. I'll make example more concrete:

          @Button {
          text: "Get Data"
          onClicked: {
          replyConnection.target = accessManager.get("http://myserver/data")
          }
          }

          Connections {
          id: replyConnection
          onFinished: console.log("reply received")
          }@

          Assigning the connection target directly is not an option, because I want to make the request on Button click.

          Maybe WorkerScript is an option, but it doesn't seem to be very elegant.

          1 Reply Last reply
          0
          • p3c0P Offline
            p3c0P Offline
            p3c0
            Moderators
            wrote on last edited by
            #5

            I think the following should work:
            @
            Connections {
            target: accessManager
            onFinished: console.log("reply received")
            }
            @

            and
            @
            onClicked: {
            accessManager.get("http://myserver/data")
            }
            @

            May be i'm not understanding you clearly.

            Edited

            157

            1 Reply Last reply
            0
            • Z Offline
              Z Offline
              zerocom
              wrote on last edited by
              #6

              The prototype for the get function looks like this:
              @Reply* AccessManager::get(const QUrl &url)@

              In C++ I would do it like that:
              @Reply *r = accessManager.get("http://myserver/data")
              connect(r, SIGNAL(finished(), this, SLOT(requestDone()));@

              Which means not the access manager does have the signal, the Reply object has it. Just like QNetworkAccessManager which returns a QNetworkReply object.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi,

                Just a question but, would be xmlhttprequest suited for your needs ? You have an example "here":http://qt-project.org/doc/qt-5/qtqml-xmlhttprequest-example.html

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • p3c0P Offline
                  p3c0P Offline
                  p3c0
                  Moderators
                  wrote on last edited by
                  #8

                  @
                  Reply *r = accessManager.get("http://myserver/data")
                  connect(r, SIGNAL(finished(), this, SLOT(requestDone()));
                  @

                  So as per this the Reply object emits the finished signal and calls the
                  accessManager's slot requestDone(). You could also create a custom signal finished() for accessManager class which can be then emitted in requestDone() or you can also connect signal to a signal.

                  157

                  1 Reply Last reply
                  0
                  • Z Offline
                    Z Offline
                    zerocom
                    wrote on last edited by
                    #9

                    [quote author="SGaist" date="1414014463"]Hi,

                    Just a question but, would be xmlhttprequest suited for your needs ? You have an example "here":http://qt-project.org/doc/qt-5/qtqml-xmlhttprequest-example.html[/quote]

                    That would exactly be the interface I need, but I want to make my own class. The http request was just an example, the goal is to do different request/reply functionality. For example remote procedure calls. I looked into the code of xmlhttprequest, but it's heavy javascript based. My goal is to have class useable from C++ and QML.

                    Using the finished() signal of the accessManager is also an option, but this would require a big selector for the response function, if I want a special response function for every request.

                    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