Smooth animation with QGraphicsScene

I hope my question will not always be the same question. I have a QGraphicsScene. Its elements are some QGraphicsPixmaps. I move them with a timer that does SetX +10 every second. I set +10 because the window is large 100. With this solution, my animations are not smooth. I thought I could make them smoother by setting +10 +1 + 10 instead. But that didn't work.

Can you suggest me a way to do this, please? Thank you very much.

void GameGui::updateScene() { for (int y = 0; y < game->getHeight(); ++y) { for (int x = 0; x < game->getWidth(); ++x) { Actor* a = game->get(y, x); if (sceneItems[a] != 0) { sceneItems[a]->setX(x*SIZE); sceneItems[a]->setY(y*SIZE); } } } } 

Each actor is my game character (in his functions, for example, moving ecc) in my main part of the program. sceneItems is a map in which I associate each actor with his pixmap. x, y are the positions in the abstract scene, and x * SIZE is the position in the graph. Positions are updated in an abstract scene, and I represent them in a graph.

+4
source share
1 answer

I animated my QGraphicItems using this approach:

"The QGraphicsItemAnimation class provides simple animation support for QGraphicsItem." http://doc.qt.io/qt-4.8/qgraphicsitemanimation.html

Example from a linked site:

  QGraphicsItem *ball = new QGraphicsEllipseItem(0, 0, 20, 20); QTimeLine *timer = new QTimeLine(5000); timer->setFrameRange(0, 100); QGraphicsItemAnimation *animation = new QGraphicsItemAnimation; animation->setItem(ball); animation->setTimeLine(timer); for (int i = 0; i < 200; ++i) animation->setPosAt(i / 200.0, QPointF(i, i)); QGraphicsScene *scene = new QGraphicsScene(); scene->setSceneRect(0, 0, 250, 250); scene->addItem(ball); QGraphicsView *view = new QGraphicsView(scene); view->show(); timer->start(); 
+7
source

Source: https://habr.com/ru/post/1386689/


All Articles