How to make Qt widgets fade in or fade out?

后端 未结 3 1255
天命终不由人
天命终不由人 2020-12-24 12:54

I am trying to fade in and fade out a QLabel or for that matter any QWidget subclass. I have tried with QGraphicsEffect, but unfortuna

3条回答
  •  Happy的楠姐
    2020-12-24 13:40

    Try expose some part of palette as property of label then animate it:

    Q_PROPERTY(QColor color READ color WRITE setColor)
    
    void MyLabel::setColor(const QColor &value) {
        QPalette palette;
        palette.setBrush(QPalette::WindowText, value);
        setPalette(palette);
    }
    
    QColor MyLabel::color() {
        return palette(QPalette::Normal, QPalette::Window).
    }
    
    void MyLabel::startFadeOutAnimation() {
        QPropertyAnimation *animation = new QPropertyAnimation(label, "color", this);
        QColor c = label->color();
        animation->setKeyValueAt(0, c);
        c.setAlpha(0);
        animation->setKeyValueAt(1, c);
        animation->setEasingCurve(QEasingCurve::OutQuad);
        animation->setDuration(1000);
        animation->start(QAbstractAnimation::DeleteWhenStopped);
    }
    

    You can try avoid subclassing by defining and registering new interpolator which will handle QPalette qRegisterAnimationInterpolator, but this is a bit complicated.

提交回复
热议问题