Qt實現動畫的機制

來源:互聯網
上載者:User

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

Qt 實現動畫狀態機器執行個體是本文介紹的內容,Qt 4.6引入了動畫架構,擺脫了以往控制項只能安靜的呆在布局裡的時代,利用Qt提供的動畫架構,我們可以讓控制項跳起舞來,呵呵,很有趣啊… 在Qt 4.7中又引入了Qt quick技術,其中的QML語言也是專門來定製GUI動畫效果的,這是一種新的GUI動畫機制,我剛也接觸了些,文法類似CSS,實現預定義的動畫很方便,所見即所得 (WYSIWYG),路還得一步步走,先學習一下傳統的Qt動畫方式——狀態機器

這裡先直接給出例子(見圖):

 

 

 

#include<QApplication>#include<QStateMachine>#include<QPushButton>#include<QSignalTransition>#include<QPropertyAnimation>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();}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.