天天看點

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();
}
           

繼續閱讀