Check if picture is displayed



  • Hello.

    I created a window that shows a little animation (gif) when the application starts.
    At the end of the animation, a "end" picture is displayed in the same window using QObject::connect.

        centralAnimLabel = new QLabel(this);
        centralAnim = new QMovie("windowLogs/centralAnim.gif");
        centralAnimLabel->setMovie(centralAnim);
        centralAnimLabel->move(112, 158);
        centralAnim->start();
        QObject::connect(centralAnim, SIGNAL(finished()), this, SLOT(endAnim()));
    

    The "endAnim"SLOT:

    void mainWindow::endAnim()
    {
        endAnimPicture = new QLabel(this);
        endAnimPicture->setPixmap(QPixmap("windowLogs/endCentralAnim.jpg"));
        endAnimPicture->move(225, 158);
        endAnimPicture->show();
    }
    

    A button is there to stop and hide the animation.
    Here is the SLOT the button is connected to:

    void mainWindow::btn3()
    {
        centralAnim->stop();
        centralAnimLabel->hide();
    }
    

    If I click the button before the animation is finished, it stops and hides. The "end" picture does not appear.
    So far all is right.

    Now, the problem is:
    I want the same button to be able to hide the "end" picture once the animation finished.
    Simply adding "endAnimPicture->hide();" to the SLOT of the button seems not to be possible because it crashes the application (no more responding message), if the button is clicked before the end of the animation.
    Quite logic in my opinion, since I tell the program to hide something that not exists yet.

    I tried setting a condition "if picture is displayed"...
    After trying many things that made crash the application, the button's SLOT looks like this:

    void mainWindow::btn3()
    {
        centralAnim->stop();
        centralAnimLabel->hide();
    
        if(!endAnimPicture)
        {
          endAnimPicture->hide();
        }
    }
    

    No more errors or crashes, but the picture does not hide by clicking the button at the end of the animation.

    Is this the right way to go?
    If yes, what is wrong with my condition?

    Thanks.


  • Lifetime Qt Champion

    Hi,

    Why are you creating your labels in these functions ? Just create them in your constructor, hide them as needed.

    Currently you have a big memory leak since you create a new endAnimPicture every time endAnim is called.



  • So easy... Shame on me :-)

    At the begining, I tried to create the label in the constructor and assign a picture to it inside the slot.
    What not really worked for me.

    Thank you for that and the memory leak tip!


Log in to reply
 

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