Solved dynamic timer firing different jobs
-
I am trying to create a dynamic timer that fires different jobs
my implementation is as follows. But in the fired job function i get always the fired job =2 ! I need someway to say take the current value of the counter and don't change it.for (int counter =0; counter <2; counter++) { timers[counter].singleShot(seconds*1000, [&](){ FireJob(counter); } void FireJob(int jobcmd) { qDebug() << jobcmd; }
any idea?
-
@SherifOmran
Hi
Hmm yes you are capturing the counter by reference ( like a pointer) so its
always the current (loop) value
Try with [=] to capture it as a copy. -
@mrjj thank you for your reply. Can you please show me how to use the code by copy, this is new to me.
-
Hi
its just = instead of & in the []for (int counter =0; counter <2; counter++) { timers[counter].singleShot(seconds*1000, [ = ](){ FireJob(counter); }
-
thank you , i got it