Results 1 to 18 of 18

Thread: QNetworkAccessManager within QThread

  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,
    _

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

    Default Re: QNetworkAccessManager within QThread

    The is the same in the constructor , i just replace it

    this is the code for the loop :

    Qt Code:
    1. void Manager::run()
    2. {
    3. while(true)
    4. {
    5. m_url.setUrl("http://time.jsontest.com/");
    6. m_request.setUrl(m_url);
    7. connect(&m_networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onResult(QNetworkReply*)));*/
    8. m_networkManager.get(m_request);
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    i need the thread to send many requests and get many replies.

  13. #13
    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
    The is the same in the constructor , i just replace it

    this is the code for the loop :

    Qt Code:
    1. void Manager::run()
    2. {
    3. while(true)
    4. {
    5. m_url.setUrl("http://time.jsontest.com/");
    6. m_request.setUrl(m_url);
    7. connect(&m_networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onResult(QNetworkReply*)));*/
    8. m_networkManager.get(m_request);
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 
    And you disconnect where? Or do you want to multiply the number of slot invocation on each loop?
    The network manager is still owned by the main thread (the thread that created it), so is the receiver object (this).
    All processing other than the loop happens in the creating thread.

    Are you sure that is what you want?

    Quote Originally Posted by hassinoss View Post
    i need the thread to send many requests and get many replies.
    No, I mean, what do you need the thread for. Sending and receiving multiple requests does not need threads, so you must have some other reason do use one, no?

    Cheers,
    _

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

    Default Re: QNetworkAccessManager within QThread

    What i want exactly is recover data from the web service in real time ( every seconde for example) that's why i used the threads, is there an other way to do this i can use it ?

  15. #15
    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

    Have you tested that it does not work with just the main thread?

    Cheers,
    _

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

    Default Re: QNetworkAccessManager within QThread

    What do you mean by the main thread ?

  17. #17
    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

    The thread executing QApplication::exec()

    Cheers,
    _

  18. #18
    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 didn't get realy what you mean but Wysoka answered me in the other Thread http://www.qtcentre.org/threads/6008...iples-requests

    Thanx for your help

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.