Results 1 to 8 of 8

Thread: QNetworkAccessManager - finished() signal is never triggered!

  1. #1
    Join Date
    Jul 2014
    Posts
    3
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Exclamation QNetworkAccessManager - finished() signal is never triggered!

    Hi there guys,

    I've asking this question on a lot of websites for the past few days, but I've been out of luck appearently! No one could help me, so I hope you guys can figure out the problem & how to solve it.


    I'm trying to use QNetworkAcessManager to get the source of a url .. But it seems that there's a problem with the signal-slot complex!
    my onFinished() slot is never triggered! Why?

    Here's my code:
    Qt Code:
    1. void Worker::start(QString url)
    2. {
    3. QNetworkAccessManager *manager = new QNetworkAccessManager();
    4. QNetworkReply *reply = manager->get(QNetworkRequest(QUrl(url)));
    5. QObject::connect(reply, SIGNAL(finished()), this, SLOT(onFinished()));
    6.  
    7.  
    8. void Worker::onFinished()
    9. {
    10. qDebug() << "Slot has been triggered!";
    11.  
    12. QString html = reply->readAll();
    13. }
    To copy to clipboard, switch view to plain text mode 


    And actually I tried to play around & change the code many times without any luck, so please guide me & show me a nice working example to get html source of a webpage.

    And thank you so much in advance;

  2. #2
    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: QNetworkAccessManager - finished() signal is never triggered!

    I don't know exactly what the problem is, but:
    1. One QNetworkAccessManager should be enough for the whole Qt application. Ensure the QNetworkAccessManager gets deleted properly when not needed anymore.
    2. If you're using Qt 5 use the new syntax to connect signals and slots.
    3. Returns QObject::connect(...) a valid connection? (you can test that using qDebug() as well)
    4. Use deleteLater() to delete the reply after reading the data.

  3. #3
    Join Date
    Jul 2014
    Posts
    3
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Exclamation Re: QNetworkAccessManager - finished() signal is never triggered!

    Quote Originally Posted by Infinity View Post
    I don't know exactly what the problem is, but:
    1. One QNetworkAccessManager should be enough for the whole Qt application. Ensure the QNetworkAccessManager gets deleted properly when not needed anymore.
    2. If you're using Qt 5 use the new syntax to connect signals and slots.
    3. Returns QObject::connect(...) a valid connection? (you can test that using qDebug() as well)
    4. Use deleteLater() to delete the reply after reading the data.
    • Yup, using debug() I can verify that the SIGNAL-SLOT connection was done successfully!
    • I'm using Qt 5.3.1, but I don't know about this new syntax you're talking about.
    • Actually I'm doing just a simple code to see why this isn't working for me, so could you please give me a working solution? My code compiles but doesn't work!

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

    Default Re: QNetworkAccessManager - finished() signal is never triggered!

    Is the application's event loop running, or in case of a secondary thread, the thread's event loop?

    Btw, your code stores the reply pointer in a function-local variable called reply, the slots accesses apparently a member with the same name.

    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: QNetworkAccessManager - finished() signal is never triggered!

    could you please give me a working solution
    There are lots of examples: http://qt-project.org/doc/qt-5/examples-network.html
    I'm using Qt 5.3.1, but I don't know about this new syntax you're talking about.
    http://qt-project.org/doc/qt-5/qobject.html#connect-4
    But this should not be the reason for your problem (it was more a recommendation).

  6. #6
    Join Date
    Jul 2014
    Posts
    3
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Exclamation Re: QNetworkAccessManager - finished() signal is never triggered!

    Quote Originally Posted by anda_skoa View Post
    Is the application's event loop running, or in case of a secondary thread, the thread's event loop?

    Btw, your code stores the reply pointer in a function-local variable called reply, the slots accesses apparently a member with the same name.

    Cheers,
    _
    Actually, I'm a newbie with Qt, I don't know what's happening while I'm doing everything as supposed to ( I read documentations all the time! ).
    I could do the mission ( synchronously ) but I hope to do it asynchronously.

    Quote Originally Posted by Infinity View Post
    There are lots of examples: http://qt-project.org/doc/qt-5/examples-network.html

    http://qt-project.org/doc/qt-5/qobject.html#connect-4
    But this should not be the reason for your problem (it was more a recommendation).
    I've seen all these before, but when I do what I see there, nothing works!

    Eventually I could achieve what I want using this synchronous code:
    Qt Code:
    1. QNetworkAccessManager manager;
    2. QNetworkRequest request;
    3. request.setUrl(QUrl(url));
    4. request.setRawHeader("User-Agent", "MyOwnBrowser 1.0");
    5. QNetworkReply *response = manager.get(request);
    6. QEventLoop event;
    7. connect(response,SIGNAL(finished()),&event,SLOT(quit()));
    8. event.exec();
    9. QString html = response->readAll(); // Source should be stored here
    To copy to clipboard, switch view to plain text mode 

    But I need it to be asynchronous, so I tried to change it to something like:
    Qt Code:
    1. QNetworkAccessManager manager;
    2. QNetworkRequest request;
    3. request.setUrl(QUrl(url));
    4. request.setRawHeader("User-Agent", "MyOwnBrowser 1.0");
    5. QNetworkReply *response = manager.get(request);
    6. connect(response, SIGNAL(finished()), this, SLOT(onFinished()));
    To copy to clipboard, switch view to plain text mode 
    But again, slot is never triggered!

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkAccessManager - finished() signal is never triggered!

    That last piece won't work because the network manager is destroyed at the end of the scope it is created in (created on the stack).

    Since the code snippets you post are correct in themselves, the problem must be elsewhere.

    Could you post a buildable example of what you have or at least show the main function and how it creates this worker object?

    Cheers,
    _

  8. #8
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QNetworkAccessManager - finished() signal is never triggered!

    Hi, since your class name is Worker, I am guessing that you might be running this code in a separate thread. Are you sure there's an event loop running in your thread?

    Can you show how you have created the Worker, the QThread, etc?

    Edit: apologies, I see anda_skoa asked about the event loop in a prior reply...

Similar Threads

  1. QNetworkAccessManager no finished() signal emitted
    By realperson in forum Qt Programming
    Replies: 4
    Last Post: 18th January 2018, 09:42
  2. QNetworkAccessManager Finished() Error
    By ttimt in forum Newbie
    Replies: 10
    Last Post: 25th February 2014, 14:21
  3. QNetworkAccessManager does not signal finished
    By lukas.zachy in forum Newbie
    Replies: 5
    Last Post: 26th January 2011, 10:05
  4. Replies: 0
    Last Post: 24th May 2010, 09:11
  5. Replies: 1
    Last Post: 11th April 2010, 15:21

Tags for this Thread

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.