#include "../headers/qmodernstackedwidget.h" QModernStackedWidget::QModernStackedWidget(QWidget* parent) : QStackedWidget(parent) { opacityAnimation = new QPropertyAnimation(); opacityAnimation->setPropertyName("opacity"); opacityAnimation->setEasingCurve(QEasingCurve::InOutQuad); opacityAnimation->setDuration(400); opacityAnimation->setLoopCount(1); connect(opacityAnimation, SIGNAL(finished()), this, SLOT(handleAnimationFinished())); } void QModernStackedWidget::setCurrentWidgetAnimated(QWidget* widget) { if(widget == nullptr) return; this->newWidget = widget; // fade old widget out QGraphicsOpacityEffect* opacityEffect = new QGraphicsOpacityEffect(this); this->currentWidget()->setGraphicsEffect(opacityEffect); opacityEffect->setOpacity(1); opacityAnimation->setStartValue(1); opacityAnimation->setEndValue(0); opacityAnimation->setTargetObject(opacityEffect); opacityAnimation->start(); } void QModernStackedWidget::handleAnimationFinished() { // disable graphical effect to prevent errors this->currentWidget()->graphicsEffect()->setEnabled(false); if(this->newWidget == nullptr) { return; } // hide new widget QGraphicsOpacityEffect* opacityEffect = new QGraphicsOpacityEffect(this); this->newWidget->setGraphicsEffect(opacityEffect); opacityEffect->setOpacity(0); // set new widget this->setCurrentWidget(this->newWidget); this->newWidget = nullptr; // fade new widget in opacityAnimation->setTargetObject(opacityEffect); opacityAnimation->setStartValue(0); opacityAnimation->setEndValue(1); opacityAnimation->start(); }