QT QUICK request API REST
-
Hello,
Sorry for our English we are French
We are two computer science students. Our project is to create an android mobile application. We have created a rest API and now we want to make the link between this API and our mobile application. But we don't know at all how QT queries work. We tried several tutorials but it doesn't work (we don't know if we adapt our requests well).
import QtQuick 2.14 import QtQuick.Window 2.14 import QtQuick.Controls 2.12 import QtQuick.Dialogs 1.0 ApplicationWindow { id: window visible: true width: 640 height: 480 title: qsTr("Stack") Rectangle { } FileDialog { id: fileDialog title: "Image" folder: shortcuts.home onAccepted: { console.log("You chose: " + fileDialog.fileUrls) } onRejected: { console.log("Canceled") } } FileDialog { id: fileDialog1 title: "Audio" folder: shortcuts.home onAccepted: { console.log("You chose: " + fileDialog.fileUrls) } onRejected: { console.log("Canceled") } } Button { id: button x: 234 y: 240 text: qsTr("Valider") onClicked: { var Image_legume = button.button1 var Audio_legume = button.button2 var Nom_legume = TextField.test var http = new XMLHttpRequest() var url = "http://localhost"; var params = "legumes.php?action=add_legumes_recettes"; http.open("POST", url, true); // Send the proper header information along with the request http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); http.setRequestHeader("Content-length", params.length); http.setRequestHeader("Connection", "close"); http.onreadystatechange = function() { // Call a function when the state changes. if (http.readyState == 4) { if (http.status == 200) { console.log("ok") } else { console.log("error: " + http.status) } } } http.send(params); } } Button { id: button1 x: 234 y: 77 text: qsTr("Ajouter une image") onClicked: { fileDialog.open() } } Button { id: button2 x: 234 y: 136 text: qsTr("Ajouter un audio") onClicked: { fileDialog1.open() } } TextField { id:test x: 187 y: 31 text: qsTr("Nom") } }
Sorry to sound so lame, we're just starting out in programming.
Lala and Ben
-
In my opinion anything to do with networking should be done in C++. Something like this it's very straightforward:
#ifndef NETWORKEXAMPLE_H #define NETWORKEXAMPLE_H #include <QObject> #include <QtNetwork> class NetworkExample : public QObject { public: NetworkExample(){ m_netManager = new QNetworkAccessManager(this); } void makeRequest() { QNetworkRequest request(QUrl("http::/google.com")); auto reply = m_netManager->get(request); connect(reply, &QNetworkReply::finished, this , &NetworkExample::requestFinished); } void requestFinished() { auto reply = qobject_cast<QNetworkReply*>(sender()); if(reply->error() == QNetworkReply::NoError){ auto response = reply->readAll(); // handle respone } else{ qDebug() << "Error" << reply->errorString(); } } private: QNetworkAccessManager* m_netManager; }; #endif // NETWORKEXAMPLE_H
Then you can call this from QML.
-
As @daljit97 - I came here to say this too - do processing out of the GUI.
Just for your own exposure - there is plenty of other kinds of networking examples too like: https://doc.qt.io/qt-5/qtnetwork-torrent-example.html
More @ https://doc.qt.io/qt-5/qtnetwork-index.htmlhttps://doc.qt.io/qt-5/qudpsocket.html
https://doc.qt.io/qt-5/qtcpsocket.htmlhttps://doc.qt.io/qt-5/qsslsocket.html
https://doc.qt.io/qt-5/qsctpsocket.html
etcAgain, I'd highly encourage to disconnect anything not GUI into c++ and pass signals of only the data you care for on completion / event signals. It just keeps gui maintainable and cleanly separated from functionality.