I've been able to create the class after converting the .ui to .py, but found it tedious if I wanted to make any changes or add anymore buttons.
Is it possible to add the class to the .py file that is using uic.loadUi(os.path.join(curPath, 'Ui', 'LITool.ui'), self)
?
The class I wanted to add created a transition timing effect for the hover action:
class PushButton(QtWidgets.QPushButton):
def __init__(self, parent=None):
super().__init__(parent)
self._animation = QtCore.QVariantAnimation(
startValue=QtGui.QColor("#6603fc"),
endValue=QtGui.QColor("#222222"),
valueChanged=self._on_value_changed,
duration=200,
)
self._update_stylesheet(QtGui.QColor("white"), QtGui.QColor("red"), QtGui.QColor("purple"))
def _on_value_changed(self, color):
foreground = (
QtGui.QColor("#CCCCCC")
if self._animation.direction() == QtCore.QAbstractAnimation.Forward
else QtGui.QColor("#CCCCCC")
)
border = (
QtGui.QColor("#222222")
if self._animation.direction() == QtCore.QAbstractAnimation.Forward
else QtGui.QColor("#6603fc")
)
self._update_stylesheet(color, foreground, border)
def _update_stylesheet(self, background, foreground, border):
self.setStyleSheet(
"""
QPushButton{
background-color: %s;
color: %s;
padding: 4px 4px;
text-align: center;
font-family: Arial;
font-size: 13px;
margin: 1px 1px;
border-width: 1px;
border-color: %s;
border-style: solid;
border-radius: 4px;
}
QPushButton:checked{
background-color: #6603fc;
color: #CCCCCC;
padding: 4px 4px;
text-align: center;
font-family: Arial;
font-size: 13px;
margin: 1px 1px;
border-width: 1px;
border-radius: 4px;
}
"""
% (background.name(), foreground.name(), border.name())
)
self.setFocusPolicy(QtCore.Qt.NoFocus)
def enterEvent(self, event):
self._animation.setDirection(QtCore.QAbstractAnimation.Backward)
self._animation.start()
super().enterEvent(event)
def leaveEvent(self, event):
self._animation.setDirection(QtCore.QAbstractAnimation.Forward)
self._animation.start()
super().leaveEvent(event)
And then within the MainWindow class, I'd just have self.calibrate = PushButton(self.centralwidget)
Since I can't access the MainWindow class, what is the best way to implement a custom QPushButton class into a python file that is loading the UI?