Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved QPropertyAnimation makes my widget disappear

    General and Desktop
    3
    9
    427
    Loading More Posts
    • 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.
    • Donation
      Donation last edited by

      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.

      jsulm 1 Reply Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion @Donation last edited by

        @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

        Donation 1 Reply Last reply Reply Quote 0
        • Donation
          Donation @jsulm last edited by

          @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.Hilk 1 Reply Last reply Reply Quote 0
          • J.Hilk
            J.Hilk Moderators @Donation last edited by 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

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

            Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


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

            Donation 1 Reply Last reply Reply Quote 2
            • Donation
              Donation @J.Hilk last edited by

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

              J.Hilk 1 Reply Last reply Reply Quote 0
              • J.Hilk
                J.Hilk Moderators @Donation last edited by

                @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

                Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


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

                Donation 1 Reply Last reply Reply Quote 2
                • Donation
                  Donation @J.Hilk last edited by

                  @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.Hilk 1 Reply Last reply Reply Quote 0
                  • J.Hilk
                    J.Hilk Moderators @Donation last edited by

                    @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

                    Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


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

                    Donation 1 Reply Last reply Reply Quote 0
                    • Donation
                      Donation @J.Hilk last edited by

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

                      1 Reply Last reply Reply Quote 0
                      • First post
                        Last post