Solved Hide QGroupBox with QGraphicsOpacityEffect
-
Hello!!
I am trying to hide a QGroupBox with a QGraphicsOpacityEffect. I have this code:
void class::showStretchingElements() { if (m_bShowStretchingElements) { QGraphicsOpacityEffect *eff = new QGraphicsOpacityEffect(this); ui->groupObjects->setGraphicsEffect(eff); QPropertyAnimation *a = new QPropertyAnimation(eff,"opacity"); a->setDuration(750); a->setStartValue(1); a->setEndValue(0); a->setEasingCurve(QEasingCurve::OutBack); a->start(QPropertyAnimation::DeleteWhenStopped); connect(a, &QPropertyAnimation::finished,this, &cameraDialog::endAnimation); } else { QGraphicsOpacityEffect *eff = new QGraphicsOpacityEffect(this); ui->groupObjects->setGraphicsEffect(eff); QPropertyAnimation *a = new QPropertyAnimation(eff,"opacity"); a->setDuration(750); a->setStartValue(0); a->setEndValue(1); a->setEasingCurve(QEasingCurve::OutBack); a->start(QPropertyAnimation::DeleteWhenStopped); connect(a, &QPropertyAnimation::finished,this, &cameraDialog::endAnimation); } } void class::endAnimation() { if (m_bShowStretchingElements) { ui->groupObjects->setHidden(true); m_bShowStretchingElements = false; } else { ui->groupObjects->setHidden(false); m_bShowStretchingElements = true; } }
When I try to hide the QGroupBox, it works fine, but, when I try to show again, it shows it but without the animation. What's wrong here?
Thank you very much!
-
Try
a->setEasingCurve(QEasingCurve::InBack);
instead of
QEasingCurve::OutBack
in your fade-In (show) function. You are using a (cubic) function to set the opacity during your animation time. Currently you have the same function for hiding and showing theQGroupBox
. This is why you probably see some weird behavior.You can check these functions here: https://doc.qt.io/qt-5/qeasingcurve.html#Type-enum
Or is your fade-in animation not starting at all? Do you reach your
endAnimation
- slot twice (after fade-Out and fade-In)? -
@Pl45m4 Yes, I reach it twice. m_bShowStretchingElements is a boolean that changes and make the condition. I'm still having the same problem. When I call the show function for showing the QGroupBox, it appears suddenly, without any animation.
-
Your code doesn't look wrong. Move the
connect
statement some lines upwards (right after you inita
) and try again.This works for me (the only difference is, that I don't hide the
widget
after fading out. I also have a privateQGraphicsOpacityEffect
- member)MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow), m_eff(new QGraphicsOpacityEffect(this)) { m_eff->setOpacity(1); ui->widget->setGraphicsEffect(m_eff); } void MainWindow::fadeIn() { QPropertyAnimation *animation = new QPropertyAnimation(m_eff, "opacity"); animation->setDuration(1000); animation->setStartValue(0); animation->setEndValue(1); animation->setEasingCurve(QEasingCurve::InQuart); animation->start(QPropertyAnimation::DeleteWhenStopped); } void MainWindow::fadeOut() { QPropertyAnimation *animation = new QPropertyAnimation(m_eff, "opacity"); animation->setDuration(1000); animation->setStartValue(1); animation->setEndValue(0); animation->setEasingCurve(QEasingCurve::OutQuart); animation->start(QPropertyAnimation::DeleteWhenStopped); }
-
@Pl45m4 I could solve it with your help! You were right, I don't hide the widget and it works! I only have to add a QGroupBox.setEnabled(false) and its done!.
-
Yes, after rethinking, it makes sense. :-)
When you fade-Out your GroupBox and hide it afterwards, the GroupBox is still hidden, when you start the animation. After animation time, you sethidden (false)
and the GroupBox just pops up. (because your fade-In animation process is already done)