1

I need to check a conditional every second and then run function. Executing is asynchronous so it won't make any problems. Can I call function somewhere in loop? I would get time since program started and check if second passed. Where can i find main loop? Thanks

  • 1
    Take a look at `QTimer`. – Sebastian Lange Mar 17 '17 at 06:20
  • Be careful of the side-effects of the answer you accepted: https://wiki.qt.io/New_Signal_Slot_Syntax – The Quantum Physicist Mar 17 '17 at 07:13
  • Did you even try searching? http://stackoverflow.com/questions/19287550/how-to-call-a-function-after-every-15-seconds-using-qt http://stackoverflow.com/questions/42850485/qt-c-run-code-every-second – dtech Mar 17 '17 at 17:29
  • http://stackoverflow.com/questions/10667689/qtimer-to-execute-method-every-second http://stackoverflow.com/questions/41710255/how-to-call-a-function-every-second-which-updates-qt-widget http://stackoverflow.com/questions/23091664/qt-how-to-loop-a-method-every-second-c http://stackoverflow.com/questions/15556315/polling-a-variable-in-qt-once-every-second http://stackoverflow.com/questions/16786109/how-to-call-a-function-periodically-in-qt – dtech Mar 17 '17 at 17:30

1 Answers1

6

Using a QTimer can solve this:

QTimer* timer = new QTimer();
timer->setInterval(1000); //Time in milliseconds
//timer->setSingleShot(false); //Setting this to true makes the timer run only once
connect(timer, &QTimer::timeout, this, [=](){
    //Do your stuff in here, gets called every interval time
});
timer->start(); //Call start() AFTER connect
//Do not call start(0) since this will change interval

In addition (and since there has been an argument about the insecurity of lambda functions on target object lifetime) one can of course connect this to a slot in another object:

connect(timer, &QTimer::timeout, anotherObjectPtr, &AnotherObject::method);

Please be aware that this method should not have parameters, since the timeout signal is also defined without parameters.

Sebastian Lange
  • 3,879
  • 1
  • 19
  • 38
  • 1
    Woah! It's perfect! Haven't thought of such a simple solution. Using qt since yesterday and it's a beast! –  Mar 17 '17 at 06:53
  • 2
    Be very careful with this and don't recommend it to noobies! Using lambdas with `QObject::connect()` is not a problem-free toy! There's no automatic disconnection when the target object is destroyed. Refer to this: https://wiki.qt.io/New_Signal_Slot_Syntax – The Quantum Physicist Mar 17 '17 at 07:12
  • 2
    @TheQuantumPhysicist Since there is `this` as context it gets disconnectd just fine. – Sebastian Lange Mar 17 '17 at 07:19
  • One more question - what does [=] mean? Can I change it with functionName()? –  Mar 17 '17 at 07:36
  • @SebastianSzczepański I edited the answer to reflect the second possibility with connecting to another object, could also be `this` of course. – Sebastian Lange Mar 17 '17 at 07:50
  • @SebastianSzczepański `[=]` cannot be changed to a function name. It means that you capture locale variables by value. See http://en.cppreference.com/w/cpp/language/lambda#Lambda_capture – Benjamin T Mar 17 '17 at 08:29
  • How can I call function then? –  Mar 17 '17 at 08:45
  • @SebastianSzczepański Benjamin is just saying, you cannot replace the = with a function, but you can connect to a class method with the second option. If you insist on calling a non-connectable function you can just call this within the lambda functor. – Sebastian Lange Mar 17 '17 at 10:03
  • Thanks for explanation. I've Got it now –  Mar 17 '17 at 10:21