Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to receive array from js?use QWebChannel



  • js:

    new QWebChannel(qt.webChannelTransport, function(channel) {
            // make dialog object accessible globally
            var js_object = channel.objects.jsObject;
            
            $("#save_data").click(function(){
                    var aa = new Array('name', 'c');
                    alert(JSON.stringify(aa));
                    js_object.receiveText(JSON.stringify(aa));
                }
                
            });
        });
    

    qt

    void PickerJsObject::receiveText(const QString &r_text)
    {
    
    
         qDebug()<<r_text;
    
    
    
         QJsonParseError error;
         QJsonDocument message = QJsonDocument::fromJson(messageData.toUtf8(), &error);
         if (error.error) {
             qWarning() << "Failed to parse text message as JSON object:" << messageData
                        << "Error is:" << error.errorString();
             return;
         } else if (!message.isObject()) {
             qWarning() << "Received JSON message that is not an object: " << messageData;
             return;
         }
    
     
    
    }
    

    console output:

    "[\"name\",\"c\"]"
    Received JSON message that is not an object:  "[\"name\",\"c\"]"
    

    JS how to return an array of data to QT? JS returns what format? QT and how to receive and parse, please enlighten.


Log in to reply