Results 1 to 5 of 5

Thread: QNetworkAccessManager no finished() signal emitted

  1. #1
    Join Date
    May 2012
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QNetworkAccessManager no finished() signal emitted

    Hello Folks,

    I've a strange problem with QNetworkAccessManager, I read the documentation, but it doesn't work. My example hast only a few lines of code, so its really easy to follow it. I'm using Qt-4.8.1 under Ubuntu 11.10. At the internet I found some articles with the same issue but no solutions... so I'm posting here now.

    Qt Code:
    1. void TmdbLookup::movieLookup()
    2. {
    3. bool ok = false;
    4. QNetworkRequest request;
    5. request.setUrl(QUrl("http://qt.nokia.com"));
    6. manager_ = new QNetworkAccessManager(this);
    7.  
    8. ok = connect(manager_, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
    9. qDebug() << "connect: " << ok;
    10. manager_->get(request);
    11. }
    12.  
    13. void TmdbLookup::replyFinished(QNetworkReply *reply)
    14. {
    15. qDebug() << "Request Finished";
    16. reply->deleteLater();
    17. }
    To copy to clipboard, switch view to plain text mode 

    The problem I've is the finished(QNetworkReply*) singal is never emitted, but the connect returns true. I never had any problems with signal / slots in qt. But the QNetworkAccessManager will not work and I can't find an other Example or a solution.

    In my second try I'll connect the finish() signal not to the QNetworkAccessManager. I'll connect it directly to the QNetworkReply.

    Qt Code:
    1. void TmdbLookup::movieLookup()
    2. {
    3. bool ok = false;
    4. QNetworkRequest request;
    5. request.setUrl(QUrl("http://qt.nokia.com"));
    6. manager_ = new QNetworkAccessManager(this);
    7.  
    8. ok = connect(manager_, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
    9. QNetworkReply *reply = manager_->get(request);
    10. ok = connect(reply, SIGNAL(finished()), this, SLOT(readyRead()));
    11. qDebug() << "connect: " << ok;
    12. }
    13.  
    14. void TmdbLookup::readyRead()
    15. {
    16. qDebug() << "ready read";
    17. }
    To copy to clipboard, switch view to plain text mode 

    The finished() signal is not emitted.

    Can someone help me to solve my problem? I'm really confused, in these small examples there is no space for mistakes so what I'm doing wrong?

    so long
    realperson

  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: QNetworkAccessManager no finished() signal emitted

    Make sure you program is returning to the Qt event loop.

    Try this complete example:
    Qt Code:
    1. #include <QtCore>
    2. #include <QtNetwork>
    3. #include <QDebug>
    4.  
    5. class Fetcher: public QObject
    6. {
    7. Q_OBJECT
    8. public:
    9. Fetcher(QObject *p = 0):
    10. QObject(p),
    11. nam(new QNetworkAccessManager(this)),
    12. reply(0)
    13. {
    14. connect(nam, SIGNAL(finished(QNetworkReply*)), SLOT(namFinished(QNetworkReply*)));
    15. }
    16.  
    17. void fetch(const QUrl &url) {
    18. reply = nam->get(QNetworkRequest(url));
    19. connect(reply, SIGNAL(finished()), SLOT(slotFinished()));
    20. connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(slotError(QNetworkReply::NetworkError)));
    21. }
    22.  
    23. public slots:
    24. void slotError(QNetworkReply::NetworkError code) {
    25. qDebug() << Q_FUNC_INFO << "Error" << code;
    26. }
    27.  
    28. void namFinished(QNetworkReply *reply) {
    29. qDebug() << Q_FUNC_INFO << reply;
    30. }
    31.  
    32. void slotFinished() {
    33. qDebug() << Q_FUNC_INFO << "Error" << reply->error();
    34. QByteArray ba = reply->readAll();
    35. qDebug() << Q_FUNC_INFO << "Bytes read" << ba.size();
    36. reply->deleteLater();
    37. reply = 0;
    38.  
    39. qApp->quit();
    40. }
    41.  
    42. private:
    43. QNetworkAccessManager *nam;
    44. QNetworkReply *reply;
    45. };
    46.  
    47.  
    48. int main(int argc, char *argv[])
    49. {
    50. QCoreApplication app(argc, argv);
    51.  
    52. Fetcher f;
    53. f.fetch(QUrl("http://qt.nokia.com"));
    54.  
    55. return app.exec();
    56. }
    57. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    I have allocated my QNetworkAccessManager on the heap like your example although I normally would not. It receives both finished() signals and no error signal.

  3. #3
    Join Date
    May 2012
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkAccessManager no finished() signal emitted

    Hello ChrisW67,
    Hello folks,

    special thanks for your help through your code I found my bad mistake, the problem is really simple... I'll explain the problem and the solution.

    You've a class called TmdbLookup. This class will create all necessary network stuff to make a lookup on a website. At the next step, we'll use our Lookup class from the QMainWindow, in a slot. So you click on a button and the slot will do the rest.

    Lookup.h
    Qt Code:
    1. class TmdbLookup : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit TmdbLookup(QObject *parent = 0);
    6. void movieLookup();
    7.  
    8. public slots:
    9. void replyFinished(QNetworkReply *reply);
    10.  
    11. private:
    12. QNetworkAccessManager *manager_;
    13. };
    To copy to clipboard, switch view to plain text mode 

    Lookup.cpp
    Qt Code:
    1. #include "tmdblookup.h"
    2.  
    3. TmdbLookup::TmdbLookup(QObject *parent) :
    4. QObject(parent)
    5. {
    6. }
    7.  
    8. void TmdbLookup::movieLookup()
    9. {
    10. qDebug() << "Lookup called";
    11. QNetworkRequest request;
    12. request.setUrl(QUrl("http://qt.nokia.com"));
    13. manager_ = new QNetworkAccessManager(this);
    14. connect(manager_, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
    15. manager_->get(request);
    16. }
    17.  
    18. void TmdbLookup::replyFinished(QNetworkReply *reply)
    19. {
    20. qDebug() << "Request Finished";
    21. }
    To copy to clipboard, switch view to plain text mode 

    mainwindows.cpp
    Qt Code:
    1. void MainWindow::lookup()
    2. {
    3. TmdbLookup tmdb(this);
    4. tmdb.movieLookup();
    5. }
    To copy to clipboard, switch view to plain text mode 

    This code compiles correctly without errors or warnings. If you execute the code, the TmdbLookup will create http traffic, so the basics will work, but the finished(QNetworkReply*) will never be emitted.

    The Problem is this:
    Qt Code:
    1. void MainWindow::lookup()
    2. {
    3. TmdbLookup tmdb(this);
    4. tmdb.movieLookup();
    5. }
    To copy to clipboard, switch view to plain text mode 

    If you execute this function, the TmdbLookup will be created. You call the additional lookup function to do the request. And then you're leaving the scope of the function.

    Qt Code:
    1. TmdbLookup tmdb(this);
    To copy to clipboard, switch view to plain text mode 

    This will allocate TmdbLookup locally on the stack and the object will be destroyed before the signal was emitted. So you can solve this problem, with a member variable.

    Qt Code:
    1. void MainWindow::lookup()
    2. {
    3. tmdb_ = new TmdbLookup(this);
    4. tmdb_->movieLookup();
    5. }
    To copy to clipboard, switch view to plain text mode 
    And now its works! The TmdbLookup will not destroyed to early. The code is not 100% complete for memory leaks follow the hints at the Qt-Documentation.

    so long
    realperson

  4. #4
    Join Date
    Jan 2006
    Posts
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QNetworkAccessManager no finished() signal emitted

    I had the same need, so I'm starting a Qt5-based tmdb access library, since I couldn't find one anywhere. It's currently at http://quickgit.kde.org/?p=scratch/dfaure/libtmdbqt.git

    (if you read this in the far future and you can't find it, try http://quickgit.kde.org/?p=libtmdbqt.git)

    Contributions welcome, there is much to do still (but at least I can query for movies and get results, already...)

  5. #5
    Join Date
    Jan 2006
    Posts
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QNetworkAccessManager no finished() signal emitted


Similar Threads

  1. QNetworkAccessManager does not signal finished
    By lukas.zachy in forum Newbie
    Replies: 5
    Last Post: 26th January 2011, 10:05
  2. Replies: 2
    Last Post: 26th April 2010, 11:46
  3. signal emitted when I zoom
    By mastupristi in forum Qwt
    Replies: 1
    Last Post: 8th July 2009, 18:02
  4. Signal emitted more than once?
    By dbrmik in forum Qt Programming
    Replies: 3
    Last Post: 13th March 2009, 13:44
  5. Program crash when a signal is emitted
    By croscato in forum Qt Programming
    Replies: 7
    Last Post: 22nd November 2008, 23:24

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.