Results 1 to 11 of 11

Thread: Waiting for readyread and requestfinished signal

  1. #1
    Join Date
    Feb 2009
    Posts
    143
    Thanks
    8

    Default Waiting for readyread and requestfinished signal

    I am building a app that contacts the server for data, gets it and then does some processing before displaying the result.

    I have created a mapping function that maps all the functions that need to be called if a particular request has to be successful.

    This code is the main part where all the calling is done.

    Qt Code:
    1. if (check_if_clientID_is_present())
    2. call = "teci.create.client";
    3. qDebug() << "call"<< call;
    4.  
    5. if(!call.isEmpty() && API_Request_Pointer == false)
    6. mapping(call);
    To copy to clipboard, switch view to plain text mode 

    This is the mapping code where the required functions are called.
    First i prepare the xml document, call the fetch function, then wait for the readyread and requestfinished signals, then call the parsing function and then exit the mapping function

    Qt Code:
    1. void MainWindow::mapping(QString callname)
    2. {
    3. qDebug() <<"in mapping";
    4. QThread thread;
    5. if(call.compare("teci.create.client",callname)== 0)
    6. {
    7. qDebug() <<"inside create client";
    8. API_Request_Pointer = true;
    9. create_client();
    10. fetch();
    11. connect(&http, SIGNAL(readyRead(const QHttpResponseHeader &)),
    12. this, SLOT(readData(const QHttpResponseHeader &)));
    13.  
    14. connect(&http, SIGNAL(requestFinished(int, bool)),
    15. this, SLOT(finished(int, bool)));
    16. parse_create_client();
    17. API_Request_Pointer = false;
    18. qDebug() <<"leaving create client";
    19. }
    20. else if (call.compare("teci.test.reverse",callname)== 0)
    21. {
    22. API_Request_Pointer = true;
    23. reverse_string();
    24. fetch();
    25. connect(&http, SIGNAL(readyRead(const QHttpResponseHeader &)),
    26. this, SLOT(readData(const QHttpResponseHeader &)));
    27.  
    28. connect(&http, SIGNAL(requestFinished(int, bool)),
    29. this, SLOT(finished(int, bool)));
    30. parse_reverse_string();
    31. API_Request_Pointer = false;
    32. }
    33. qDebug() <<"leaving mapping";
    34.  
    35. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void MainWindow::fetch()
    2. {
    3.  
    4. xml.clear();
    5.  
    6. QByteArray string ("username");
    7. string.append(":");
    8. string.append("12345");
    9. QString encoded = string.toBase64();
    10. qDebug() <<"String"<< string <<"Encoded" <<encoded ;
    11.  
    12. QUrl url("http://..../");
    13. url.setUserName("username");
    14. url.setPassword("12345");
    15.  
    16. QHttpRequestHeader header("POST","/api/");
    17. header.setValue("Host",url.host());
    18. header.setValue("Path",url.path());
    19. header.setValue("Authorization","Basic " + encoded);
    20.  
    21. http.setHost(url.host(),url.port(80));
    22.  
    23. connectionId = http.request(header,xmlrequest);
    24. qDebug() << "Leaving fetch";
    25. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void MainWindow::create_client()
    2. {
    3. here i add tags and create the xml
    4.  
    5. xmlrequest.append(doc.toString());
    6. qDebug() << xmlrequest.data();
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void MainWindow::readData(const QHttpResponseHeader &resp)
    2. {
    3.  
    4.  
    5. if (resp.statusCode() != 200)
    6. http.abort();
    7. else
    8. {
    9. xml.addData(http.readAll());
    10. }
    11. qDebug() << "Leaving readdata";
    12. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void MainWindow::finished(int id, bool error)
    2. {
    3. qDebug() <<"Error" <<http.errorString();
    4. if (error)
    5. qWarning("Received error during HTTP fetch.");
    6.  
    7. qDebug() << "Leaving finished";
    8. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void MainWindow::parse_create_client()
    2. {
    3. qDebug() <<"Entering parser";
    4. while (!xml.atEnd())
    5. {
    6. xml.readNext();
    7. qDebug()<<xml.readNext();
    8. if (xml.isStartElement())
    9. {
    10. currentTag = xml.name().toString();
    11.  
    12. if (currentTag == "client")
    13. API_key += xml.attributes().value("key").toString();
    14. qDebug() << "CurrentTag"<<currentTag;
    15.  
    16. }
    17.  
    18. else if (xml.isCharacters() && !xml.isWhitespace())
    19. {
    20.  
    21. if (currentTag == "password")
    22. API_password += xml.text().toString();
    23. }
    24. }
    25.  
    26. qDebug() << "webiste_clientID"<< API_key;
    27. qDebug() << "webiste_serial no"<<API_password;
    28.  
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 

    After reading the data, i write it down in a file.

    This is the output i am getting.

    Qt Code:
    1. Getting data from the webiste
    2. call "teci.create.client"
    3. in mapping
    4. inside create client
    5. <request version="0.1" >
    6. <call name="teci.client.create" >
    7. <client>
    8. <kind>MOBILE</kind>
    9. <name>API Test suite 7001</name>
    10. </client>
    11. </call>
    12. </request>
    13.  
    14. Leaving fetch
    15. Entering parser
    16. 1
    17. webiste_clientID ""
    18. webiste_serial no ""
    19. leaving create client
    20. leaving mapping
    21. Leaving finished
    22. Leaving readdata
    To copy to clipboard, switch view to plain text mode 

    the readyread and requestfinished signals are being called after the parsing function has been called.

    How can i wait for the signal and then call the parsing function??

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Waiting for readyread and requestfinished signal

    Either use waitForReadyRead() or call the parsing method from a slot connected to both signals you are waiting for where you will check if you received both of them or read this article [wiki]Keeping the GUI Responsive[/wiki].
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2009
    Posts
    143
    Thanks
    8

    Default Re: Waiting for readyread and requestfinished signal

    can u please gimme the code to wait for both the signals at the same time?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Waiting for readyread and requestfinished signal

    No, I can't. But you can read the article and write it yourself

    Of course if you posted in the newbie section of the forum, I might have helped you a bit more but since you don't consider yourself a newbie, I'm sure your skills are enough to transform the code properly.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Waiting for readyread and requestfinished signal

    Quote Originally Posted by srohit24 View Post
    can u please gimme the code to wait for both the signals at the same time?
    rohit bhai.. this thing does not work anywhere.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Waiting for readyread and requestfinished signal

    Quote Originally Posted by srohit24 View Post
    can u please gimme the code to wait for both the signals at the same time?
    Actually I can give you the code. But only if you give me your salary (or equivalent) for writing the application. I don't think that's worth it, is it?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Waiting for readyread and requestfinished signal

    Quote Originally Posted by wysota View Post
    Actually I can give you the code. But only if you give me your salary (or equivalent) for writing the application. I don't think that's worth it, is it?
    cool man cool down . he got enough.

  8. #8
    Join Date
    Feb 2009
    Posts
    143
    Thanks
    8

    Default Re: Waiting for readyread and requestfinished signal

    i would have given u my salary but i am not working for anyone nor earning a salary.

    i am just working in Qt to complete a project for this semester.

    I have had a lot of help from this forum, so got a bit carried away

    Anyway, any hints or clue to complete this particular task huh?

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Waiting for readyread and requestfinished signal

    Quote Originally Posted by srohit24 View Post
    i would have given u my salary but i am not working for anyone nor earning a salary.

    i am just working in Qt to complete a project for this semester.
    So maybe a salary for all the projects you will ever participate in thanks to the education you are going to receive?

    I have had a lot of help from this forum, so got a bit carried away

    Anyway, any hints or clue to complete this particular task huh?
    Please read the article I pointed you to. Then use QEventLoop or move your parsing code to another method which will be called when both signals are flagged as received.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Feb 2009
    Posts
    143
    Thanks
    8

    Default Re: Waiting for readyread and requestfinished signal

    If u really wanted returns for your help, in cash, you wouldnt be helping others out, almost every day in this forum

    I split the creating and parsing xml into two parts.

    I am calling the creating b4 fetch and then parsing it in the readData fucntion

    Qt Code:
    1. void RSSListing::readData(const QHttpResponseHeader &resp)
    2. {
    3.  
    4.  
    5. if (resp.statusCode() != 200)
    6. http.abort();
    7. else
    8. {
    9. xml.addData(http.readAll());
    10. if(call.compare("teci.create.client",call)== 0)
    11. {
    12. qDebug() <<"inside parse create client";
    13. parse_create_client();
    14. API_Request_Pointer = false;
    15.  
    16. }
    17. else if (call.compare("teci.test.reverse",call)== 0)
    18. {
    19. qDebug() << "Enter parse string reverse";
    20. parse_reverse_string();
    21. API_Request_Pointer = false;
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Waiting for readyread and requestfinished signal

    Quote Originally Posted by srohit24 View Post
    If u really wanted returns for your help, in cash, you wouldnt be helping others out, almost every day in this forum
    Damn... you got me figured out...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.