Qt SplashScreen w / Fading Logo未运行动画

时间:2017-09-14 01:35:58

标签: c++ qt

我正在为我的应用创建一个加载屏幕,我想实现两个QLabel(背景和叠加层),其中叠加层只是背景的发光轮廓。我希望这个叠加层淡入淡出(0.0 - 1.0)我使用QPropertyAnimation和windowOpacity属性作为标签,但没有任何效果。这是我的完整源代码。

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

1 个答案:

答案 0 :(得分:2)

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

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 ,则可以反之亦然(显示标签)