how to make the widget fade out with the animation object?
-
int Dialog::onAnimationFade(QWidget*from,QWidget* to){ //m_Animation = new QLabel(); m_Animation->setParent(from); m_Animation->resize(from->size()); m_Animation->setPixmap(from->grab()); m_Animation->show(); from->hide(); QPropertyAnimation *animation = new QPropertyAnimation(m_Animation,"windowOpacity"); animation->setDuration(600); animation->setStartValue(0.95); animation->setEndValue(0.30); animation->setEasingCurve(QEasingCurve::BezierSpline); QObject::connect(animation, &QPropertyAnimation::finished, [to,this]() { to->show(); m_Animation->hide(); }); animation->start(); }
when i used the codes upside.the widget of the 'from' dosent fade out smoothly, it just disappeared suddenly.the animation seemed dont work.
-
int Dialog::onAnimationFade(QWidget*from,QWidget* to){ //m_Animation = new QLabel(); m_Animation->setParent(from); m_Animation->resize(from->size()); m_Animation->setPixmap(from->grab()); m_Animation->show(); from->hide(); QPropertyAnimation *animation = new QPropertyAnimation(m_Animation,"windowOpacity"); animation->setDuration(600); animation->setStartValue(0.95); animation->setEndValue(0.30); animation->setEasingCurve(QEasingCurve::BezierSpline); QObject::connect(animation, &QPropertyAnimation::finished, [to,this]() { to->show(); m_Animation->hide(); }); animation->start(); }
when i used the codes upside.the widget of the 'from' dosent fade out smoothly, it just disappeared suddenly.the animation seemed dont work.
@nicker-player said in how to make the widget fade out with the animation object?:
animation->setEasingCurve(QEasingCurve::BezierSpline);
What's the point for this?
If you want to fade a widget out, I suggest usingQEasingCurve::OutQuad
or
QEasingCurve::Linear
Also why is your end value for "opacity"
0.30
( = 70% transparent / 30% opaque )?
If you want to make it fade-out completely, it needs to end at0.00
.
In addition I would set the start value to1.00
.Or why do you use 95% to 30%?
Is it ChatGPT code above? :DD
Edit:
What I've noticed afterwards:
Besides all what I wrote above regarding your animation config:
Makingfrom
the parent ofm_animation
and then hidingfrom
, will also hide all children... includingm_animation
.
Which makes everything else (the screen grabbing, the animation...) completely useless, as you won't see any of it anyway. -
Here is an excerpt from the documentation of QWidget about translucent widgets:
To enable this feature in a top-level widget, set its Qt::WA_TranslucentBackground attribute with setAttribute() and ensure that its background is painted with non-opaque colors in the regions you want to be partially transparent.
Platform notes:
- X11: This feature relies on the use of an X server that supports ARGB visuals and a compositing window manager.
- Windows: The widget needs to have the Qt::FramelessWindowHint window flag set for the translucency to work.
- macOS: The widget needs to have the Qt::FramelessWindowHint window flag set for the translucency to work.
There is some additional information for the attribute
autoFillBackground
. Maybe the suggestions for this attribute already work. -
@nicker-player said in how to make the widget fade out with the animation object?:
animation->setEasingCurve(QEasingCurve::BezierSpline);
What's the point for this?
If you want to fade a widget out, I suggest usingQEasingCurve::OutQuad
or
QEasingCurve::Linear
Also why is your end value for "opacity"
0.30
( = 70% transparent / 30% opaque )?
If you want to make it fade-out completely, it needs to end at0.00
.
In addition I would set the start value to1.00
.Or why do you use 95% to 30%?
Is it ChatGPT code above? :DD
Edit:
What I've noticed afterwards:
Besides all what I wrote above regarding your animation config:
Makingfrom
the parent ofm_animation
and then hidingfrom
, will also hide all children... includingm_animation
.
Which makes everything else (the screen grabbing, the animation...) completely useless, as you won't see any of it anyway.@Pl45m4
I just follow your tips.rem the code of the from->hide();.but how to figure out the result what i wanted.
widget a faded out and the other one faded in.
so that the user could change the ui interface with the animations. -
@Pl45m4
I just follow your tips.rem the code of the from->hide();.but how to figure out the result what i wanted.
widget a faded out and the other one faded in.
so that the user could change the ui interface with the animations.@nicker-player said in how to make the widget fade out with the animation object?:
how to figure out the result what i wanted.
How should we know what you want?
I used the code you have posted and corrected the obvious issues.Have you also corrected the animation?
Does it work now?
What do you expect then?There is nothing about "fading in" in your current code.
So you need the same property animation again (with an "in"-curve) and use it on yourto
widget. -
@nicker-player said in how to make the widget fade out with the animation object?:
how to figure out the result what i wanted.
How should we know what you want?
I used the code you have posted and corrected the obvious issues.Have you also corrected the animation?
Does it work now?
What do you expect then?There is nothing about "fading in" in your current code.
So you need the same property animation again (with an "in"-curve) and use it on yourto
widget.@Pl45m4
what I wanted is that one widget fade out and hide(),then another widget came out and show.but if I added both of the widgets,the layout became twice size .Bu f if I hided one of them,then the animations dont work.I saw that someone said that the windowOpacity only worked for the whole window ui object,but not the child object of the qwidget.So it is normally that it wont work ? -
@Pl45m4
what I wanted is that one widget fade out and hide(),then another widget came out and show.but if I added both of the widgets,the layout became twice size .Bu f if I hided one of them,then the animations dont work.I saw that someone said that the windowOpacity only worked for the whole window ui object,but not the child object of the qwidget.So it is normally that it wont work ?@nicker-player said in how to make the widget fade out with the animation object?:
what I wanted is that one widget fade out and hide(),then another widget came out and show.but if I added both of the widgets,the layout became twice size .Bu f if I hided one of them,then the animations dont work.
Yeah, I got that, but your initial code as well as your topic title is only about fading out :)
So it is normally that it wont work ?
No, you can make it work.
WithQStackedLayout
, for example.Put all widgets you want to replace via fading in and fading out on top of each other in the layout.
Then write a function similar to yours above, where you add your animations and change the currently active widget in the layout.
You simply always fade out the current one (currentIndex ofQStackedLayout
), and bring the widget of your choice back up to the front via "fade-in" animation. Change the active index as soon as the animation finishes. -
@nicker-player said in how to make the widget fade out with the animation object?:
what I wanted is that one widget fade out and hide(),then another widget came out and show.but if I added both of the widgets,the layout became twice size .Bu f if I hided one of them,then the animations dont work.
Yeah, I got that, but your initial code as well as your topic title is only about fading out :)
So it is normally that it wont work ?
No, you can make it work.
WithQStackedLayout
, for example.Put all widgets you want to replace via fading in and fading out on top of each other in the layout.
Then write a function similar to yours above, where you add your animations and change the currently active widget in the layout.
You simply always fade out the current one (currentIndex ofQStackedLayout
), and bring the widget of your choice back up to the front via "fade-in" animation. Change the active index as soon as the animation finishes.@Pl45m4
Ive tried what u just mentioned.but didnt work.someone said that the if u want to get the effect of the fade out for the child widget,you need to use the other class.heres the code i founded on somewhere.QGraphicsOpacityEffect *m_pGraphicsOpacityEffect = new QGraphicsOpacityEffect(ui->groupBox_login); m_pGraphicsOpacityEffect->setOpacity(1.0); ui->groupBox_login->setGraphicsEffect(m_pGraphicsOpacityEffect); QPropertyAnimation *animation = new QPropertyAnimation(m_pGraphicsOpacityEffect,"opacity",ui->groupBox_login); animation->setDuration(500); animation->setStartValue(1); animation->setEndValue(0); animation->setEasingCurve(QEasingCurve::Linear); animation->start(); [link text](http://www.lotfond.com/articles/detail?2023123101)
-
@Pl45m4
Ive tried what u just mentioned.but didnt work.someone said that the if u want to get the effect of the fade out for the child widget,you need to use the other class.heres the code i founded on somewhere.QGraphicsOpacityEffect *m_pGraphicsOpacityEffect = new QGraphicsOpacityEffect(ui->groupBox_login); m_pGraphicsOpacityEffect->setOpacity(1.0); ui->groupBox_login->setGraphicsEffect(m_pGraphicsOpacityEffect); QPropertyAnimation *animation = new QPropertyAnimation(m_pGraphicsOpacityEffect,"opacity",ui->groupBox_login); animation->setDuration(500); animation->setStartValue(1); animation->setEndValue(0); animation->setEasingCurve(QEasingCurve::Linear); animation->start(); [link text](http://www.lotfond.com/articles/detail?2023123101)
That might also work, but from my personal experience and other topics like this one (see @Devopia53 's reply there), the first method should work as well, if done correctly.