Results 1 to 9 of 9

Thread: QNetworkAccesManager (Connect not emiting signal)

  1. #1
    Join Date
    Jul 2013
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QNetworkAccesManager (Connect not emiting signal)

    Hi Guys,

    I have been trying to use QObject::connect , but the signal is not being sent.


    [code]

    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    QNetworkReply* reply = manager->get(QNetworkRequest(url));
    QObject::connect(reply,SIGNAL(finished(QNetworkRep ly*)),this,SLOT(replyFinish(QNetworkReply*)));

    [\code]

    [code]

    void USGSDialog::replyFinish(QNetworkReply* reply)
    {

    if(reply->isOpen()){
    QXmlInputSource input;
    input.setData(reply->readAll());

    QDomDocument doc;
    doc.setContent(input.data());

    // Get the root element
    QDomElement root = doc.firstChildElement();

    // Get Data
    getData(root);
    if(reply->isFinished())
    reply->close();
    }


    [\code]

    My class file looks like this
    [code]

    class USGSDialog : public QDialog
    {
    Q_OBJECT

    public:
    explicit USGSDialog(QWidget *parent = 0);
    ~USGSDialog();


    signals:

    public slots:
    void replyFinish(QNetworkReply* reply);


    [\code]

    Can anybody help me out why the control doesn't go into the slot ? Why is the signal not being emitted ?
    Thanks in Advance
    Last edited by Sricharan; 7th July 2013 at 06:46.

  2. #2
    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: QNetworkAccesManager (Connect not emiting signal)

    Have you run qmake since you declared replyFinish() as a slot?
    Are you getting a warning message about the failing connect() in the console output of the program?
    How have you checked that the slot is not called?
    Have you looked for an error condition on the reply?

  3. #3
    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: QNetworkAccesManager (Connect not emiting signal)

    Well, you obviously are not paying attention to the output of your program, nor do you check the return value of connect().

    Both would have told you that the connect failed, the output would have even told you that there is no such signal QNetworkReply::finished(QNetworkReply*).

    You either need to change the connect's first argument to the network access manager or the second and forth argument to the signature of the network reply's finished signal.

    Cheers,
    _

  4. #4
    Join Date
    Jul 2013
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QNetworkAccesManager (Connect not emiting signal)

    Hi chris and anda,

    Now I changed the code by changing the first argument to QNEtworkaccessManager
    and I am not getting any warnings from the complier

    Qt Code:
    1. QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    2.  
    3. QObject::connect(manager,SIGNAL(QNetworkAccessManager::finished(QNetworkReply*)),this,SLOT(replyFinish(QNetworkReply*)));
    4.  
    5. manager->get(QNetworkRequest(url));
    To copy to clipboard, switch view to plain text mode 

    and I am testing if the ReplyFinsh() is executing by creating a message box to display as soon as the control goes to slot

    Qt Code:
    1. void USGSDialog::replyFinish(QNetworkReply* reply)
    2. {
    3. QMessageBox* rep = new QMessageBox();
    4. rep->setText("Inside Reply Finmsih");
    5. rep->show();
    6.  
    7. if(reply->isOpen()){
    8. input.setData(reply->readAll());
    9.  
    10. doc.setContent(input.data());
    11.  
    12. // Get the root element
    13. QDomElement root = doc.firstChildElement();
    14.  
    15. // Get Data
    16. getData(root);
    17. if(reply->isFinished())
    18. reply->close();
    19. }
    20. if(reply->error() != QNetworkReply::NoError){
    21. QString err = reply->errorString();
    22. QMessageBox* error = new QMessageBox();
    23. error->setText(err);
    24. error->show();
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 

    Still I dont get the slot working.

  5. #5
    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: QNetworkAccesManager (Connect not emiting signal)

    You still are not checking the return value from the connect() call or the the program run time output (in the Application Output pane if you are using Qt Creator). The first will tell you the connect failed, the second will tell you why.

  6. #6
    Join Date
    Jul 2013
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QNetworkAccesManager (Connect not emiting signal)

    Hi Chris,

    I have no warning messages from QT. When I used the same code in a new project, It worked. But I was integrating the code into an existing project, Somehow its not working.

    I have the same problem from this post in the forum

    http://www.qtcentre.org/threads/4025...send-s-nothing

    Is there anything to do with the merging of the code into an existing project.

    Cheers

  7. #7
    Join Date
    Jul 2013
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QNetworkAccesManager (Connect not emiting signal)

    Hi I finally got it working.

    I added QEventLoop

    Qt Code:
    1. QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    2.  
    3. QObject::connect(manager,SIGNAL(QNetworkAccessManager::finished(QNetworkReply*)),this,SLOT(replyFinish(QNetworkReply*)));
    4.  
    5. manager->get(QNetworkRequest(url));
    6.  
    7. loop.exec();
    To copy to clipboard, switch view to plain text mode 

    I am really curious how this worked ?

  8. #8
    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: QNetworkAccesManager (Connect not emiting signal)

    Quote Originally Posted by Sricharan View Post
    I have no warning messages from QT.
    With your last posted code, connect() is returning false. Qt will be outputting:
    Qt Code:
    1. Object::connect: No such signal QNetworkAccessManager::QNetworkAccessManager::finished(QNetworkReply*)
    To copy to clipboard, switch view to plain text mode 
    when you run the program. The signal is called "finished(QNetworkReply*)"

    I am really curious how this worked ?
    It doesn't. There is still no signal called "QNetworkAccessManager::QNetworkAccessManager::fin ished(QNetworkReply*)"

    You might have changed the program's overall behaviour by adding explicit event loop but this particular connection issue has not gone away.

  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: QNetworkAccesManager (Connect not emiting signal)

    QNetworkAccessManager, like most of Qt's I/O classes, works asynchronous, event loop based.

    Usually that event loops comes from the Qt application object created in main().
    If you had to add an explicit event loop you are probably missing that object or you forgot to call its exec() method or you have that code in a thread and forgot to call its exec() method, ...

    And there should be no "QNetworkAccessManager::" in the SIGNAL() macro, only the name of signal and its argument types.

    Cheers,
    _

Similar Threads

  1. problem with emiting a signal
    By msmihai in forum Newbie
    Replies: 2
    Last Post: 3rd January 2009, 14:32
  2. QDateEdit emiting signal problem
    By anafor2004 in forum Newbie
    Replies: 1
    Last Post: 17th June 2008, 07:59
  3. not emiting signal
    By bisz in forum Newbie
    Replies: 4
    Last Post: 3rd October 2007, 07:49
  4. Problem emiting signal in QTreeWidgetItem's subclass
    By Shawn in forum Qt Programming
    Replies: 12
    Last Post: 4th September 2007, 12:08
  5. Emiting signal, which NOT connected to any slots
    By krivenok in forum Qt Programming
    Replies: 7
    Last Post: 27th February 2006, 16:32

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.