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:
Code:
void itemthread::run() {
int i;
for(i=1; i < 10; i++) {
out << i;
QNetworkAccessManager *manager = new QNetworkAccessManager();
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(gotData(QNetworkReply *)));
QNetworkRequest request;
request.setUrl(url);
request.setRawHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
manager->get(request);
}
}
"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
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.
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.
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 ?
Re: QNetworkAccessManager within QThread
Quote:
Originally Posted by
hassinoss
someone know why ?
Yes, easy, you have done something wrong.
Cheers,
_
Re: QNetworkAccessManager within QThread
This is my code
Code:
{
m_url.setUrl("http://time.jsontest.com/");
m_request.setUrl(m_url);
connect(&m_networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onResult(QNetworkReply*)));*/
m_networkManager.get(m_request);
}
void Manager::onResult(QNetworkReply* reply)
{
if(reply)
{
......;
}
}
void Manager::run()
{
while(true)
{
}
}
whene i put the code in the constructor in the run method it doesn't work , there is any problem with doing this
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,
_
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.
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,
_
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.
Re: QNetworkAccessManager within QThread
Your posting is missing the code for the loop. Again.
What do you need the thread for anyway?
Cheers,
_
Re: QNetworkAccessManager within QThread
The is the same in the constructor , i just replace it
this is the code for the loop :
Code:
void Manager::run()
{
while(true)
{
m_url.setUrl("http://time.jsontest.com/");
m_request.setUrl(m_url);
connect(&m_networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onResult(QNetworkReply*)));*/
m_networkManager.get(m_request);
}
}
i need the thread to send many requests and get many replies.
Re: QNetworkAccessManager within QThread
Quote:
Originally Posted by
hassinoss
The is the same in the constructor , i just replace it
this is the code for the loop :
Code:
void Manager::run()
{
while(true)
{
m_url.setUrl("http://time.jsontest.com/");
m_request.setUrl(m_url);
connect(&m_networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onResult(QNetworkReply*)));*/
m_networkManager.get(m_request);
}
}
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
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,
_
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 ?
Re: QNetworkAccessManager within QThread
Have you tested that it does not work with just the main thread?
Cheers,
_
Re: QNetworkAccessManager within QThread
What do you mean by the main thread ?
Re: QNetworkAccessManager within QThread
The thread executing QApplication::exec()
Cheers,
_
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