Important: Please read the Qt Code of Conduct -

Animation with QPropertyAnimation and QTimer

  • Hello
    I try to make a program that shows animations on the icons while loading

    the problem is that when l 'animation ends the icon is as before but still "nuanced"

    I tried with UI-> label1-> setPixmap (QPixmap ("path"));
    but it does not work

    this is my code of animaction

    //your code here
    void MainWindow::change()
        effect1 = new QGraphicsOpacityEffect();
        anim1 = new QPropertyAnimation(effect1,"opacity");
        anim1->setDuration(1000);//quanto dura l'effetto
        anim1->setLoopCount(1000);//ogni quanto riparte
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
    blink = !blink;
    //your code here
    void MainWindow::e2()

  • Lifetime Qt Champion

    @21sdino said:


    Hi, and welcome
    Im not sure what you say, so im guessing :)

    It sounds like your problem is that the opacity
    stays at 0.3 after animation ends?

    Cant you just set opacity back to 1.0 ?

  • Thank you,
    It is not the problem.
    Even if imposed top 0.3, and the end to 1
    when I run the program a few icons finish the animation with opacity to 1 instead other end with opacity 0.3
    and I do not understand why

  • Lifetime Qt Champion

    @21sdino said:

    Ok :)

    You should put
    QDebug() << "Called: EX";
    in e1(), e2() etc
    to see if somehow its not called?

    its hard to see from code how it really works.

    QList<QLabel*> LabelList= mainwindow->findChilden<QLabel *>();

    Gives you all Labels on the parent. ( mainwindow) in a neat list.
    Dont solve your isse but could be nicer code :)

  • the program creates 7 QLabels
    assigns a gray icon
    create an animation
    assigns the animamazione the first icon
    starts a 3 second timer
    the first icon opacifies an illuminated icon to 3 times
    When the timer ends, sometimes the icon is to be illuminated other times it remains opaque

  • Lifetime Qt Champion

    So what u say is that the actual animation
    on 1 icon do not do the same?

    Sometimes it ends right , sometimes not?

  • from time to time changes on all icons, some of it ends up well on some no

  • Lifetime Qt Champion

    well I cant guess from the shown code.

    They should all run for 1 sec.
    But you seem also to send stop to the animation every 3 sec? via the timer.

    So are you sure the logic is as you want?

  • I want that when the timer ends the icon appears to be illuminated
    but this does not work
    connect (timer, SIGNAL (timeout ()), UI- <label1, SLOT (setPIxmap (pix));

  • Lifetime Qt Champion

    that is not valid syntax :)
    connect (timer, SIGNAL (timeout ()), UI- <label1, SLOT (setPIxmap (pix));
    You cannot just call a function like that with a random parameter. You must use a slot function that
    has same parameters as the signal. here signal is timeout () and it has no parameters.

    So Its not how it works. Did u study ?

    you could hook up

    and then call
    setPIxmap (pix) in that slot

  • so? connect(anim1,SIGNAL(finished()),ui->label1,SLOT(setPixmap(pix)));

  • Lifetime Qt Champion

    so that is still wrong u cannot just use a random function as slot. u must make your own :)
    void AnimFinished();

    void mainwindow::AnimFinished() {
    setPIxmap (pix); /// here its legal


  • the problem remains the same I do not understand why

  • Lifetime Qt Champion

    but when animation is finished, your slot is called
    and u can then set the pixmap you want?

    Im not sure which part is not working.

  • only it works in more cases than before but not in all

  • Lifetime Qt Champion

    Ok, i cant tell. seems ok.
    You should single step and see what is going on.

  • i'm tring this from 2 days and i'don't find the solution

  • Lifetime Qt Champion

    ok, but u need to use more QDebug and find out where it fails.
    Sadly we cannot just guess it. Or at Least I cannot.

  • Could you please explain in detail what you want to do ?
    Your code does not seem to be very structured.
    As @mrjj says we can only guess what you are doing.

    I think you have to remove your QGraphicsOpacityEffect after animation ends (ui->label2->setGraphicsEffect(NULL);) because your end value is 0.3 and this means it has no full opacity.

Log in to reply