Important: Please read the Qt Code of Conduct -

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->move(112, 158);
        QObject::connect(centralAnim, SIGNAL(finished()), this, SLOT(endAnim()));

    The "endAnim"SLOT:

    void mainWindow::endAnim()
        endAnimPicture = new QLabel(this);
        endAnimPicture->move(225, 158);

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

    void mainWindow::btn3()

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

    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?


  • Lifetime Qt Champion


    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