PDA

View Full Version : Qtimer is not working....



k.qasempour
21st June 2012, 05:47
im writing some code to send request to google... so i need to place some delay between my request... i thought that the below code will work.. but nothing happen... do u know whats the problem?



// some code untill here

qDebug() << "Wait three Second...!";

QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(fake()));
timer->start(3000);

myHttp.request(myRequestHeader);

connect(&myHttp,SIGNAL(responseHeaderReceived(QHttpResponse Header)),
this,SLOT(showResponseHeader(QHttpResponseHeader)) );


the fake() is a fake slot just for calling when the timer timed out...
and doing sthing like this
int a;
a++;


whats the problem here?

Jonny174
21st June 2012, 05:55
Maybe better make list of requests and on timer timeout exec request from list

ChrisW67
21st June 2012, 06:43
whats the problem here?
QTimer does not introduce a delay, it schedules an event for later. You schedule a timer to fire in three seconds (or the first time the Qt event loop is reached after that time). You then schedule an HTTP request (using the obsolete and deprecated QHttp class) that will also happen later. Both events are scheduled immediately, and both events will be candidates to run when the event loop is next reached. Usually this will happen in less than three seconds so the HTTP request will happen (at least start) before the timer fires.

If the delay and request is a one-off then you could queue the HTTP request (using QNetworkRequest and QNetworkAccessManager) in the timer slot. If you have a series of requests to send a three second intervals then Jonny174 has a good suggestion.

Ali Reza
21st June 2012, 08:16
i think your timer section code have no error...