'Label position in layout breaks after trying to resize the window after using QPropertyAnimation

I have the following problem: I'm trying to animate a QLabel, more specifically to make it fadeIn using QPropertyAnimation. It animates successfully, but when I'm trying to resize the window, the label breaks out of the layout that's in, making it overlap with the other items. The code:

        self.label = QtWidgets.QLabel(self.frame)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.label.sizePolicy().hasHeightForWidth())
        self.label.setSizePolicy(sizePolicy)
        self.label.setStyleSheet("color: #fc8102;\n"
"font-weight: bold;\n"
"font-size: 18px;")
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setObjectName("label")

        self.opacity_effect = QtWidgets.QGraphicsOpacityEffect(self.label)
        self.opacity_effect.setOpacity(0)

        self.label.setGraphicsEffect(self.opacity_effect)

        self.anim = QtCore.QPropertyAnimation(self.opacity_effect, b'opacity')
        self.anim.setDuration(800)
        self.anim.setStartValue(0)
        self.anim.setEndValue(1)
        self.anim.start()

        self.label.setAlignment(QtCore.Qt.AlignCenter)

        self.verticalLayout.addWidget(self.label)

Image with what's happening:

layout breaking

Image with how it's supposed to look:

how it's supposed to look

Thanks in advance.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source