Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QPropertyAnimation makes my widget disappear



  • I made a timer in my program, and I want to do some animation when timeout. So I used QPropertyAnimation, but here is a problem, when the function run, my widget disappeared. The function is like this:
    d1c2f38d-08d8-435b-b6be-0bf86441b200-image.png

    84585b12-78e4-4ed4-b978-2d9ce2c57d45-image.png
    What makes the problem? thank you.


  • Qt Champions 2019

    @Donation Can you please post your code as text instead of screen-shot?
    Maybe your widget is just transparent when animation finishes?



  • @jsulm
    here is my code

    #include "Widget.h"
    #include "ui_Widget.h"
    
    Widget::Widget(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Widget),
        m_timer(new QTimer)
    {
        ui->setupUi(this);
        connect(m_timer, SIGNAL(timeout()), this, SLOT(OnTimer()));
    }
    
    Widget::~Widget()
    {
        delete ui;
    }
    
    void Widget::StartTimer()
    {
        m_time = ui->lineEdit->text().toInt();
        m_past = 0;
    //    m_isCheck = false;
    //    m_isStartTime = true;
        ui->label->setText(QString::number((m_time - m_past)));
        m_timer->start(1000);
    }
    
    void Widget::OnTimer()
    {
        m_past++;
    
        if(m_past != m_time)
        {
            ui->label->setText(QString::number((m_time - m_past)));
        }
        else
        {
            m_timer->stop();
            TimeOut();
        }
    
    }
    
    void Widget::SetTime(int time)
    {
        m_time = time;
    }
    
    void Widget::TimeOut()
    {
        this->setWindowFlags(Qt::WindowStaysOnTopHint);
        ui->label->setText("Time out!");
    //    onShakeWindow();
        onTwinkle();
    }
    
    int Widget::alpha() const
    {
        return m_alpha;
    }
    
    void Widget::setAlpha(const int alpha)
    {
        m_alpha = alpha;
        QString strQSS = QString("color: rgb(0, 160, 230); background-color: rgba(10, 160, 105, %1);").arg(m_alpha);
        ui->label->setStyleSheet(strQSS);
    
    }
    
    void Widget::onShakeWindow()
    {
        QPropertyAnimation *pAnimation = new QPropertyAnimation(this, "pos");
        pAnimation->setDuration(500);
        pAnimation->setLoopCount(2);
    
        int x = geometry().x();
        int y = geometry().y();
        x = 10;
        y = 10;
    
        pAnimation->setKeyValueAt(0, QPoint(x - 3, y - 3));
        pAnimation->setKeyValueAt(0.1, QPoint(x + 6, y + 6));
        pAnimation->setKeyValueAt(0.2, QPoint(x - 6, y + 6));
        pAnimation->setKeyValueAt(0.3, QPoint(x + 6, y - 6));
        pAnimation->setKeyValueAt(0.4, QPoint(x - 6, y - 6));
        pAnimation->setKeyValueAt(0.5, QPoint(x + 6, y + 6));
        pAnimation->setKeyValueAt(0.6, QPoint(x - 6, y + 6));
        pAnimation->setKeyValueAt(0.7, QPoint(x + 6, y - 6));
        pAnimation->setKeyValueAt(0.8, QPoint(x - 6, y - 6));
        pAnimation->setKeyValueAt(0.9, QPoint(x + 6, y + 6));
        pAnimation->setKeyValueAt(1, QPoint(x- 3, y - 3));
        pAnimation->start(QAbstractAnimation::DeleteWhenStopped);
    }
    
    void Widget::onTwinkle()
    {
        QPropertyAnimation* pa = new QPropertyAnimation();
        pa->setTargetObject(this);
        pa->setPropertyName("alpha");
        pa->setDuration(5);
        pa->setLoopCount(5);
        pa->setKeyValueAt(0, 255);
        pa->setKeyValueAt(0.5, 100);
        pa->setKeyValueAt(1, 255);
        pa->start();
    }
    
    //void Widget::on_pushButton_clicked()
    //{
    //    if(m_isStartTime == false)
    //    {
    //        return;
    //    }
    
    //    m_isCheck = true;
    //    m_isStartTime = false;
    //}
    
    void Widget::on_pushButton_2_clicked()
    {
        StartTimer();
    }
    

  • Moderators

    @Donation I'm pretty sure alpha is not a valid property of a QWidget, you're probably looking for opacity


    edit: nope, opacity does not exist either, one is supposed to use https://doc.qt.io/qt-5/qgraphicsopacityeffect.html



  • @J-Hilk
    thanks for your help, but what about onShakeWindow(), it use pos as the property, and has the same problem with onTwinkle()


  • Moderators

    @Donation
    not for me, works perfectly fine.

    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
    
        QWidget w;
    
        w.resize(100, 100);
        w.show();
    
        QPropertyAnimation *p= new QPropertyAnimation();
        p->setTargetObject(&w);
        p->setPropertyName("pos");
        p->setDuration(2000);
        p->setLoopCount(-1);
        int x = 100;
        int y = 100;
        p->setKeyValueAt(0, QPoint(x - 3, y - 3));
        p->setKeyValueAt(0.1, QPoint(x + 6, y + 6));
        p->setKeyValueAt(0.2, QPoint(x - 6, y + 6));
        p->setKeyValueAt(0.3, QPoint(x + 6, y - 6));
        p->setKeyValueAt(0.4, QPoint(x - 6, y - 6));
        p->setKeyValueAt(0.5, QPoint(x + 6, y + 6));
        p->setKeyValueAt(0.6, QPoint(x - 6, y + 6));
        p->setKeyValueAt(0.7, QPoint(x + 6, y - 6));
        p->setKeyValueAt(0.8, QPoint(x - 6, y - 6));
        p->setKeyValueAt(0.9, QPoint(x + 6, y + 6));
        p->setKeyValueAt(1, QPoint(x- 3, y - 3));
        p->start();
    
        return app.exec();
    }
    

    Recording1.gif



  • @J-Hilk
    If I put these codes in main.cpp, it also works fine. But when I put into member function onShakeWindow(), the problem comes out.
    20200402_204737.gif


  • Moderators

    @Donation is your widget part of a layout?



  • @J-Hilk No, I only made one widget, and put some QPushButton and QLable on it.


Log in to reply