3

As a follow up for this post, I have created a Qt3D project which is a modified version of a Qt3D example. I'm running my project with Qt 5.13.1 on openSUSE Linux with GCC-7. On my project, I can use a time-delay between consecutive ray casts. I made some unexpected observations:


On main.cpp file, when I run this line, the ray casting gets stuck at some point and cannot continue:

ConsecutiveRayCaster *consecutiveRayCaster = new ConsecutiveRayCaster(scene, TimeDelayStatus::NoDelay);

However, when I run this line, even when time-delay is set to 0 msec, all the consecutive ray castings are done without any issue, :

ConsecutiveRayCaster *consecutiveRayCaster = new ConsecutiveRayCaster(scene, TimeDelayStatus::SomeDelay, 0 /* milliseconds */);

This switch makes the difference:

switch (m_timeDelayStatus) {
case NoDelay:
    rayCaster->trigger(origin, direction, length);
    break;
case SomeDelay:
    QTimer::singleShot(m_timeDelayBetweenRayCasts, [rayCaster, origin, direction, length](){ rayCaster->trigger(origin, direction, length); });
    break;
    }

I wonder why?

Megidd
  • 7,089
  • 6
  • 65
  • 142

1 Answers1

3

The reason of different behavior is in how the trigger() function is called. In the fist case (no delay), the execution waits until the trigger() function exits. However in the second case the function call is scheduled for the execution and QTimer::singleShot() function exits almost immediately without waiting for the trigger() function call returns.

vahancho
  • 20,808
  • 3
  • 47
  • 55
  • Up-voted =) but I don't understand why `NoDelay` leads to the ray-castings come to an immature stop. – Megidd Sep 23 '19 at 11:43
  • 1
    Because it waits until the `trigger()` is finished and returned. The key difference is the order of execution. – vahancho Sep 23 '19 at 11:50