Qt 动画应用

作者: paresly | 来源:发表于2019-11-10 18:19 被阅读0次

    Qt的动画机制,能用在QWidget、QGraphicWidget、以及在QML中也有部分应用的模型,这里只是先举两个小的例子,目前也没有太深入研究。
    以QPropertyAnimation为例,在Widget类中有另一个m_subWidget子类,点击按钮后,会以动画的形式显示出m_subWidget界面。

    //定义槽连接
    connect(btn,&QPushButton::clicked,this,[&](){
            showAnimation();
    });
    
    void Widget::showAnimation()
    {
        m_subWidget->show();
    
        QPoint pStart,pEnd;
        pStart = mapToGlobal(QPoint(0-width(),0));
        pEnd = mapToGlobal(QPoint(0,0));
    
        animation = new QPropertyAnimation(m_subWidget, "geometry");
        animation->setDuration(1000); //设置动画时间
        animation->setStartValue(QRect(pStart, QSize(width(),height())));//设置动画开始位置
        animation->setEndValue(QRect(pEnd, QSize(width(),height())));//设置动画结束位置
    
        animation->setEasingCurve(QEasingCurve::SineCurve);
        animation->start();
    }
    

    根据帮助文档的说明,QSequentialAnimationGroup可以将多个个动画关联起来,而QParallelAnimationGroup类则会同时展示多个动画。

      QPushButton button("Animated Button");
      button.show();
    
      QPropertyAnimation anim1(&button, "geometry");
      anim1.setDuration(3000);
      anim1.setStartValue(QRect(0, 0, 100, 30));
      anim1.setEndValue(QRect(500, 500, 100, 30));
    
      QPropertyAnimation anim2(&button, "geometry");
      anim2.setDuration(3000);
      anim2.setStartValue(QRect(500, 500, 100, 30));
      anim2.setEndValue(QRect(1000, 500, 100, 30));
    
      QSequentialAnimationGroup group;
    
      group.addAnimation(&anim1);
      group.addAnimation(&anim2);
    
      group.start();
    

    相关文章

      网友评论

        本文标题:Qt 动画应用

        本文链接:https://www.haomeiwen.com/subject/xgpabctx.html