NetworkAccessManager like interface in QMl
-
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...
-
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
-
Hi,
You could use "Connections":http://qt-project.org/doc/qt-5/qml-qtqml-connections.html to handle this. Just set the proper target.
-
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.
-
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
-
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.
-
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
-
@
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. -
[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.