Results 1 to 9 of 9

Thread: Getting data from a QNetworkReply

  1. #1
    Join Date
    Nov 2012
    Posts
    47
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Getting data from a QNetworkReply

    That is very long and intimidating.
    Anyway, I got setcookiesforUrl() and cookiesforUrl() done without that.



    But I don't want to use global variable and my read all data is in another function.
    I want to return it like this :
    Qt Code:
    1. QByteArray data = emit finished(QNetworkReply*);
    2. connect(loginnam, finished(...), this, readdata(...);
    3. QByteArray readdata()
    4. {
    5. reply->readall.....
    6. }
    7. //Want to return slot values
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //This code below is the same cause can't get the return value
    2. //In case someone said you can't emit finished or what lol
    3. connect (loginnam, finished(), this, getdata())
    4. QByteArray MainWindow::getdata()
    5. {
    6. //Emit here
    7. QByteArray returnvalue = emit readdata();
    8. return returnvalue;
    9. }
    10. QByteArray readdata()
    11. {
    12. reply->readall.....
    13. }
    To copy to clipboard, switch view to plain text mode 
    There's no finished signal except using connect with sender as qnetworkaccessmanager object. So I can't use emit finished......
    Is there any solutions to return the slot value with signal as finished? Other than private variables


    It's unnecessary question cause I have two global variable to be used globally already, but I would like to know how to do it.
    Last edited by ttimt; 2nd March 2014 at 16:39.
    I'm a newbie. Don't trust me

  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: QNetworkCookieJar

    That doesn't make any sense or I don' understand at all what you are trying to do.

    How is that related to the cookies? Is that for loading the cookies? Where do you see a need for signals?

    Cheers,
    _

  3. #3
    Join Date
    Nov 2012
    Posts
    47
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QNetworkCookieJar

    Quote Originally Posted by anda_skoa View Post
    That doesn't make any sense or I don' understand at all what you are trying to do.

    How is that related to the cookies? Is that for loading the cookies? Where do you see a need for signals?

    Cheers,
    _
    Well I mean the QNetworkAccessManager, a little bit related to cookie, I believe is unnecessary to create new thread

    I use connect(accessmanager, finished(...), this, readdata(...))
    What readdata does is only read QNetworkReply->readAll and save it in a QByteArray variable,
    and I want to return it to use it in the function, don't want to use global variable

    What I know to get return value from slot is like : Qbytearray data = emit->signal()

    Thus, I want to emit finished() to save the return value.
    But finished is QNetworkReply signal, not a signal I implemented myself
    I definitely can't add something like this to the header file
    Qt Code:
    1. signals:
    2. void QNetworkReply::finished();
    To copy to clipboard, switch view to plain text mode 
    So what i'm asking is there anyway to get the slot value since I can't use emit on finished() and I don't want to use a global variable
    Last edited by ttimt; 2nd March 2014 at 17:40.
    I'm a newbie. Don't trust me

  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: QNetworkCookieJar

    Quote Originally Posted by ttimt View Post
    Well I mean the QNetworkAccessManager, a little bit related to cookie, I believe is unnecessary to create new thread
    It is not related to the other thread at all, created a new thread.

    Quote Originally Posted by ttimt View Post
    I use connect(accessmanager, finished(...), this, readdata(...))
    What readdata does is only read QNetworkReply->readAll and save it in a QByteArray variable,
    and I want to return it to use it in the function, don't want to use global variable
    What does "the function" mean?
    Do you mean main()?

    Quote Originally Posted by ttimt View Post
    What I know to get return value from slot is like : Qbytearray data = emit->signal()
    While not impossible, it is very unlikely that you have a variable called "emit" in a Qt program :-)

    Quote Originally Posted by ttimt View Post
    Thus, I want to emit finished() to save the return value.
    Don't see a problem with that, your finished() signal can easily transport a QByteArray.

    Quote Originally Posted by ttimt View Post
    But finished is QNetworkReply signal, not a signal I implemented myself
    Obviously

    Quote Originally Posted by ttimt View Post
    I definitely can't add something like this to the header file
    Qt Code:
    1. signals:
    2. void QNetworkReply::finished();
    To copy to clipboard, switch view to plain text mode 
    True, but you can call your own signal finished() as well. Signal names do not need to be globally unique.

    Quote Originally Posted by ttimt View Post
    So what i'm asking is there anyway to get the slot value since I can't use emit on finished() and I don't want to use a global variable
    What do you mean with "slot value"? The return value of a method that is marked as a slot? Or a value created inside a slot?

    Cheers,
    _

  5. #5
    Join Date
    Oct 2012
    Posts
    132
    Thanks
    10
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QNetworkCookieJar

    Your code is very confused.
    To get an idea how QNetworkReply and related classes have to be used, you can check out the several network examples (of course in addition to reading the documentation):
    http://qt-project.org/doc/qt-5.0/qtn...s-network.html

  6. #6
    Join Date
    Nov 2012
    Posts
    47
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Getting data from a QNetworkReply

    Ok my question is really just how to read/get the return value if the function is ran by connect(),

    I only know how to get it using data = emit signal() but I can't emit finished() cause it's not mine.
    Also not data = emit->signal(). Maybe typo or what


    Anyway, I believe the code below will work
    Qt Code:
    1. //Don't want this connect cause can't get the value returned by readdata()
    2. //connect(loginnam, SIGNAL(finished(QNetworkReply*)), this, SLOT(readdata(QNetworkReply*)));
    3. QNetworkReply *a = loginnam->get(*request);
    4.  
    5. while(!a->isFinished())
    6. {
    7. }
    8.  
    9. data = readdata(a);
    To copy to clipboard, switch view to plain text mode 
    I'm a newbie. Don't trust me

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Getting data from a QNetworkReply

    Ok my question is really just how to read/get the return value if the function is ran by connect(),

    I only know how to get it using data = emit signal() but I can't emit finished() cause it's not mine.
    Que?

    From the trusty documentation we get the unequivocal:
    Signals are automatically generated by the moc and must not be implemented in the .cpp file. They can never have return types (i.e. use void).
    Frankly, you seem very confused. If you want to know that a network request has finished then you have no choice but to listen for the finished() signal from the QNetworkReply or QNetworkAccessManager. If you busy-wait on the request like you suggest then your application will not function: the request will not be sent until a Qt event loop is running again. You can use QEventLoop (connect reply->finished() to loop exit()) to block and maintain some responsiveness but a blocking design is really not the Qt way.

    When the request is successful then the data from the response can be read from the QNetworkReply. What you do with it then has nothing to do with the QNetworkReply signal/slot connection. You can choose to expose that data through an interface on your class, or send it on to another QObject(s) by emitting a signal of your own which can, as anda_skoa pointed out, happily carry a QByteArray payload.
    Last edited by ChrisW67; 3rd March 2014 at 05:41.

  8. #8
    Join Date
    Nov 2012
    Posts
    47
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Getting data from a QNetworkReply

    Quote Originally Posted by ChrisW67 View Post
    If you want to know that a network request has finished then you have no choice but to listen for the finished() signal from the QNetworkReply or QNetworkAccessManager. If you busy-wait on the request like you suggest then your application will not function: the request will not be sent until a Qt event loop is running again. You can use QEventLoop (connect reply->finished() to loop exit()) to block and maintain some responsiveness but a blocking design is really not the Qt way.
    So while(isfinished()) would not work? Maybe I'll will try an event loop.

    So maybe here's a "simplier" form of my question.
    After I do reply->readAll() in a function/method named readdata, I want to store it in a variable called "data" and use it in another function. And the way I ran the readdata function is by connect( loginnam, finished(), this, readdata()). So there's no way I could get the return value?

    The important thing is the (loginnam, finished(),,), this is the only way I know, my "knowledge" of listening/waiting for the finished() signal, not sure if there's other, not to use connect, so I can return the value from readdata()




    Quote Originally Posted by ChrisW67 View Post
    Que?

    From the trusty documentation we get the unequivocal:
    Signals are automatically generated by the moc and must not be implemented in the .cpp file. They can never have return types (i.e. use void).
    Que? Googled, is that Spanish?



    I didn't go to the trusty documentation and read everything, but I did some googling.

    First link :
    http://qt-project.org/forums/viewthread/37451
    Saw some argument, or debate, there's a guy called "raven-worx" a certified specialist with rank "Mad Scientist" said :
    Thats just NOT true.
    Not Practicable, maybe I can use disconnect? I'm doing GUI


    Second link :
    http://stackoverflow.com/questions/5...return-a-value
    The rated +15 answer said it is possible, of course by changing slot and signal to non void, he/she also said you'll only get the last value.
    This is not a web browser or what, I could get some data from the readAll and append it to a list.


    Third link:
    http://stackoverflow.com/questions/1...t-is-impossibl
    Stackoverflow again. i'm using QByteArray not some double/float.


    Fourth link :
    http://stackoverflow.com/questions/1...t-return-value
    And this, one of them mentioned about Qmetaobject::invokemethod.
    Also saw this function metioned in this forum in some thread that wysota posted. But I think invokemethod cant't listen to signals. I don't know yet
    I'll take a look at that later but it's unfamiliar to me. I used some basic Qt method not like all of them.
    and the documentation :
    bool QMetaObject::invokeMethod ( QObject * obj, const char * member, Qt::ConnectionType type, QGenericReturnArgument ret, QGenericArgument val0 = QGenericArgument( 0 ), QGenericArgument val1 = QGenericArgument(), QGenericArgument val2 = QGenericArgument(), QGenericArgument val3 = QGenericArgument(), QGenericArgument val4 = QGenericArgument(), QGenericArgument val5 = QGenericArgument(), QGenericArgument val6 = QGenericArgument(), QGenericArgument val7 = QGenericArgument(), QGenericArgument val8 = QGenericArgument(), QGenericArgument val9 = QGenericArgument() ) [static]
    So it takes time


    Fifth link:
    http://www.qtcentre.org/threads/18833-slot-return-type
    This! The global variable! Maybe is what i'm talking about, don't want to use global variable or class member, just local.
    Spirit suggested to use class member, and QMetaObject::InvokeMethod again


    Sixth link:
    http://www.qtcentre.org/threads/2672...a-return-value
    Slot with a return value! QMetaObject::invokeMethod()




    From stackoverflow and qt-project, they used signal slot as non void.
    And as I stated above, i'm not sure whether invokemethod can listen to signals, will read its documentation later.
    My real question goes back to this post's first paragraph.


    And, after those 6 links, maybe I misinterpreted all of them again, or maybe they're discussing different things, cause maybe like you said :
    I might be really really really confused.

    Also, in the end, if i'm really confused, class member like spirit suggested or global variable will work just fine!
    Last edited by ttimt; 3rd March 2014 at 10:06.
    I'm a newbie. Don't trust me

  9. #9
    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: Getting data from a QNetworkReply

    Quote Originally Posted by ttimt View Post
    So maybe here's a "simplier" form of my question.
    After I do reply->readAll() in a function/method named readdata, I want to store it in a variable called "data" and use it in another function.
    You can call the other function and pass data, or, if both functions are methods of the same class, store data as a member variable and access it from both methods, or if not, store data in an object that both functions have acess to.

    Not really a question of Qt though, that applies to C++ in general.

    Lets do an example
    Qt Code:
    1. class Foo : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. void login(const LoginData &loginData);
    6.  
    7. signals:
    8. void receivedLoginResponse(const QByteArray &data);
    9.  
    10. private:
    11. void doSomething(const QByteArray &data);
    12. void doSomethingElse();
    13.  
    14. private:
    15. QByteArray m_data;
    16.  
    17. private slots:
    18. void loginiFinished(QNetworkReply *reply);
    19. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void Foo::login(const LoginData &loginData)
    2. {
    3. // prepare QNetworkRequest
    4.  
    5. QNetworkReply *reply = networkAccessManager->get(...);
    6. connect(networkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(loginFinished(QNetworkReply*)));
    7. }
    8.  
    9. void Foo::doSomething(const QByteArray &data)
    10. {
    11. qDebug() << Q_FUNC_INFO << "data .length=" << data.length();
    12. }
    13.  
    14. void Foo::doSomethingElse()
    15. {
    16. qDebug() << Q_FUNC_INFO << "m_data .length=" << m_data.length();
    17. }
    18.  
    19. void Foo::loginFinished(QNetworkReply *reply)
    20. {
    21. QByteArray data = reply->readAll();
    22.  
    23.  
    24. // call a function with data
    25. doSomething(data);
    26.  
    27. // call a method of same object, share data as member
    28. m_data = data;
    29. doSomethingElse();
    30.  
    31. // let some other class handle the data
    32. emit receivedLoginResponse(data);
    33. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

Similar Threads

  1. Replies: 0
    Last Post: 5th December 2011, 20:26
  2. Replies: 2
    Last Post: 14th August 2011, 05:47
  3. QNetworkReply: how to know when data is sent?
    By mentalmushroom in forum Qt Programming
    Replies: 17
    Last Post: 24th June 2011, 08:22
  4. Replies: 1
    Last Post: 8th March 2011, 06:27
  5. When to delete QNetworkReply?
    By QPlace in forum Qt Programming
    Replies: 5
    Last Post: 11th February 2009, 12:46

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.