Qt: QGraphicsScene not updating when I would expect it to

不想你离开。 提交于 2019-12-05 21:39:24

This is the kind of behaviour that is often seen in event driven GUI frameworks when one wants to do continuous animation. I'm going to guess that eye::playSequence is called from a button click or maybe from some point during the application startup code? In any case, here is what's going on.

Qt uses the main application thread to drive an event loop. The event loop is something like this:

while(app_running)
{
  if(EventPending)
    ProcessNextEvent();
}

The problem you are seeing is that updates to the screen are done during a paint event. If you are running some code during a mouse click event or any other event, then all the drawing you are doing is queued up and will be drawn to the screen on the next paint event. Sometimes it takes awhile for this to sink in.

The best way to address this is to change your approach a bit. One way is to throw away your while loop and setup a QTimer set to fire every 5 seconds. In the timer slot you can draw one slide. When the timer fires again, draw the next slide, etc.

If you want a more direct and less elegant quick fix, try calling qapp->processEvents() right after your call to presentSlide(sequenceNum, i). This (most of the time) will force the application to clear out any queued up events which should include paint events.

I should also mention that eye::presentSlide() is merely adding new scene objects to the scene on each iteration covering the ones that were added during the last call. Think of the scene as a fridge door and when you call scene().addXXX you are throwing more fridge magnets on the door :)

You need to let the application's event loop run in order to keep the user interface updated. The easiest way to do this from your code is to call QApplication::processEvents() in your inner while loop.

Many people would consider your while loop to be inefficient - after all, you are just waiting for a given period of time to elapse. You may want to think about restructuring your code to use a timer instead.

You could create a QTimer object in your eye class's constructor, set a time-out of 5 seconds, and connect its timeout() signal to a slot in your eye class which updates the index into the set of slides and calls presentSlide(). You would start the timer in the playSequence() function.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!