tags:

views:

224

answers:

2

If I wanted to, using Qt, simply have some circles move around in a white box, or a graphic, what would be the best method of this?

Would I need to draw white/the graphic behind where the circle moved from every time? Is there a simple way of accomplishing this in Qt?

A: 

Subclass a QWidget. Start a timerEvent() to animate the positions of the circles, then call update() at the end to schedule a repaint of the widget. Override the widget's paintEvent() - in there you draw your background and circles, using a QPainter object. The Qt Assistant has examples of how to use QPainter.

Qt also has a new animation framework that may facilitate something like this, but I have not looked into it.

JimDaniel
So we do have to redraw the background every time?
adrien
Yeah, pretty sure you do, but if you needed the efficiency there's probably some work-around. I do a lot of custom drawing in paintEvent and it's always pretty quick.
JimDaniel
With Qt Graphics Framework, you don't need to handle these kind of low level operations. See my reply below.
Mason Chang
Cool, thanks Mason. Good to know. I haven't gotten into that part of the framework.
JimDaniel
+3  A: 
  1. Create QGraphicsView object to your widget and added a QGraphicsScene to view.
  2. Add a QGraphicsEllipseItem to scene
  3. Use QPropertyAnimation to change the "pos" property of the ellipse item.

If you need more advanced features, you can build your own animation class on QPropertyAnimation.

enjoy it:)

Update: You can read Qt's Next Generation UI for more information.

Mason Chang