Results 1 to 8 of 8

Thread: QNetworkAccessManager doesn't call finished signal

  1. #1
    Join Date
    Nov 2014
    Posts
    8
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default QNetworkAccessManager doesn't call finished signal

    Howdy!
    Trying to make GET request with Qt5 and get the response.
    But slots never called and no network activity from application.
    connect returns true.
    Perhaps QNetworkAccessManager get destroyed after calling get method.

    Give me explanation how it's works.

    Thanks!

    appapi.h
    Qt Code:
    1. #include <QObject>
    2. #include <QNetworkAccessManager>
    3. #include <QNetworkReply>
    4. #include <QNetworkRequest>
    5.  
    6. class AppApi : public QObject
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit AppApi(QObject *parent = 0);
    11. void makeRequest();
    12. QNetworkAccessManager* manager;
    13.  
    14. signals:
    15.  
    16. public slots:
    17. void replyFinished(QNetworkReply* reply);
    18. void slotError(QNetworkReply::NetworkError error);
    19. };
    To copy to clipboard, switch view to plain text mode 

    appapi.cpp
    Qt Code:
    1. AppApi::AppApi(QObject *parent) :
    2. QObject(parent)
    3. {
    4. this->manager = new QNetworkAccessManager(this);
    5. }
    6.  
    7. void AppApi::makeRequest()
    8. {
    9. connect(this->manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
    10.  
    11. QNetworkRequest request;
    12. request.setUrl(QUrl("http://google.com"));
    13.  
    14. QNetworkReply *reply = this->manager->get(request);
    15. connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(slotError(QNetworkReply::NetworkError)));
    16. }
    17.  
    18. void AppApi::replyFinished(QNetworkReply* reply)
    19. {
    20. qDebug("replyFinished");
    21. }
    22.  
    23. void AppApi::slotError(QNetworkReply::NetworkError error)
    24. {
    25. qDebug("slotError");
    26. }
    To copy to clipboard, switch view to plain text mode 

  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: QNetworkAccessManager doesn't call finished signal

    What does the rest of the program look like?
    Where do you create an instance of AppApi and how long does it live?

    Cheers,
    _

  3. #3
    Join Date
    Nov 2014
    Posts
    8
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: QNetworkAccessManager doesn't call finished signal

    anda_skoa, thanks for reply!

    Oh it's my fault, 12 hours with PC is a bad thing

    I forgot to create a new instance:
    Qt Code:
    1. AppApi a;
    2. a.makeRequest();
    To copy to clipboard, switch view to plain text mode 
    Here the "solution".
    Qt Code:
    1. AppApi *a = new AppApi;
    2. a->makeRequest();
    To copy to clipboard, switch view to plain text mode 

    Some questions.
    - How i can make async requests and return response without creating new instances for AppApi? (static functions?)
    For example, i have a class UserApi which inherits AppApi and i need able to use makeRequest().

    - Should i use deleteLater() after i process request response?

    Thanks
    Last edited by Swiftie; 13th November 2014 at 23:37.

  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: QNetworkAccessManager doesn't call finished signal

    Quote Originally Posted by Swiftie View Post
    Some questions.
    - How i can make async requests and return response without creating new instances for AppApi? (static functions?)
    You will always need an instance of the network access manager.
    And you will need some object instance for the slots.

    So a static method could create a network access manager, initiate the request, return both the manager and the reply and the caller could connect to its own slot.
    But the only thing you "safe" is the line that creates the manager, the two lines that create the request and the get() call.

    Why is creating an object of the Api class something you don't want to do?
    Sounds like common delegation to me.

    Quote Originally Posted by Swiftie View Post
    For example, i have a class UserApi which inherits AppApi and i need able to use makeRequest().
    Well, in this case the UserApi object is an instance of AppApi.

    Quote Originally Posted by Swiftie View Post
    - Should i use deleteLater() after i process request response?
    Yes.

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    Swiftie (14th November 2014)

  6. #5
    Join Date
    Nov 2014
    Posts
    8
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: QNetworkAccessManager doesn't call finished signal

    Thanks for reply,
    Here the some changes i made
    Qt Code:
    1. void AppApi::replyFinished(QNetworkReply* reply)
    2. {
    3. qDebug() << "replyFinished" << reply;
    4. reply->deleteLater();
    5. }
    To copy to clipboard, switch view to plain text mode 
    Here the UserApi class:
    Qt Code:
    1. #include "../appapi.h"
    2.  
    3. class UserApi : public AppApi
    4. {
    5. public:
    6. UserApi();
    7. void sendMessage();
    8. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "userapi.h"
    2.  
    3. UserApi::UserApi()
    4. {
    5. }
    6.  
    7. QString UserApi::sendMessage()
    8. {
    9. // Processing content, etc
    10. AppApi api;
    11. api.makeRequest();
    12. }
    To copy to clipboard, switch view to plain text mode 
    Somewhere in code
    Qt Code:
    1. UserApi *c = new UserApi;
    2. c->sendMessage();
    To copy to clipboard, switch view to plain text mode 
    Raises Segmentation fault.
    I know it's a lame code, but after 2 years of Python i forgot even a basics of C++ and it's my first project in Qt.

    Can you explain how properly i make request from UserApi (for example) and return response.

    Best regards

  7. #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: QNetworkAccessManager doesn't call finished signal

    UserApi is derived from AppApi, it is an instance of AppApi, just like in Python.

    So just call the implementation of the base class
    Qt Code:
    1. QString UserApi::sendMessage()
    2. {
    3. makeRequest(); // method of base class AppApi.
    4.  
    5. // return missing here. this is not a void method
    6. }
    To copy to clipboard, switch view to plain text mode 

    Also make sure you connect the manager's finished() signal only once, e.g. right after you create the manager.

    Cheers,
    _

  8. The following user says thank you to anda_skoa for this useful post:

    Swiftie (14th November 2014)

  9. #7
    Join Date
    Nov 2014
    Posts
    8
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: QNetworkAccessManager doesn't call finished signal

    Thanks, i figured out.

    And the last question, it's how to properly return the response?
    I know how to "return" response in synchronous mode (using QEventLoop), but i need to implement returning response from async requests.
    Here the "example"
    Qt Code:
    1. UserApi *c = new UserApi;
    2. QJsonDocument j = QJsonDocument::fromJson(c->sendMessage(..parameters here..)); // c->sendMessage needs to return response from GET request
    To copy to clipboard, switch view to plain text mode 

    Best regards
    Last edited by Swiftie; 14th November 2014 at 16:59.

  10. #8
    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: QNetworkAccessManager doesn't call finished signal

    You get the data in replyFinished() from the QNetworkReply object.

    Then you send it in a signal of your class if you want to handle it outside that object or call a virtual method that you implement in UserApi.

    Returning is inherently synchronous.

    Cheers,
    _

  11. The following user says thank you to anda_skoa for this useful post:

    Swiftie (14th November 2014)

Similar Threads

  1. QNetworkAccessManager no finished() signal emitted
    By realperson in forum Qt Programming
    Replies: 4
    Last Post: 18th January 2018, 09:42
  2. Replies: 7
    Last Post: 7th August 2014, 07:43
  3. QFutureWatcher doesn't get finished signal
    By Mobility in forum Newbie
    Replies: 3
    Last Post: 20th September 2012, 10:25
  4. QNetworkAccessManager does not signal finished
    By lukas.zachy in forum Newbie
    Replies: 5
    Last Post: 26th January 2011, 10:05
  5. QUrlOperator doesn't emit finished signal
    By hayati in forum Qt Programming
    Replies: 16
    Last Post: 26th March 2007, 21:25

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.