如何使 Qt 小部件淡入或淡出? [英] How to make Qt widgets fade in or fade out?

查看:63
本文介绍了如何使 Qt 小部件淡入或淡出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试淡入和淡出 QLabel 或任何 QWidget 子类.我已经尝试过 QGraphicsEffect,但不幸的是它只在 Windows 上运行良好,而在 Mac 上不起作用.

I am trying to fade in and fade out a QLabel or for that matter any QWidget subclass. I have tried with QGraphicsEffect, but unfortunately it works well only on Windows and not on Mac.

唯一可以在 Mac 和 Mac 上运行的其他解决方案Windows 似乎有我自己的自定义 paintEvent,我在其中设置了 QPainter 的不透明度,并在我的派生 Q_PROPERTY 中为opacity"定义了一个 Q_PROPERTYcode>QLabel 并通过 QPropertyAnimation 更改不透明度.

The only other solution which can work on both Mac & Windows seems to be having my own custom paintEvent where I set the opacity of QPainter and also define a Q_PROPERTY for "opacity" in my derived QLabel and change the opacity through QPropertyAnimation.

我粘贴在相关代码片段下方供您参考.我仍然在这里看到一个问题 - 重用 QLabel::paintEvent 似乎不起作用,它只有在我使用 QPainter 进行完整的自定义绘画时才有效,但是这似乎不是一个简单的方法,如果我需要为每个想要淡出的 QWidget 子类都这样做,那真是一场噩梦.请澄清我是否在这里做任何明显的错误.

I am pasting below the relevant code snippet for your reference. I still see an issue here - reusing the QLabel::paintEvent doesn't seem to be working, it works only if I do a complete custom painting using the QPainter, but that doesn't seem to be an easy way and if I need to do that for every QWidget subclass I want to fade out, that's a nightmare. Please clarify if I am doing any obvious mistakes here.

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);
}

推荐答案

实际上有一种超级简单的方法可以做到这一点,无需凌乱的 QPaintEvent 拦截,也无需 QGraphicsProxyWidget 的严格要求>,这不适用于提升的小部件子级.下面的技术甚至适用于升级的小部件及其子小部件.

There's actually a super easy way to do this without messy QPaintEvent intercepts and without the tough requirements of QGraphicsProxyWidget, which doesn't work on promoted widget children. The technique below will work even with promoted widgets and their children widgets.

小部件淡入淡出

// 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.

这篇关于如何使 Qt 小部件淡入或淡出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆