Animation on QWidget is not working...



  • I want a widget to animate its opacity when it is shown/hidden. I used the below code, but it does not work.

    If I animate the property "maximumHeight", it gets animated in show(), but not in hide(). Could someone tell me where I am going wrong?

    Header file
    byeform.h

    #include <QWidget>
    #include <QPropertyAnimation>
    
    namespace Ui {
    class ByeForm;
    }
    
    class ByeForm : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit ByeForm(QWidget *parent = 0);
        ~ByeForm();
    
    private:
        Ui::ByeForm *ui;
    
        QPropertyAnimation *mpTransition;
    
    protected:
        bool eventFilter(QObject *obj, QEvent *event);
    };
    

    Source file
    byeform.cpp

    #include "byeform.h"
    #include "ui_byeform.h"
    #include <QDebug>
    
    ByeForm::ByeForm(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::ByeForm)
    {
        ui->setupUi(this);
    
        this->installEventFilter(this);
    
        mpTransition = new QPropertyAnimation(this, "windowOpacity");
        mpTransition->setDuration(1000);
        mpTransition->setStartValue(0.00);
        mpTransition->setEndValue(1.00);
    }
    
    ByeForm::~ByeForm()
    {
        delete ui;
    }
    
    bool ByeForm::eventFilter(QObject *obj, QEvent *event)
    {
        if (this == obj && QEvent::Show == event->type())
        {
            qDebug() << Q_FUNC_INFO << "in show";
            mpTransition->setDirection(QAbstractAnimation::Forward);
            mpTransition->start();
        }
        else if (this == obj && (QEvent::Hide == event->type() ||
                                 QEvent::Close == event->type()))
        {
            mpTransition->setDirection(QAbstractAnimation::Backward);
            mpTransition->start();
        }
    
        return false;
    }
    

  • Moderators

    If your window is hidden then of course you won't see it animating.
    You need to ignore the close event (return true), start the animation and then close it manually when animation finishes (finished signal).



  • @Chris-Kawa

    I do not close the widget, but only hide and show on a button click.

    void MainWindow::on_pushButton_clicked()
    {
    if (mpHelloForm->isVisible())
    {
    mpHelloForm->hide();
    mpByeForm->show();
    }
    else
    {
    mpHelloForm->show();
    mpByeFrame->hide();
    }
    }


  • Moderators

    Yes and, as I said, you can't see the animation because the widget is already hidden.
    Instead of hiding it and then starting animation start it and hide the widget in response to animation's finished signal.



  • @Chris-Kawa

    I also tried the below code, where hide is handled separately. This also does not animate the widget. I just made the widget stay for 1 sec.

    void ByeForm::setVisible(bool visible)
    {
    qDebug() << Q_FUNC_INFO << "invoked";
    if(isVisible() && !visible) // transition to hide
    {
    mpTransition->setDirection(QAbstractAnimation::Backward);
    mpTransition->start();
    QTimer::singleShot(1000, this, SLOT(hideForm()));
    }
    if(!isVisible() && visible) // transition to show
    {
    mpTransition->setDirection(QAbstractAnimation::Forward);
    mpTransition->start();
    QWidget::setVisible(true);
    }
    }

    void ByeForm::hideForm()
    {
    QWidget::setVisible(false);
    }

    Also, I have a doubt why it did not animate when show is called?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.