26

サブクラスQLabelをフェードインおよびフェードアウトしようとしています。QWidgetで試してみましQGraphicsEffectたが、残念ながら Windows でのみ正常に動作し、Mac では正常に動作しません。

Mac と Windows の両方で機能する唯一の他のソリューションはpaintEvent、不透明度を設定し、派生で「不透明度」をQPainter定義し、不透明度を変更する独自のカスタムを持っているようです。Q_PROPERTYQLabelQPropertyAnimation

ご参考までに、関連するコード スニペットの下に貼り付けます。ここでもまだ問題があります - を再利用してQLabel::paintEventもうまくいかQPainterないようです。QWidgetサブクラス フェードアウトしたい、それは悪夢です。ここで明らかな間違いを犯しているかどうかを明確にしてください。

Q_PROPERTY(qreal opacity READ opacity WRITE setOpacity)

void MyLabel::setOpacity(qreal value) {
    m_Opacity = value;
    repaint();
}

void MyLabel::paintEvent((QPaintEvent *pe) {
    QPainter p;
    p.begin(this);
    p.setOpacity();
    QLabel::paintEvent(pe);
    p.end();
}

void MyLabel::startFadeOutAnimation() {
    QPropertyAnimation *anim = new QPropertyAnimation(this, "opacity");
    anim->setDuration(800);
    anim->setStartValue(1.0);
    anim->setEndValue(0.0);
    anim->setEasingCurve(QEasingCurve::OutQuad);
    anim->start(QAbstractAnimation::DeleteWhenStopped);
}
4

4 に答える 4

43

実際には、厄介なQPaintEventインターセプトや の厳しい要件なしでこれを行うための非常に簡単な方法がありますがQGraphicsProxyWidget、これは昇格したウィジェットの子では機能しません。以下の手法は、昇格したウィジェットとその子ウィジェットでも機能します。

ウィジェットをフェードイン

// w is your widget
QGraphicsOpacityEffect *eff = new QGraphicsOpacityEffect(this);
w->setGraphicsEffect(eff);
QPropertyAnimation *a = new QPropertyAnimation(eff,"opacity");
a->setDuration(350);
a->setStartValue(0);
a->setEndValue(1);
a->setEasingCurve(QEasingCurve::InBack);
a->start(QPropertyAnimation::DeleteWhenStopped);

ウィジェットをフェードアウト

// w is your widget
QGraphicsOpacityEffect *eff = new QGraphicsOpacityEffect(this);
w->setGraphicsEffect(eff);
QPropertyAnimation *a = new QPropertyAnimation(eff,"opacity");
a->setDuration(350);
a->setStartValue(1);
a->setEndValue(0);
a->setEasingCurve(QEasingCurve::OutBack);
a->start(QPropertyAnimation::DeleteWhenStopped);
connect(a,SIGNAL(finished()),this,SLOT(hideThisWidget()));
// now implement a slot called hideThisWidget() to do
// things like hide any background dimmer, etc.
于 2015-10-07T06:24:06.400 に答える