Important: Please read the Qt Code of Conduct -

Repainting QWidget with QProperAnimation

  • I have a piece of code that initializes an instance of QWidget, puts it inside a layout and invokes the animation. All of that is happening in the loop. I've encountered a problem of animation being drawn only after the loop is over, though I need it to paint every iteration. QThread::sleep() , repaint() , update() , QCoreApplication::processEvents() have all failed to give me the solution. Is there a way to prompt the execution of the QProperAnimation inside setChunk() method?`

     for(int p = 0 ; p < 100 ; p++)
                    for(int i = 0 ; i < numOfPoints ; i++)
                        randomTestArray[i] = fMin + (double)rand() * (fMax - fMin) / RAND_MAX;
                    eq->setChunk(&randomTestArray[0] , &startArray[0]);

  • Lifetime Qt Champion

    @MadScientist92 said in Repainting QWidget with QProperAnimation:


    Can you also show how u start the animation ?

    I think the main issue is the loop. You will starve the event loop and QAnimations depends on it.
    Even you call processEvents() its not enough for smooth animation.

  • This is setChunk() method : 
            QPropertyAnimation *animationLine = new QPropertyAnimation(this , "nline");
            animationLine->setStartValue(QLine(xCoordinateArray[0] + 5 , *(startYArr + 0) , xCoordinateArray[1] , *(startYArr + 1)));
            animationLine->setEndValue(QLine(xCoordinateArray[0] + 5 , 150 - yCoordinateArray[0] , xCoordinateArray[1] ,  150 - yCoordinateArray[1]));
            connect(animationLine , &QPropertyAnimation::valueChanged , [=](){update();});
          QPropertyAnimation *animationLine2 = new QPropertyAnimation(this , "nline2");
            animationLine2->setStartValue(QLine(xCoordinateArray[1] + 5 , *(startYArr + 1) , xCoordinateArray[2] ,    *(startYArr + 2)));
            animationLine2->setEndValue(QLine( xCoordinateArray[1] + 5 , 150 - yCoordinateArray[1]  ,xCoordinateArray[2] , 150 - yCoordinateArray[2]));
            connect(animationLine2 , &QPropertyAnimation::valueChanged , [=](){update();});

    and paintEvent() :

    void Equalizer::paintEvent(QPaintEvent *event)
    QRect ellipse(mRect);
    QPainter painter(this);
    QRect ellipse2(mRect2);
    QRect ellipse3(mRect3);

  • Lifetime Qt Champion

    It looks ok so I guess its the loop of 100 that does it.
    Even if u call processEvents, then the double loop might block the loop so long that it
    cannot run the animations before later.

    How much is numOfPoints ?

  • @mrjj it's 20. Btw, in this case only the last animation is drawn. I've tried initializing Equalizer object and adding it to the layout upon every iteration and then destroying it, and that leaded to all animations being displayed at the same time.

  • Lifetime Qt Champion

    Ok, thats not so bad.

    If you only make one ( instead of 100 ), does it work ?

  • I've updated previous comment. It paints only one animation anyway

  • Lifetime Qt Champion


    Ok, im not sure what could be wrong.

    Is it possible to upload project somewhere and post link here?
    I want to run it.
    Something is going on, i dont see.
    Its clear the loop with 100 kills the event loop ( u need other design)
    but both animations should run when there is only 1.

  • @mrjj What's interesting, is that even if I try just to call this setChunk() method manually for two times in a row, without any loop, it still would paint only one animation.

  • Lifetime Qt Champion


    ok. sounds. odd. i dont have time before tonight to see if i get same result :)

  • Solved it by creating QTimer) Thanks for your help!

  • Lifetime Qt Champion

    Ok i was actually thinking about suggesting that to avoid the loop.

    Does it then run in parallel as you want?

  • Yes, it is now painting all the animations I want sequentially . Have to play around with the timing though, but that is still a huge step forward after a week of stupor)

  • Moderators

    Hi, from what I read sofar in this thread, I asume you want to draw a couple lines. In that way that only 1 line is drawn at a time? E.g: line 1 is drawn -> start drawing line2

    I would suggest the following:

    Move all your animations into a list:

    QList<QPropertyAnimation*> animationList;

    That make the following loop:

    for(int i = 0; i < animationList.size()-1; i++){
         connect(animationList[i], &QPropertyAnimation::finished, animationList[i+1], [=]{animationList[i+1]->start();});

    This will automatically start the next animation, once the previousone is finsihed. No need for extra QTimer management,

    If you want instead to draw multiple lines simultaneously, I would suggest looking into QParallelAnimationGroup

  • I was attempting to draw equalizer for sound. Therefore, I needed to paint multiple 'states' of it. The problem was that only the last 'state' was drawn, and I wasn't getting a sequence of different animations.

Log in to reply