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

 

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