QT屬性動畫

QT屬性動畫,可以比較方便的實現窗口,按鈕之類的控件大小,位置,顏色的緩慢變化。 比較適合軟鍵盤的彈出收回,窗口側邊欄的彈出隱藏之類的場景 

下邊是操作流程(主要寫出了函數的使用過程, 實際使用的時候最好不要每次都設置一堆屬性,最好是在初始化的時候就設置好,然後在按鈕事件的時候只啓動動畫。 包括這裏用了全局變量,都是爲了體現內容,因此不符合編程規範)

QPropertyAnimation animation;
QPropertyAnimation animation2;
void Widget::on_pushButton_2_clicked()
{
    if(animation.currentValue().toRect().width() < 200 && animation.currentValue().toRect().width() > 100)
        return;

    animation.setTargetObject( ui->pushButton );    //這個動畫操作的對象是按鈕
    animation.setPropertyName("geometry");          //操作按鈕的 位置大小屬性

    animation.setDuration( 200 ); //200毫秒時間變化完成
    animation.setStartValue( QRect(200, 20, 100, 100) ); 
    animation.setEndValue( QRect(200, 20, 200, 200) );
    animation.start();

}

void Widget::on_pushButton_3_clicked()
{
    if(animation2.currentValue().toRect().width() < 200 && animation2.currentValue().toRect().width() > 100)
        return;

    animation2.setTargetObject( ui->pushButton );
    animation2.setPropertyName("geometry");

    animation2.setDuration( 200 ); 
    animation2.setStartValue( QRect(200, 20, 200, 200) );
    animation2.setEndValue( QRect(200, 20, 100, 100) );
    animation2.start();
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章