×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    8

    Unhappy Problem with networkManager.get(QNetworkRequest(url));

    Hi All,

    I am trying to retrieve some information from Qt Qucik(UI) and use those userName and password in Qt and want to print whether it is success full Login or Failure. The problem is when I use the following syntax for connect (connect(&networkManager, SIGNAL(finished(QNetworkReply*)),this, SLOT(handleNetworkData(QNetworkReply*))) and hard Code userName and password in the URL in the constructor it is working fine.

    But I am trying to call the handleNetworkData(networkManager.get(QNetworkRequest(url))); directly with out using connect . It is not working. Any help on this would be appreciated.

    Here is My code for retrieving information: Calling retrieveXml() in QT QUIK with object of QT. Like test.retriveXml())

    Code:
    void test::retrieveXml()
     {
    
        if (userName.isEmpty() || password.isEmpty())
        {
            output = "Empty Fields";
            return;
        }
        QUrl url(" My Own Url");   
      handleNetworkData(networkManager.get(QNetworkRequest(url)));
    }
    
    void test::handleNetworkData(QNetworkReply *networkReply)
    {
        if (!networkReply->error())
            parseXml(networkReply->readAll());
        else
            output = "Network Error";
        networkReply->deleteLater();
    }
    
    void test::parseXml(const QString &data)
     {
     QXmlStreamReader xml(data);
     coordinates.clear();
     xml.readNext();
     int i=0;
     while (!xml.atEnd()) {
         i++;
         xml.readNext();
         coordinates = xml.tokenString();
         coordinates = xml.tokenString();
         if (xml.tokenType() == QXmlStreamReader::StartElement)
         {
             if (xml.name() == "id")
             {
                 coordinates = xml.readElementText();
                  output = "Login Success";
              }
         }
     }
     if(xml.error())
     {
         output = "Login Failed";
     }
     }
    Last edited by k.rajanireddy; 2010-10-18 at 19:49.

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    4

    Re: Problem with networkManager.get(QNetworkRequest(url));

    Did you open the connection before requesting your XML file ?
    If not take a look at this thread it might help you
    http://discussion.forum.nokia.com/fo...se-Qt-Mobility

  3. #3
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: Problem with networkManager.get(QNetworkRequest(url));

    Instead of trying to read data immediately from not ready QNetworkReply, you should connect to finished like this is done in the example:
    http://wiki.forum.nokia.com/index.ph..._request_in_Qt

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    8

    Re: Problem with networkManager.get(QNetworkRequest(url));

    Hi Divanov,

    Thanks for the reply.

    If I am building QT applications the handler part is working fine. But if I try coding with QT quick(QML) and QT then the handler part is not working.


    Looks like After reading all the data it is not going to the handler. The output I am getting is retriveXML . This show that the handler is not called after finishing the work in retriveXML function. Here is the modified code. Please take a look.
    Any help on this would be great.

    Here is my main.qml FILE: (QT Quick)
    Code:
    Rectangle{id:rectMain;width: 320;height: 460;color: "#282828"
    Rectangle{id: loginMainRect;width: 200;height: 100;anchors.fill: parent; visible:true;
            Rectangle{id:rectLogin;width: 320;height: 460;color: "steelblue"
                Grid{columns: 1;spacing: 20;width: 50;height: 40;
                    Image{id: quitLoginButton;x:0;y: 0;source: "images/quit.png"
                        Text{text: "MyOwnThing";x:70;y: 10;font.pointSize: 10;font.bold: true;color:"black"}}}
                Grid{columns: 1;spacing: 10;anchors.centerIn:parent
                    Rectangle{width: 70;height: 20;color: "steelblue"
                        Text{text: "UserName";x:60;y:4;color:"black";font.pointSize: 10;font.bold: true}}
                    Rectangle{width: 150;height: 20;color:"silver";
                        TextEdit{id:userName;width: 300;text: "";font.pointSize: 10;color: "black";focus: true}}
                    Rectangle{width: 70;height: 20;color: "steelblue"
                        Text{text: "password";x:60;y:4;color:"black";font.pointSize: 10;font.bold: true}}
                    Rectangle{width: 150;height: 20;color:"silver";
                        TextEdit{id:password;width: 300;text: "";font.pointSize: 10;color: "black";focus: true}}
                    Rectangle{width: 150;height: 20;color:"silver";
                        Text{id: outputLoginDetails;x:0;y:4;text: ""}}}
                Grid{columns:2;spacing:150;x:10;y:400
                    BorderImage{id: loginButton;width:70;height: 30;source: "images/button-purple.png"; clip: true;
                        Text{id:loginButtonText;text: "Save";anchors.centerIn: parent;font.pointSize: 10;color: "black"
                            MouseArea {anchors.fill: parent;
                                onClicked: loginButtonClicked();}}}
                    BorderImage{id: loginBackButton;width:70;height: 30;source: "images/button-purple.png"; clip: true;
                        Text{id:loginBackButtonText;text: "Back";anchors.centerIn: parent;font.pointSize: 10;color: "black"
                            MouseArea {anchors.fill: parent;
                                onClicked: loginBackButtonClicked();}}}}}
        }
    function loginButtonClicked()
        {
    
            login.getUserName(userName.text);
            login.getUserPassword(password.text);
            login.retrieveXml();
            outputLoginDetails.text = login.printOutput();
        }
        function loginBackButtonClicked()
             {
             }
    }

    Here is the code for QT:
    Code:
    login::login(): QObject()
    {
        connect(&networkManager, SIGNAL(finished(QNetworkReply*)),this, SLOT(handleNetworkData(QNetworkReply*)));
    }
    
    void login::getUserName(QString userName)
    {
        this->userName=userName;
    }
    void login::getUserPassword(QString password)
    {
        this->password=password;
    }
    
    
    QString login::printOutput()
    {
      return this->output;
    }
    QString login::printPassword()
    {
       return this->output;
    }
    
    void login::retrieveXml()
     {
        output = "retriveXML";
        if (userName.isEmpty() || password.isEmpty())
        {
            output = "Empty Fields";
            return;
        }
        QUrl url("my own urluserName:"+userName+"passowrd="+password);
        networkManager.get(QNetworkRequest(url));
    }
    
    void login::handleNetworkData(QNetworkReply *networkReply)
    {
        output = "handleNetworkData";
        if (!networkReply->error())
            parseXml(networkReply->readAll());
        else
            output = "Network Error";
        networkReply->deleteLater();
    }
    
    void login::parseXml(const QString &data)
     {
         output = "parseXML";
     QXmlStreamReader xml(data);
     coordinates.clear();
     xml.readNext();
     int i=0;
     while (!xml.atEnd()) {
         i++;
         xml.readNext();
         coordinates = xml.tokenString();
         coordinates = xml.tokenString();
         if (xml.tokenType() == QXmlStreamReader::StartElement)
         {
             if (xml.name() == "id")
             {
                 coordinates = xml.readElementText();
                  output = "Login Success";
              }
         }
     }
     if(xml.error())
     {
         output = "Login Failed";
     }
     }
    
    
    Program Main file:
    Code:
    #include <QtGui/QApplication>
    #include "login.h"
    #include <io.h>
    #include <QDeclarativeComponent>
    #include <QDeclarativeContext>
    #include <QDeclarativeView>
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        QDeclarativeView view;
        QDeclarativeContext *context = view.rootContext();
        context->setContextProperty("login", new login);
        view.setSource(QUrl::fromLocalFile("main.qml"));
        view.show();
        return app.exec();
    }

Similar Threads

  1. QNetworkRequest Authentication
    By wenz in forum Qt
    Replies: 2
    Last Post: 2010-10-18, 13:19
  2. Replies: 3
    Last Post: 2010-08-02, 12:56

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×