Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QPropertyAnimation makes my widget disappear

QPropertyAnimation makes my widget disappear

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 858 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • DonationD Offline
    DonationD Offline
    Donation
    wrote on last edited by
    #1

    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.

    jsulmJ 1 Reply Last reply
    0
    • DonationD Donation

      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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      DonationD 1 Reply Last reply
      0
      • jsulmJ jsulm

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

        DonationD Offline
        DonationD Offline
        Donation
        wrote on last edited by
        #3

        @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();
        }
        
        J.HilkJ 1 Reply Last reply
        0
        • DonationD Donation

          @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();
          }
          
          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by J.Hilk
          #4

          @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


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          DonationD 1 Reply Last reply
          2
          • J.HilkJ J.Hilk

            @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

            DonationD Offline
            DonationD Offline
            Donation
            wrote on last edited by
            #5

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

            J.HilkJ 1 Reply Last reply
            0
            • DonationD Donation

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

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @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


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              DonationD 1 Reply Last reply
              2
              • J.HilkJ J.Hilk

                @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

                DonationD Offline
                DonationD Offline
                Donation
                wrote on last edited by
                #7

                @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

                J.HilkJ 1 Reply Last reply
                0
                • DonationD Donation

                  @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

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #8

                  @Donation is your widget part of a layout?


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  DonationD 1 Reply Last reply
                  0
                  • J.HilkJ J.Hilk

                    @Donation is your widget part of a layout?

                    DonationD Offline
                    DonationD Offline
                    Donation
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved