使用keypressevent为qt中的项目设置动画 [英] use of keypressevent for animating a item in qt
问题描述
在这个程序中,我构建了一个QPropertyanimation。并添加了我的item和pos()属性。我重写KeyPressEvent.and使用由j,f,z项组成的键前进,返回并跳转。根据重力,物品跳跃应该下降。为此目的我调用功能。但项目只跳一次,不要下降。我也有另一个问题。当第一次按j和f(前进,后退)项目动画理想,但下次项目前进并返回所有场景。我的意思是它应该动画例如40像素,但它动画800像素。
in this program I have built a QPropertyanimation .and add to it my item and pos () property. I override KeyPressEvent.and with using of keys consist of "j,f,z" item go forward ,go back and jump. According gravity when item jump should fall.for this purpose i call down function .but item just once jump and don't fall .and I also have another problem .when the first press "j" and "f" (forward,back) item animate desirably but for next times item go forward and go back all of scene. I mean It should animated for example 40 pixel but It animated 800 pixel.
<small></small>class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
QPoint start;
QPoint end;
~MainWindow();
private:
QGraphicsView* view;
QGraphicsScene* scene;
void keyPressEvent(QKeyEvent* k);
MyQgraphicsObject* m;
QPropertyAnimation* pr;
QElapsedTimer* timer;
int f;
int u;
int b;
void forward();
void up();
void back();
void down();
};
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
view=new QGraphicsView;
scene=new QGraphicsScene;
m=new MyQgraphicsObject;
pr=new QPropertyAnimation(m,"pos");
view->setScene(scene);
view->resize(800,800);
view->setFixedSize(800,800);
setCentralWidget(view);
scene->addItem(m);
start= QPoint(0,0);
f=30;
u=-30;
b=-30;
}
void MainWindow::keyPressEvent(QKeyEvent *k)
{
switch (k->key())
{
case Qt::Key_J:{
forward();
break;
}
case Qt::Key_Z:
{
up();
down();
break;
}
case Qt::Key_F:
{
back();
break;
}
default:
break;
}
}
void MainWindow::forward()
{
end.setX(f);
pr->setEndValue(end);
pr->setDuration(1000);
pr->setEasingCurve(QEasingCurve::Linear);
pr->start();
f+=40;
}
void MainWindow::up()
{
end.setY(u);
pr->setEndValue(end);
pr->setDuration(1000);
pr->setEasingCurve(QEasingCurve::Linear);
pr->start();
u-=30;
pr->pause();
}
void MainWindow::back()
{
end.setX(b);
pr->setEndValue(end);
pr->setDuration(1000);
pr->setEasingCurve(QEasingCurve::Linear);
pr->start();
b-=40;
}
void MainWindow::down()
{
u+=30;
end.setY(u);
pr->setEndValue(end);
pr->setDuration(1000);
pr->setEasingCurve(QEasingCurve::Linear);
pr->start();
}
推荐答案
这篇关于使用keypressevent为qt中的项目设置动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!