Qt动画控制效果

    首先需要创建一个状态机对被控制对象的状态, 动画效果, 信号触发等进行统一管理.


    //创建状态机
    QStateMachine *pStateMachine = new QStateMachine(this);


然后根据动画需求设置状态, 比如希望一个按钮从左边移动到右边, 并且移动过程中放大到2倍.


    //设置起始状态
    QState *pStartState = new QState(pStateMachine);
    //绑定属性
    pStartState->assignProperty(pButton, "geometry",
            QRect(10,(this->height()-pButton->height())/2,pButton->width(),pButton->height()));
    //设置为起始状态
    pStateMachine->setInitialState(pStartState);

    //设置结束状态
    QState *pEndState = new QState(pStateMachine);
    //绑定属性
    pEndState->assignProperty(pButton, "geometry",
            QRect(this->width()-pButton->width()*2-10,
            (this->height()-pButton->height()*2)/2,
            pButton->width()*2,pButton->height()*2));

绑定状态属性的时候描述好属性内容, 比如坐标和宽高.

然后设置动画效果


    //设置动画效果
    QPropertyAnimation *pPropertyAnimation = new QPropertyAnimation(pButton, "geometry");
    pPropertyAnimation->setDuration(2000);
    pPropertyAnimation->setEasingCurve(QEasingCurve::OutBounce);

接下来绑定动画信号槽


    //绑定动画信号槽
    QSignalTransition *pTransitionStart2End = pStartState->addTransition(pButton, SIGNAL(clicked()), pEndState);
    pTransitionStart2End->addAnimation(pPropertyAnimation);

    QSignalTransition *pTransitionEnd2Start = pEndState->addTransition(pButton, SIGNAL(clicked()), pStartState);
    pTransitionEnd2Start->addAnimation(pPropertyAnimation);

最后就可以启动状态机了


    //开启状态机
    pStateMachine->start();


Qt动画控制效果_第1张图片


需要完整代码请访问 QtWidgetsExamples


你可能感兴趣的:(界面开发)