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()
    {
        name.clear();
        name.append(a[i]);
        qDebug()<<name;
    
        effect1 = new QGraphicsOpacityEffect();
        anim1 = new QPropertyAnimation(effect1,"opacity");
        anim1->setDuration(1000);//quanto dura l'effetto
        anim1->setLoopCount(1000);//ogni quanto riparte
        anim1->setStartValue(1);
        anim1->setEndValue(0.3);
        anim1->setEasingCurve(QEasingCurve::OutQuad);
        anim1->start(QAbstractAnimation::DeleteWhenStopped);
    
        switch(i)
        {
            case 0:
                e1();ui->label1->setPixmap(QPixmap(path+name));break;
            case 1:
                e2();ui->label2->setPixmap(QPixmap(path+name));break;
            case 2:
                e3();ui->label3->setPixmap(QPixmap(path+name));break;
            case 3:
                e4();ui->label4->setPixmap(QPixmap(path+name));break;
            case 4:
                e5();ui->label5->setPixmap(QPixmap(path+name));break;
            case 5:
                e6();ui->label6->setPixmap(QPixmap(path+name));break;
            case 6:
                e7();ui->label7->setPixmap(QPixmap(path+name));break;
        }
    blink = !blink;
        i++;}
    
    //your code here
    void MainWindow::e2()
    {
        timer->start(3000);
        ui->label2->setGraphicsEffect(effect1);
        connect(timer,SIGNAL(timeout()),anim1,SLOT(stop()));
        ui->label2->setPixmap(QPixmap(path+name));
    }
    

  • Qt Champions 2016

    @21sdino said:

    anim1->setStartValue(1);
    anim1->setEndValue(0.3);

    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


  • Qt Champions 2016

    @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.

    note:
    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


  • Qt Champions 2016

    @21sdino
    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


  • Qt Champions 2016

    @21sdino
    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.
    connect(timer,SIGNAL(timeout()),anim1,SLOT(stop()));

    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));


  • Qt Champions 2016

    @21sdino
    nope
    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 ?
    http://doc.qt.io/qt-5/signalsandslots.html

    you could hook up
    http://doc.qt.io/qt-5/qabstractanimation.html#finished

    and then call
    setPIxmap (pix) in that slot



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


  • Qt Champions 2016

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

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

    connect(anim1,SIGNAL(finished()),ui->label1,SLOT(AnimFinished()));



  • the problem remains the same I do not understand why


  • Qt Champions 2016

    @21sdino
    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


  • Qt Champions 2016

    @21sdino
    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


  • Qt Champions 2016

    @21sdino
    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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.