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. QT QUICK request API REST
Forum Updated to NodeBB v4.3 + New Features

QT QUICK request API REST

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 4 Posters 454 Views
  • 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.
  • B Offline
    B Offline
    benjamin12
    wrote on last edited by benjamin12
    #1

    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

    1 Reply Last reply
    0
    • CTORC Offline
      CTORC Offline
      CTOR
      wrote on last edited by
      #2

      What is your problem? Can you explain?

      Is your call run with successful? or any error?

      1 Reply Last reply
      0
      • D Offline
        D Offline
        daljit97
        wrote on last edited by daljit97
        #3

        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.

        1 Reply Last reply
        1
        • 6thC6 Offline
          6thC6 Offline
          6thC
          wrote on last edited by
          #4

          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.html

          https://doc.qt.io/qt-5/qudpsocket.html
          https://doc.qt.io/qt-5/qtcpsocket.html

          https://doc.qt.io/qt-5/qsslsocket.html
          https://doc.qt.io/qt-5/qsctpsocket.html
          etc

          Again, 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.

          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