Qt实现动画的机制

http://mobile.51cto.com/symbian-271249.htm

Qt 实现动画状态机实例是本文介绍的内容,Qt 4.6引入了动画框架,摆脱了以往控件只能安静的呆在布局里的时代,利用Qt提供的动画框架,我们可以让控件跳起舞来,呵呵,很有趣啊… 在Qt 4.7中又引入了Qt quick技术,其中的QML语言也是专门来定制GUI动画效果的,这是一种新的GUI动画机制,我刚也接触了些,语法类似CSS,实现预定义的动画很方便,所见即所得,路还得一步步走,先学习一下传统的Qt动画方式——状态机

这里先直接给出例子(见图):

 

 

Qt实现动画的机制_第1张图片

 

#include
#include
#include
#include
#include

int main(int argc, char * argv[])
{
    QApplication app(argc, argv);

    QWidget * m_widget = new QWidget;
    m_widget->resize(240, 320);
    QPushButton * m_button = new QPushButton("hello", m_widget);
    //属性机 两个属性
    QStateMachine * m_machine = new QStateMachine;
    QState * m_state1 = new QState(m_machine);
    m_state1->assignProperty(m_button, "geometry", QRect(0, 0, 80, 30));
    m_state1->assignProperty(m_button, "text", "hello");
    m_machine->setInitialState(m_state1);
    QState * m_state2 = new QState(m_machine);
    m_state2->assignProperty(m_button, "geometry", QRect(m_widget->width() - 30,
                                                        m_widget->height() - 80,
                                                        30, 80));
    QFont font = QFont("Airl", 12);
    m_state2->assignProperty(m_button, "font", font);
    m_state2->assignProperty(m_button, "text", "welcome");

    QPropertyAnimation * m_animation = new QPropertyAnimation(m_button, "geometry");
    m_animation->setDuration(2000);
    m_animation->setEasingCurve(QEasingCurve::OutBounce);
    //给属性添加一个转变
    QSignalTransition * transition1 = m_state1->addTransition(m_button, SIGNAL(clicked()), m_state2);
    //添加一个动画
    transition1->addAnimation(m_animation);
    QSignalTransition * transition2 = m_state2->addTransition(m_button, SIGNAL(clicked()), m_state1);
    transition2->addAnimation(m_animation);

    //开始到初始状态
    m_machine->start();
    m_widget->show();

    return app.exec();
}


 

你可能感兴趣的:(QT)