qt - Return response of GET request from c++ to QML string -


i'm trying simple request (with modified user-agent), return response qml , json parsing.

actually returns page content when loading complete doesn't return qml.

sorry noob question. i'm new language , i'm trying learn :)

here's code:

home.qml

function getrequest() {  [...] console.log('request...') var jsonresult = json.parse(connectnet.connecturl("http://myurl.com/index.php").tostring())  lboutput.text = jsonresult.predictions[0].description.tostring()     } } 

connectnet.cpp

#include "connectnet.h" #include "stdio.h" #include <qdebug> #include <qnetworkrequest> #include <qnetworkreply> #include <qnetworkaccessmanager> #include <qurl>   connectnet::connectnet(qobject *parent) : qobject(parent) { }  void connectnet::connecturl(qstring url) {         qnetworkaccessmanager *manager = new qnetworkaccessmanager();     qnetworkrequest request;     qnetworkreply *reply = null;      request.seturl(qurl(url));     request.setrawheader( "user-agent" , "fake user agent here" );      reply = manager->get(request);     connect(manager, signal(finished(qnetworkreply*)), this,                      slot(replyfinished(qnetworkreply*))); }      qstring connectnet::replyfinished(qnetworkreply *reply) {    return reply->readall(); } 

appname.cpp

#ifdef qt_qml_debug #include <qtquick>  #endif  #include <sailfishapp.h> #include "connectnet.h"   int main(int argc, char *argv[]) {     //init settings      qguiapplication *app = sailfishapp::application(argc, argv);     qquickview *view = sailfishapp::createview();     connectnet connectnet;     view->rootcontext()->setcontextproperty("connectnet", &connectnet);     view->setsource(sailfishapp::pathto("qml/appname.qml"));     view->showfullscreen();     app->exec(); } 

hope i've explained i'm looking for. help.

====================================================

edit 20/08/2015: added updated connectnet.h

    #ifndef connectnet_h     #define connectnet_h      #include <qobject>     #include <qnetworkreply>     #include <qdebug>     #include <qnetworkrequest>     #include <qnetworkreply>     #include <qnetworkaccessmanager>     #include <qurl>  class connectnet : public qobject {     q_object     qnetworkaccessmanager m_manager; public:   connectnet(qobject * parent = 0) : qobject(parent) {       connect(&m_manager, &qnetworkaccessmanager::finished,       [this](qnetworkreply * reply) {         if (reply->error() == qnetworkreply::noerror)           emit replyavailable(qstring::fromutf8(reply->readall()));       });     } signals:     void replyavailable(const qstring & reply); public slots:     void sendrequest(const qstring url) {         qnetworkrequest request;         request.seturl(qurl(url));         request.setrawheader("user-agent", "mylittleagent");         m_manager.get(request);     } };      #endif // connectnet_h 

this part of code gives lot of errors :( (screenshot below)

      connect(&m_manager, &qnetworkaccessmanager::finished,       [this](qnetworkreply * reply) {         if (reply->error() == qnetworkreply::noerror)           emit replyavailable(qstring::fromutf8(reply->readall()));       }); 

compiling erros: http://i.stack.imgur.com/30vwn.jpg

your problem think synchronously. connecturl cannot return value (and doesn't), since when runs result not available. must do, instead, connectnet class emit signal when data available.

it'd horrible idea if tried make synchronous wrapper did return value: qml engine stuck long took result received. freeze application pulling network cable @ right moment, or if server down. users hate that, , it's horrible antipattern must expediently eliminated , discouraged.

here's how connectnet (please, not connectnet, lowercase names members!) class look. note qnetworkaccessmanager instance doesn't need pointer.

class connectnet : public qobject {   q_object   qnetworkaccessmanager m_manager; public:   connectnet(qobject * parent = 0) : qobject(parent) {     connect(&m_manager, &qnetworkaccessmanager::finished,      [this](qnetworkreply * reply) {       if (reply->error() == qnetworkreply::noerror)          emit replyavailable(qstring::fromutf8(reply->readall()));     });   }   q_slot void sendrequest(const qstring & url) {     auto request = qnetworkrequest(qurl(url));     request.setrawheader("user-agent", "mylittleagent");     m_manager.get(request);   }   q_signal void replyavailable(const qstring & reply); }; 

since connectnet instance instance exposed property in global qml context, can connect signals follows:

function getrequest() {   connectnet.sendrequest("http://myurl.com/index.php") }  function resulthandler(result) {   var jsonresult = json.parse(result.tostring())   lboutput.text = jsonresult.predictions[0].description.tostring() }  rectangle { // or other item   component.oncompleted: {     connectnet.replyavailable.connect(resulthandler)   }   ... } 

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -