Qt动画:窗口从8个方向随机弹出的动画效果

void setWidgetAnimation(QWidget * w,bool isShow = true)
{
    auto screenList = qApp->screens();
    int screenCount = screenList.count();
    QPoint pos = QCursor::pos();
    for (int i = 0; i < screenCount; ++i)
    {
        auto screen = screenList.at(i);
        if(screen->geometry().contains(pos))
        {
            auto rect = screen->geometry();

            auto animation = new QPropertyAnimation(w, "geometry");
            animation->setDuration(400);

            int index = QRandomGenerator::global()->bounded(8);
            QRect startRect;
            switch (index)
            {
                case 0:startRect = QRect(50,50,50,50);break;
                case 1:startRect = QRect(50,rect.height() - 100,50,50);break;
                case 2:startRect = QRect(rect.width() - 100,50,50,50);break;
                case 3:startRect = QRect(rect.width() - 100,rect.height() - 100,50,50);break;
                case 4:startRect = QRect((rect.width() - w->width()) / 2,50,50,50);break;
                case 5:startRect = QRect((rect.width() - w->width()) / 2,rect.height() - 100,50,50);break;
                case 6:startRect = QRect(50,(rect.height() - w->height()) / 2,50,50);break;
                case 7:startRect = QRect(rect.width() - 100,(rect.height() - w->height()) / 2,50,50);break;
            }
            if(isShow)
            {
                w->setWindowOpacity(0);
                w->show();
                w->setGeometry(QRect((rect.width() - w->width()) / 2,
                                     (rect.height() - w->height()) / 2,
                                      w->width(),w->height()));
            }

            if(isShow)
            {
                animation->setStartValue(startRect);
                animation->setEndValue(w->geometry());
            }
            else
            {
                animation->setStartValue(w->geometry());
                animation->setEndValue(startRect);
            }
            auto easingCurveValue = QRandomGenerator::global()->bounded(47);
            if(easingCurveValue == 43 || easingCurveValue == 44)//这两个效果不好
            {
                easingCurveValue = 42;
            }

            auto easingCurve = QEasingCurve(static_cast<QEasingCurve::Type>(easingCurveValue));
            animation->setEasingCurve(easingCurve);

            auto animation2 = new QPropertyAnimation(w, "windowOpacity");
            animation2->setDuration(400);
            if(isShow)
            {
                animation2->setStartValue(0);
                animation2->setEndValue(1);
            }
            else
            {
                animation2->setStartValue(1);
                animation2->setEndValue(0);
            }
            animation2->setEasingCurve(easingCurve);

            QParallelAnimationGroup *group = new QParallelAnimationGroup;
            group->addAnimation(animation);
            group->addAnimation(animation2);
            if(!isShow)
            {
                w->connect(group,&QParallelAnimationGroup::finished,[w]
                {
                    w->close();
                });
            }
            group->start(QAbstractAnimation::DeleteWhenStopped);
            break;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值