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 loop continuously
Qt 6.11 is out! See what's new in the release blog

QPropertyAnimation loop continuously

Scheduled Pinned Locked Moved General and Desktop
qgraphicsanimat
6 Posts 3 Posters 6.8k Views 3 Watching
  • 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.
  • H Offline
    H Offline
    houmingc
    wrote on last edited by
    #1

    My code below fails to make animation loop continuously. Please kindly assist.

           ui->setupUi(this);
           QWidget *window= new QWidget;
          window->setWindowTitle("My App");
    
         ml = new WidgetMarqueeLabel(this);
        ml->setTextFormat(Qt::RichText);
        ml->setAlignment(Qt::AlignBottom);
       ml->setText("this is a very long text, weather is very hot");
       ml->setFont(QFont("Arial", 20,20));
    
       QGridLayout *layout= new QGridLayout;
      video();
    
       label= new QLabel;
       movie = new QMovie("/home/ubuntu/Desktop/graphic.gif");
       label->setMovie(movie);
      movie->start();
    
       int i;
      do{
        animation = new QPropertyAnimation(label,"geometry");
        animation->setDuration(1000);
        animation->setStartValue(QRect(0,200,100,30));
        animation->setEndValue(QRect(600,200,100,30));
        animation->start(QAbstractAnimation::DeleteWhenStopped);
          i++;
       }while(i<100);
    
    
       QTimer *timer = new QTimer();
      //connect(timer,SIGNAL(timeout()),movie,SLOT(move()));
      timer->start(60);
    
       layout->addWidget(videoWidget,0,0);
       layout->addWidget(videoWidget1,0,1);
       layout->addWidget(ml,1,0,1,2);
       layout->addWidget(label,2,0,1,2);
    
       window->setLayout(layout);
       window->show();
    
    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Did you try setLoopCount() ?

      157

      1 Reply Last reply
      1
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #3

        For looping do what @p3c0 said.
        I've got to ask though - do you really intend to start 100 animations of the same label and geometry at once? This seems weird and incorrect at best.
        In any case int i; has undefined value in c++ (no, it's not initialized to 0 automatically).
        And what's the timer for here? It doesn't seem to do anything.

        1 Reply Last reply
        0
        • H Offline
          H Offline
          houmingc
          wrote on last edited by
          #4

          Thanks for all the help given. i have 3 more Question

          Question 1
          animation->start(QAbstractAnimation::DeleteWhenStopped)
          The animation doesn't seem to delete itself when stopped?

          Question 2
          Strange, i can't make 2 or multiple label appear at the same time.
          How do i achieve this?

                QTimer *timer = new QTimer();
                connect(timer,SIGNAL(timeout()),this,SLOT(move()));
                timer->start(500);
          
               void MainWindow::move()
              {
                 animation = new QPropertyAnimation(label,"geometry");
                 animation->setDuration(1000);
                 animation->setLoopCount(4);
                 animation->setStartValue(QRect(0,990,30,30));
                 animation->setEndValue(QRect(4000,990,30,30));
                 animation->start(QAbstractAnimation::DeleteWhenStopped);
          

          }

          Question 3
          qt allocates a big horizontal tab for label. Very Ugly.
          How to ask qt in code to allocate a smaller horizontal tab for label?

                    layout->addWidget(videoWidget,0,0);
                    layout->addWidget(videoWidget1,0,1);
                    layout->addWidget(ml,1,0,1,2);
                    layout->addWidget(label,2,0,1,2);     //qt allocates a big space for this label
          
                   window->showMaximized();
                   window->setLayout(layout);
                   window->show();
          
          1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by Chris Kawa
            #5
            1. How did you checked that? The code from 2. doesn't track the lifetime of the animation in any way. I suspect it is deleted.

            2. This does not create multiple labels. This creates new animations for the same label. The animations are created every 0.5s and last 1s each so after a couple of seconds you've got a dozen of animations fighting to animate the same label.

            3. It's not Qt. It's you :) The last parameter in layout->addWidget(label,2,0,1,2); is column span. Since you gave it 2 and there are two columns of widgets in that layout it will take the whole width of that layout. Give it a 1 and see if that's what you need.

            Bonus 1. As previously mentioned - you won't be able to animate the "geometry" property of your label if it's in a layout. The layout takes it over and setting geometry has no effect.

            Bonus 2. The timer has no parent so it's a memory leak.

            Bonus 3. What's with the multiple shows? This should be enough:

            window -> setLayout(layout);
            window -> showMaximized();
            
            1 Reply Last reply
            0
            • H Offline
              H Offline
              houmingc
              wrote on last edited by
              #6

              "This does not create multiple labels. This creates new animations for the same label. The animations are created every 0.5s and last 1s each so after a couple of seconds you've got a dozen of animations fighting to animate the same label.

                connect(timer,SIGNAL(timeout),this,SLOT(move()));
              

              in Theory, if timer has short timeout, move() should executed label closed to each other resulting multiple label appearing.
              i can't achieve this, how to amend the code to achieve this?

              "Bonus 2. The timer has no parent so it's a memory leak"
              always parent a constructor [QTimer *timer = new QTimer(this)] else delete object-timer in destructor(which i did)

              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