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;
}
}
}
Qt动画:窗口从8个方向随机弹出的动画效果
于 2022-06-16 12:28:19 首次发布