QT SplashScreen带有褪色徽标未运行动画

Qt SplashScreen w/ Fading Logo Not Running Animation

本文关键字:运行 动画 褪色 SplashScreen QT      更新时间:2023-10-16

我正在为我的应用创建一个加载屏幕,我想实现两个Qlabels(背景和覆盖),其中覆盖只是背景的发光轮廓。我希望这个叠加层以不透明(0.0-1.0)淡入淡出(0.0-1.0),我将Qpropertyanimation与标签的WindousOpacity属性一起使用,但对此无济于事。这是我的完整源代码。

main.cpp:

#include "mainwindow.h"
#include "imagefade.h"
#include <QApplication>
#include <QSplashScreen>
#include <QTimer>
#include <QHBoxLayout>
#include <QLabel>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QSplashScreen *splashScreen = new QSplashScreen();
    splashScreen->resize(500, 500);
    QPixmap bkgdImage(":/Files/Images/Launch/launch.png");
    QPixmap ovlyImage(":/Files/Images/Launch/launch-glow.png");
    ImageFade *imageLabel= new ImageFade(splashScreen);
    imageLabel->setBackgroundImage(bkgdImage);
    imageLabel->setOverlayImage(ovlyImage);
    imageLabel->startAnimation(5000);
    splashScreen->show();
    MainWindow w;
    QTimer::singleShot(2500, splashScreen, SLOT(close()));
    QTimer::singleShot(2500, &w, SLOT(show()));
    //w.show();
    return a.exec();
}

imagefade.cpp:

#include "imagefade.h"
#include <QDebug>
ImageFade::ImageFade(QWidget *parent) : QWidget(parent)
{
    bkgdLabel = new QLabel();
    ovlyLabel = new QLabel();
    bkgdLabel->setGeometry(QRect(QPoint(0, 0), QSize(parent->size())));
    ovlyLabel->setGeometry(QRect(QPoint(0, 0), QSize(parent->size())));
    fadeAnimation = new QPropertyAnimation(ovlyLabel, "windowOpacity");
    fadeAnimation->setLoopCount(5);
    fadeAnimation->setStartValue(1.0);
    fadeAnimation->setEndValue(0.0);
    fadeAnimation->setEasingCurve(QEasingCurve::OutQuad);
    connect(fadeAnimation, SIGNAL(stateChanged(QAbstractAnimation::State,QAbstractAnimation::State)), this, SLOT(stateChanged(QAbstractAnimation::State,QAbstractAnimation::State)));
}
ImageFade::~ImageFade()
{
    //fadeAnimation->stop();
}
void ImageFade::setBackgroundImage(QPixmap bkgdImg)
{
    this->bkgdImg = bkgdImg;
    bkgdLabel->setPixmap(bkgdImg.scaled(size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
}
void ImageFade::setOverlayImage(QPixmap ovlyImg)
{
    this->ovlyImg = ovlyImg;
    ovlyLabel->setPixmap(ovlyImg.scaled(size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
}
void ImageFade::startAnimation(int fadeDelay)
{
    fadeAnimation->setDuration(fadeDelay);
    fadeAnimation->start(QAbstractAnimation::DeleteWhenStopped);
}
void ImageFade::stop()
{
    fadeAnimation->stop();
}
void ImageFade::stateChanged(QAbstractAnimation::State state1, QAbstractAnimation::State state2)
{
    qDebug() << state1 << state2;
}

imagefade.h:

#ifndef IMAGEFADE_H
#define IMAGEFADE_H
#include <QWidget>
#include <QLabel>
#include <QLabel>
#include <QPropertyAnimation>
class ImageFade : public QWidget
{
    Q_OBJECT
public:
    explicit ImageFade(QWidget *parent = nullptr);
    ~ImageFade();
    void setBackgroundImage(QPixmap bkgdImg);
    void setOverlayImage(QPixmap ovlyImg);
    void startAnimation(int fadeDelay);
    void stop();
signals:
public slots:
    void stateChanged(QAbstractAnimation::State state1, QAbstractAnimation::State state2);
private:
    QLabel *bkgdLabel;
    QLabel *ovlyLabel;
    QPixmap bkgdImg;
    QPixmap ovlyImg;
    QPropertyAnimation *fadeAnimation;
};
#endif // IMAGEFADE_H

我几天前也遇到了同样的问题。我可以分享您的代码如何使用动画隐藏/显示标签。

QGraphicsOpacityEffect *opacity;
opacity = new QGraphicsOpacityEffect("label_name");
ui->"label_name"->setGraphicsEffect(opacity);
QPropertyAnimation *anim = new QPropertyAnimation(opacity, "opacity");
anim->setEasingCurve(QEasingCurve::Linear);
anim->setStartValue(1.0);
anim->setEndValue(0.01);
anim->setDuration(1000);
anim->start(QAbstractAnimation::DeleteWhenStopped);

这是一个很小的示例,如何使用qpropertyanimation隐藏Qlabel。如果您设置启动值0.01 末端值1.0