我正在为我的应用程序创建一个加载屏幕,我想实现两个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

最佳答案

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

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

10-08 07:10