Results 1 to 6 of 6

Thread: WebService REST CPP-QML

  1. #1
    Join Date
    Dec 2015
    Posts
    35
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default WebService REST CPP-QML

    Hi Forum,

    I have a problem, I want to get the response from the serveur if the operation is Ok or not, So I analysed
    Qt Code:
    1. QNetworkRequest::HttpStatusCodeAttribute
    To copy to clipboard, switch view to plain text mode 

    // *** createProject( param ) ***
    Qt Code:
    1. //Create project using post json
    2. // ...
    3. reply = manager->post(request, dataByteArray);
    4. /* The finished() signal is triggered once an HTTP request is complete, by connect method we are connecting finished signal with the slot method onRequestFinished(user defined method). If it returns true it means connection is successful else not. You can process the response of the call in this user defined method */
    5. result = connect(reply, SIGNAL(finished()), this, SLOT(onFinished()));
    To copy to clipboard, switch view to plain text mode 

    // *** slot onFinished() ***
    Qt Code:
    1. bool Connection::onFinished()
    2. {
    3. codeResponse = false;
    4. qDebug() << "QNetworkRequest::HttpStatusCodeAttribute" << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() << "received";
    5.  
    6. if(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 201)
    7. {
    8. codeResponse = true;
    9. }
    10. return codeResponse;
    11. }
    To copy to clipboard, switch view to plain text mode 

    // *** getter ***
    Qt Code:
    1. bool Connection::getResponse()
    2. {
    3. return codeResponse;
    4. }
    To copy to clipboard, switch view to plain text mode 

    QML code
    Qt Code:
    1. connection.createProject( param );
    2. rep = connection.getResponse()
    3. console.log("Response: "+rep)
    4.  
    5. infoAdd.open()
    To copy to clipboard, switch view to plain text mode 

    All is OK, but in QML sometimes function getResponse() runs before the slot onFinished() ??

    So there is an other way whitout using getResponse() to get response from onFinichid() ?

    I tried to use connection.onFinichid() in QML but it executed 2 times :
    1st with connection.createProject( param ), 2nd directly connection.onFinichid() == > Response always = false

    I try to find an easy way to get the resp without getter getResponse()

    Cheers,
    Last edited by RegMe; 25th May 2016 at 11:43.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: WebService REST CPP-QML

    If you call the method that initiates the HTTP post and immediately call the getter, then the post has not happend yet.

    What you could do is add a signal to the Connection class and emit it in onFinished(), it could even have the code as its argument.

    If the "connection" object in QML has been created in QML then you can use the usual onSignalname syntax, otherwise you can use a Connections element or connect a script function using connection.signalname.connect(functionname)

    Cheers,
    _

  3. #3
    Join Date
    Dec 2015
    Posts
    35
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: WebService REST CPP-QML

    Thanks, I still have the problem

    // *** slot onFinished() ***
    Qt Code:
    1. void Connection::onFinished()
    2. {
    3. bool codeResponse = false;
    4. qDebug() << "QNetworkRequest::HttpStatusCodeAttribute" << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() << "received";
    5.  
    6. if(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 201)
    7. {
    8. codeResponse = true;
    9. }
    10.  
    11. emit finishChanged(codeResponse);
    12.  
    13. }
    To copy to clipboard, switch view to plain text mode 

    QML code
    Qt Code:
    1. Connection{
    2. id: connection
    3. onFinishChanged: {
    4. console.log("Received : " + codeResponse)
    5. updateOK = codeResponse
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    QML code : createProject
    Qt Code:
    1. connection.createProject( param );
    2.  
    3. infoUpdate.open()
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. MessageDialog{
    2. id:infoUpdate
    3.  
    4. icon: StandardIcon.Information
    5. text: updateOK == true ? "UPDATE OK":"ERROR UPDATING !!"
    6. onAccepted: {
    7.  
    8. updateOK = false
    9.  
    10. updateWindow.close()
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    Result out : ERROR UPDATING !!

    I still have the probleme of synchronisation: infoUpdate runs before the end of the Slot onFinished()

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: WebService REST CPP-QML

    Yes, you still call infoUpdate.open() right after you queue the request.
    If you don't give your program any chance to actually perform the request, then it can't.

    Just consider a real live situation with asynchronous delivers: ordering food in a restaurant

    1) you give the waiter your order
    2) you immediately try to start eating?

    No, that won't work, as the waiter hadn't had time to bring your order to the kitchen, the cook didn't have time to prepare what you've asked for, so the waiter couldn't have brought it back yet

    What you really do is

    1) you give your order to the waiter
    2) you wait until the waiter returns with your meal
    3) you eat

    In your code the waiter is the HTTP call, the kitchen is the REST server.
    You can only start eating (processing the data), once the waiter has returned.

    Cheers,
    _

  5. #5
    Join Date
    Dec 2015
    Posts
    35
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: WebService REST CPP-QML

    Hiii,

    It has been a long time I called infoUpdate.open() inside :
    Qt Code:
    1. Connection{
    2. id: connection
    3. onFinishChanged: {
    4. console.log("Received : " + codeResponse)
    5. updateOK = codeResponse
    6. infoUpdate.open()
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    I think that you mean this method ??

    What happed to qtcentre !: off for a week !!!

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: WebService REST CPP-QML

    Yes, that's what I meant.

    Cheers,
    _

Similar Threads

  1. Redmine Rest API
    By Binary01 in forum Qt Programming
    Replies: 6
    Last Post: 24th May 2016, 11:00
  2. WebService
    By Binary01 in forum Qt Quick
    Replies: 1
    Last Post: 29th February 2016, 11:11
  3. Replies: 1
    Last Post: 15th June 2012, 12:45
  4. Webservice with QT
    By BP in forum Newbie
    Replies: 9
    Last Post: 16th June 2011, 05:08
  5. Using webservice in Qt
    By mind_freak in forum Qt Programming
    Replies: 1
    Last Post: 16th May 2011, 06:50

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.