I have implemented a custom label that animates the font size. When defining the UI programatically, as in AniLabelTest, the animation works. However, when using the UI designed in QtCreator, the animation does not work. How could it be fixed?
from PyQt5 import QtWidgets, uic
from PyQt5.QtCore import QSequentialAnimationGroup, QPropertyAnimation, pyqtProperty
from PyQt5.QtWidgets import QMainWindow, QLabel
from resources.utils import resource_path
from resources.widgets import AnimatedLabel
class AnimatedLabel(QLabel):
def __init__(self, text, increase, decrease, min_size, max_size, duration):
super().__init__()
self.labelFont = self.font()
self.setText(text)
self.ani_group = QSequentialAnimationGroup()
if increase and decrease:
duration = int(duration / 2)
if increase:
self.ani_increase = QPropertyAnimation(self, b'labelFontSize')
self.ani_increase.setStartValue(min_size)
self.ani_increase.setEndValue(max_size)
self.ani_increase.setDuration(duration)
self.ani_group.addAnimation(self.ani_increase)
if decrease:
self.ani_decrease = QPropertyAnimation(self, b'labelFontSize')
self.ani_decrease.setStartValue(max_size)
self.ani_decrease.setEndValue(min_size)
self.ani_decrease.setDuration(duration)
self.ani_group.addAnimation(self.ani_decrease)
@pyqtProperty(int)
def labelFontSize(self):
return self.labelFont.pointSize()
@labelFontSize.setter
def labelFontSize(self, size):
self.labelFont.setPointSize(size)
self.setFont(self.labelFont)
def start(self):
"""
Start animation
"""
print("Animation started")
self.setHidden(False)
self.ani_group.start()
class AniLabelTest(QMainWindow):
def __init__(self):
super(AniLabelTest, self).__init__()
self.startButton = QtWidgets.QPushButton('Start')
self.label = AnimatedLabel(f"test", True, True, 16, 60, 1000)
self.setCentralWidget(QtWidgets.QWidget(self))
layout = QtWidgets.QVBoxLayout(self)
self.centralWidget().setLayout(layout)
layout.addWidget(self.startButton)
layout.addWidget(self.label)
self.startButton.clicked.connect(self.label.start)
Ui_MainWindow, _ = uic.loadUiType(
resource_path("resources/ui/mainwindow2.ui")
)
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setupUi(self)
self.label.hide()
self.fdbckTargetLabel = AnimatedLabel(f"test", True, True, 16, 60, 1000)
self.frame.layout().addWidget(self.fdbckTargetLabel, 0, 0)
self.pushButton.clicked.connect(self.fdbckTargetLabel.start)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
test = AniLabelTest()
# test = MainWindow()
test.show()
sys.exit(app.exec_())
The .ui file is
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>594</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<property name="styleSheet">
<string notr="true">QWidget {
font-size: 16pt
};
</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QFrame" name="frame">
<property name="styleSheet">
<string notr="true"/>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="1">
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>START</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>ph</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>17</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
<property name="title">
<string>File</string>
</property>
</widget>
<widget class="QMenu" name="menuSettings">
<property name="title">
<string>Settings</string>
</property>
<addaction name="actionChange_Settings"/>
</widget>
<addaction name="menuFile"/>
<addaction name="menuSettings"/>
</widget>
<action name="actionChange_Settings">
<property name="text">
<string>Change Settings</string>
</property>
</action>
</widget>
<resources/>
<connections/>
</ui>
I have tried implementing the start() in a separate thread using QRunnable and QThreadingPool but it does not work