Results 1 to 18 of 18

Thread: QNetworkAccessManager within QThread

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default QNetworkAccessManager within QThread

    Hey,

    I'm trying to make an application that is designed to grab data from a website in a thread but for some reason, despite having no errors in compile/run, the slot never seems to get fired.

    Heres my code:

    Qt Code:
    1. void itemthread::run() {
    2. int i;
    3. QTextStream out(stdout, QIODevice::WriteOnly);
    4. for(i=1; i < 10; i++) {
    5. out << i;
    6. QNetworkAccessManager *manager = new QNetworkAccessManager();
    7. connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(gotData(QNetworkReply *)));
    8.  
    9. QString url_string = QString("http://www.someurl.com?arg=%1").arg(i);
    10. QUrl url(url_string);
    11.  
    12. QNetworkRequest request;
    13. request.setUrl(url);
    14. request.setRawHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
    15.  
    16. manager->get(request);
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

    "itemthread" is a class that extends QThread. My slot "gotData" never seems to be fired, no matter how long you leave the program running. I had to remove this from new QNetworkAccessManager because it was causing a error during runtime ("QObject: Cannot create children for a parent that is in a different thread.").

    How can I request URLs while inside a QThread?

    Thanks,

    Tom

  2. #2
    Join Date
    Jan 2010
    Posts
    73
    Thanks
    6
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkAccessManager within QThread

    My guess is that your thread does not have an event loop. Try a simple experiment and create a QEventLoop and call exec on it.

    Also, I assume that you are taking care of delete the allocated network managers and such somewhere.

  3. #3
    Join Date
    Nov 2009
    Posts
    68
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded

    Default Re: QNetworkAccessManager within QThread

    Make sure that you have gotData(QNetworkReply *) is defined in your *.h file as a slot.

    If you have it defined as a standard function the compiler will not give you an error but it will not work.

    Something like:

    slot:
    void gotData(QNetworkReply *trigger);

    BTW: I found this out the hard way.

  4. #4
    Join Date
    Dec 2013
    Location
    Jerada, Morroco
    Posts
    106
    Thanks
    11
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkAccessManager within QThread

    I have the same problem, when i put the code in the constructor the slot is working but when i put it in the run method it doesn't work , someone know why ?

  5. #5
    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 within QThread

    Quote Originally Posted by hassinoss View Post
    someone know why ?
    Yes, easy, you have done something wrong.

    Cheers,
    _

  6. #6
    Join Date
    Dec 2013
    Location
    Jerada, Morroco
    Posts
    106
    Thanks
    11
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkAccessManager within QThread

    This is my code
    Qt Code:
    1. Manager::Manager(QWidget *parent)
    2. {
    3. m_url.setUrl("http://time.jsontest.com/");
    4. m_request.setUrl(m_url);
    5. connect(&m_networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onResult(QNetworkReply*)));*/
    6. m_networkManager.get(m_request);
    7. }
    8.  
    9. void Manager::onResult(QNetworkReply* reply)
    10. {
    11. if(reply)
    12. {
    13. QString data = (QString) reply->readAll();
    14.  
    15. ......;
    16. }
    17. }
    18.  
    19. void Manager::run()
    20. {
    21. while(true)
    22. {
    23.  
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 


    whene i put the code in the constructor in the run method it doesn't work , there is any problem with doing this

  7. #7
    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 within QThread

    Well, assuming that "Manager" is a QThread, it has a weird parent (QWidget can't live in a secondary thread).

    Also the networkmanager is created in the context of the thread running the manager constructor, so it will do its event processing in that thread, not in the context of the "Manager" thread.

    Your run() method also looks suspiciously like it doesn't run the thread's event loop, but since the code is missing this is just a wild guess.

    Cheers,
    _

  8. #8
    Join Date
    Dec 2013
    Location
    Jerada, Morroco
    Posts
    106
    Thanks
    11
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkAccessManager within QThread

    I changed the run method with a method with a loop for with 10 loops, and i see that it wait the end of the loop and it enter after that to the onResult method 10 times.

  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: QNetworkAccessManager within QThread

    So your loop enters event processing by calling exec() and each time your slot is invoked you quit() the event loop and continue in your outer loop?

    Why not just count the invocations and quit on the last one?

    Cheers,
    _

  10. #10
    Join Date
    Dec 2013
    Location
    Jerada, Morroco
    Posts
    106
    Thanks
    11
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkAccessManager within QThread

    No what i want is for each loop the onResult must be invoked, but im my case its invoked just until the end of the loop.

  11. #11
    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 within QThread

    Your posting is missing the code for the loop. Again.

    What do you need the thread for anyway?

    Cheers,
    _

Similar Threads

  1. QNetworkAccessManager and phonon - can it be done?
    By serenti in forum Qt Programming
    Replies: 16
    Last Post: 2nd December 2011, 18:32
  2. Problems with QNetworkAccessManager
    By pfid in forum Qt Programming
    Replies: 1
    Last Post: 18th November 2009, 19:51
  3. QNetworkAccessManager or QHttp
    By mind_freak in forum Qt Programming
    Replies: 3
    Last Post: 29th September 2009, 20:24
  4. How to Login using QNetworkAccessManager?
    By cydside in forum Newbie
    Replies: 1
    Last Post: 31st August 2009, 21:41
  5. QNetworkAccessManager vs QHttp
    By jiveaxe in forum Newbie
    Replies: 3
    Last Post: 17th February 2009, 14:07

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.