[Moved] Timer used to repaint (advance) delayed by event flood processing
-
I have a scene with a ball and a bat. The ball is advance using a timer (frequency about 30 / sec). The bat is moved using the mouse events (or touch events).
The problem is that when I move the bat continuously, the ball is slowing down (stopping in between).I printed out some traces and the issue seems to be related to the fact that the timer is not triggered because of the mouse (touch) events flood. In the worst cases the timer is called once or twice a second.
@
const int TimerFreq = 1000/33;
QTimer timer;
QObject::connect(&timer, SIGNAL(timeout()), &scene, SLOT(advance()));
timer.start(TimerFreq);@How can I rework this in order to get the ball moving smoothly, not depending on the mouse/touch events.
-
You can move your ball work to another thread with its own event loop.
-
As Denis suggested "QThread":http://doc.qt.nokia.com/4.7/qthread.html is the best place to start.
-
How many events are we talking about? Which device is it, that is generating so many events (and they're not somehow filtered)? Which platform?
[quote]
You can move your ball work to another thread with its own event loop.
[/quote]Nonsense. QGraphics* classes are not reentrant, thus they're usable only from the main thread, whose event loop seems to be flooded by touch events.
-
peppe, who is saying about moving QGraphics* to another thread? We can move all calculations of new ball position to another thread and left only painting at main thread. So calculations will be made every 1/30 sec and will be painted asap.
-
peppe, I think if all work about ball and about bat moving will be moved away from main thread (i.e. we will have only painting and catching events, all calculations will be done in background) then it will help.
-
How can it help if the main thread's event loop is flooded by those mouse/touch events? Even supposing that you can pick the data from the main thread, do your math in 1 millionth of a second, and schedule an update() for your item, the QPaintEvent for the QGV will be sitting in the main thread's event queue and will actually be delivered after 1-2s (according to OP).
-
peppe, according to OP timer events are invoked once in 1-2 seconds.
-
[quote author="Denis Kormalev" date="1293486729"]peppe, according to OP timer events are invoked once in 1-2 seconds.[/quote]
Instead of the desidered 30 updates per second.
[quote]“background” thread does all the ball calculations and sends a signal to the main thread which in turn does the graphics update.[/quote]
Again, HOW does the main thread update the view if the main event loop is flooded by input events?
-
Volker, exactly what I am saying :) Also I think not only ball, but bat calculations can be moved to background thread too.
-
[quote author="Volker" date="1293486907"]Main thread does all the GUI stuff.
"background" thread does all the ball calculations and sends a signal to the main thread which in turn does the graphics update.
[/quote]Calculating the next ball position is just x=x+1. The problem is that the timer's timeout() is not triggered as expected (30 times a second), because the main thread is busy handling the mouse/touch events.
-
peppe, at least OP can try it and maybe it will solve the problem.
-
peppe, forum is "try this thing" at 85% of questions.
dflorin, try to count how often paintEvent is invoked. If it invoked in good frequency then using background thread will help. -
peppe, I don't know forum where you can ask any question you have stucked with and receive answer correctly and quickly. And this discussion is offtopic. If you have something to add about subj of topic then add.
-
Back to the subject. I made some measurements:
When not moving the bat:
- Bat::paint() called 30 times (how many times the bat is repainted as a result of setPos() called from the event() method)
- Ball::advance() called 30 times (this is called by the timer and should be 30 calls instead) - this will trigger paint()
NOTE: the Bat is also repainted because I use:
view.setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
This should not have any impact on my problem.When moving the bat as fast as I can using touch events:
- Scene::event() called 47 times (this is how fast I move the bat by touching the screen)
- Bat::paint() called 50 times (how many times the bat is repainted as a result of setPos() called from the event() method and as a result of paint() done for the ball)
- Ball::advance() called 13 times (this is called by the timer and should be 30 calls instead) - this is my problem: the ball has slowed down at less than half speed.
And one more thing: this is noticeable on a Symbian device. Running it in the simulator on Windows and using mouse events instead of touch events does not slow down the ball. Should the post be moved to Mobile and Embedded?