Smooth animations with QGraphicsScene

一世执手 提交于 2019-12-05 02:01:30

问题


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

Can you suggest me a way to do that please? Thanks a lot.

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, such as moving ecc) in my core part of the progamm. sceneItems is a map where I associate each actor to his pixmap. x, y are the positions in the abstract scene and x*SIZE is the position in the graphicscene. The positions are updating in the abstract scene and i represent them in the graphicscene.


回答1:


I animated my QGraphicItems with this approach:

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

Example from the 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();


来源:https://stackoverflow.com/questions/8533499/smooth-animations-with-qgraphicsscene

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