受bilibili客户端启发,同款效果动画
初始化
void MainWindow::initOverlayLabelAnimation()
{
// 在central widget上创建一个覆盖的label
overlayLabel = new QLabel(this);
// 加载logo图片
QPixmap pix = QPixmap(PNG_LINEX);
float scaled = 0.3;
overlayLabel->setPixmap(pix.scaled(pix.width() * scaled, pix.height() * scaled, Qt::KeepAspectRatio, Qt::SmoothTransformation));
overlayLabel->setAlignment(Qt::AlignCenter);
overlayLabel->setGeometry(0, 0, geometry().width(), geometry().height());
if (mInterfaceColor == QColor(Qt::black)) {
overlayLabel->setStyleSheet(QString("background-color: rgb(12,12,12);"));
}
else {
overlayLabel->setStyleSheet(QString("background-color: rgba(%1, %2, %3, %4);").arg(mInterfaceColor.red()).arg(mInterfaceColor.green()).arg(mInterfaceColor.blue()).arg(mInterfaceColor.alpha()));
}
// 设置透明度效果
QGraphicsOpacityEffect* opacityEffect = new QGraphicsOpacityEffect(overlayLabel);
overlayLabel->setGraphicsEffect(opacityEffect);
opacityEffect->setOpacity(1);
// 创建渐变动画
mOverlayLabelAnimation = new QPropertyAnimation(opacityEffect, "opacity");
mOverlayLabelAnimation->setDuration(600); // 持续时间ms
mOverlayLabelAnimation->setStartValue(1);
mOverlayLabelAnimation->setEndValue(0);
mOverlayLabelAnimation->setEasingCurve(QEasingCurve::InOutQuad);
// 设置动画结束的操作
connect(mOverlayLabelAnimation, &QPropertyAnimation::finished, overlayLabel, &QLabel::hide);
connect(mOverlayLabelAnimation, &QPropertyAnimation::finished, mOverlayLabelAnimation, &QPropertyAnimation::deleteLater);
connect(mOverlayLabelAnimation, &QPropertyAnimation::finished, this, &MainWindow::SignalOverlayLabelAnimationFinished);
}
启动
void MainWindow::showWindow()
{
this->show();
// 延时1秒后启动动画
QTimer::singleShot(1000, this, [=]() {
mOverlayLabelAnimation->start();
});
}
标签:动画,Qt,overlayLabel,启动,QPropertyAnimation,mInterfaceColor,mOverlayLabelAnimation
From: https://www.cnblogs.com/Yzi321/p/18511846