I started using PyQt4 a while ago and now I'm trying to create a small animation using QGraphicsItemAnimation. I never did that with PyQt4 before, I always used pygame. There have to be different layers, qpixmaps can be animated on these layers but these layers can also be drawn onto other layers and then the whole layer can be animated with all qpixmaps draw onto it. I saw somewhere that with QGraphicsItemAnimation I can animate the QGraphicsItems without having to redraw them to the layer they're on like I have to in pygame. It's a little bit hard to explain. I hope you understand what I mean. I'll add some pictures so it's more clear.

For example if I have these two shapes:

enter image description here

I can for example move only the qgraphicsitem of the rectangle:

enter image description here

But I can also move the whole layer, the ellipse and the rectangle are drawn onto the same layer, so if I move the whole layer after moving the rectangle, it'll look like this:

enter image description here

To achieve this I somehow have to create these "layers" and I thought of doing this by using the QGraphicsPixmapItems as layers and then draw other QGraphicsPixmapItems onto them, either as layers or as single pictures. Something like when you add a QGraphicsItem to a QGraphicsScene, but QGraphicsScenes can't be animated with QGraphicsItemAnimation as far as I know. I looked through the documentation but I could only find QGraphicsPixmapItem.setPixmap and that replaces the old pixmap instead of adding another one on top of it. So how can I add a QGraphicsPixmapItem to another QGraphicsPixmapItem? I'm using Python 2.7.3 if that matters.

有帮助吗?

解决方案

To create a "layer", add the items to a QGraphicsItemGroup.

The QGraphicsItemGroup class inherits QGraphicsItem, so it can be set as animation item, and all its child-items will be animated as a unit. At the same time, the child-items can still be animated separately.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top