zoukankan      html  css  js  c++  java
  • Qt之QPauseAnimation

    简述

    QPauseAnimation类为QSequentialAnimationGroup提供了一个暂停。

    如果你想为QSequentialAnimationGroup动画之间添加延迟,可以插入一个QPauseAnimation。它没有任何动画,但当在指定的毫秒数之内开始运行时不会结束。可以通过构造函数指定暂停的时间,也可以通过setDuration()设置。

    没必要自己建立一个QPauseAnimation,QSequentialAnimationGroup提供了便利的函数addPause()和insertPause(),这些函数可以简单地暂停应该持续的毫秒数。

    公共函数

    • void setDuration(int msecs)
      设置暂停的毫秒数。

      暂停持续的时间不应该是负的,默认的时间是250毫秒。

    示例

    下面,我们通过QSequentialAnimationGroup来构建一个串行动画组,并添加属性动画QPropertyAnimation和暂停动画QPauseAnimation,这里也可以使用addAnimation()添加其它动画/动画组,就不予演示了。

    效果

    这里写图片描述

    源码

    MainWindow::MainWindow(QWidget *parent)
        : CustomWindow(parent)
    {
        ...
    
        QPushButton *pStartButton = new QPushButton(this);
        pStartButton->setText(QString::fromLocal8Bit("开始动画"));
    
        QList<QLabel *> list;
        QStringList strList;
        strList << QString::fromLocal8Bit("一去丶二三里") << QString::fromLocal8Bit("青春不老,奋斗不止");
    
        for (int i = 0; i < strList.count(); ++i)
        {
            QLabel *pLabel = new QLabel(this);
            pLabel->setText(strList.at(i));
            pLabel->setAlignment(Qt::AlignCenter);
            list.append(pLabel);
    
            pLabel->setObjectName("highlightLabel");
        }
    
        // 动画一
        QPropertyAnimation *pAnimation1 = new QPropertyAnimation(list.at(0), "geometry");
        pAnimation1->setDuration(1000);
        pAnimation1->setStartValue(QRect(0, 0, 100, 30));
        pAnimation1->setEndValue(QRect(120, 130, 100, 30));
        pAnimation1->setEasingCurve(QEasingCurve::OutBounce);
    
        // 暂停 - 特殊的动画
        QPauseAnimation *pPauseAnimation = new QPauseAnimation(this);
        pPauseAnimation->setDuration(1000);
    
        // 动画二
        QPropertyAnimation *pAnimation2 = new QPropertyAnimation(list.at(1), "geometry");
        pAnimation2->setDuration(1000);
        pAnimation2->setStartValue(QRect(120, 130, 120, 30));
        pAnimation2->setEndValue(QRect(170, 0, 120, 30));
        pAnimation2->setEasingCurve(QEasingCurve::OutInCirc);
    
        m_pGroup = new QSequentialAnimationGroup(this);
    
        // 添加动画
        m_pGroup->addAnimation(pAnimation1);
        m_pGroup->addAnimation(pPauseAnimation);
        m_pGroup->addAnimation(pAnimation2);
    
        // 循环2次
        m_pGroup->setLoopCount(2);
    
        // 连接信号槽
        connect(pStartButton, SIGNAL(clicked(bool)), this, SLOT(startAnimation()));
        connect(m_pGroup, SIGNAL(currentAnimationChanged(QAbstractAnimation*)), this, SLOT(onCurrentAnimationChanged(QAbstractAnimation*)));
    
        ...
    }

    槽函数如下:

    // 开始动画
    void MainWindow::startAnimation()
    {
        m_pGroup->start();
    }
    
    // 动画切换时会调用
    void MainWindow::onCurrentAnimationChanged(QAbstractAnimation *current)
    {
        QPropertyAnimation *pAnimation = dynamic_cast<QPropertyAnimation *>(current);
        if (pAnimation == NULL)
            return;
    
        QLabel *pLabel = dynamic_cast<QLabel *>(pAnimation->targetObject());
        if (pLabel != NULL)
            pLabel->setText(pLabel->text() + ".");
    }

    这里需要注意,暂停也是一个动画(比较特殊而已),所以我们需要用dynamic_cast来转换,并判断是否为NULL(否则会crash)。

    更多参考

  • 相关阅读:
    40个Java集合面试问题和答案
    jsp九大内置对象:request、response、session、application、out、page、pageContext、config、exception
    JAVA多线程和并发基础面试问答
    线程的五大状态
    人物与动物快速抠图技巧
    实时监听数据库变化
    inter jion与WITH (NOLOCK) 排他锁(脏读)
    Win定时任务未正常运行的bug(win Server2019)
    HttpWebRequest与HttpWebResponse的关系
    A5M2 sql自动换行快捷键
  • 原文地址:https://www.cnblogs.com/itrena/p/5938265.html
Copyright © 2011-2022 走看看