Results 1 to 9 of 9

Thread: Getting data from a QNetworkReply

Hybrid View

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

    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

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    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 06:41.

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

    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 11:06.
    I'm a newbie. Don't trust me

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

    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, 21:26
  2. Replies: 2
    Last Post: 14th August 2011, 06:47
  3. QNetworkReply: how to know when data is sent?
    By mentalmushroom in forum Qt Programming
    Replies: 17
    Last Post: 24th June 2011, 09:22
  4. Replies: 1
    Last Post: 8th March 2011, 07:27
  5. When to delete QNetworkReply?
    By QPlace in forum Qt Programming
    Replies: 5
    Last Post: 11th February 2009, 13: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
  •  
Qt is a trademark of The Qt Company.