0

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?

  • @musicamante I think that is using additional headerfiles, I wanted to use a class within the Python script itself while loading the UI externally – Giuseppe Marziano Jan 11 '22 at 15:42
  • That is exactly what widget promotion does. No additional header files are required. – ekhumoro Jan 11 '22 at 18:14
  • @GiuseppeMarziano please carefully read the linked answers and follow their directions: there's no "header file", that is just a label for what's generally used with C++, in Python that is the module in which the custom class is stored. – musicamante Jan 11 '22 at 18:50
  • The linked answer is a little confusing because of the extra junk about .h files. You can ignore that. In this example, your custom widget should be saved in a file named `pushbutton.py`. And you need to have an import in your Python file that runs loadUIC: `from pushbutton import PushButton`. If you do these steps, your promoted widget should work very well. – bfris Jan 12 '22 at 01:01
  • 1
    @bfris the import is not necessary, as loadUi will do it on its own. – musicamante Jan 12 '22 at 14:41
  • @musicamante. Ooh. that's a nice tip. I just tested it out and the import is not needed. – bfris Jan 12 '22 at 21:41
  • @bfris cool! so where would I put the custom widget python file? how would the import know that I'm talking about that python file specifically? – Giuseppe Marziano Jan 14 '22 at 17:37
  • @GiuseppeMarziano, the easiest place to put the custom widget file is in the same folder as your main program. Somehow loadUI does the import of that widget for you. You can also put the file in a subfolder in that case, you use the format `subfolder/customwidget.h` in the Header file in Promoted Widgets window in Designer. I have not experimented to see if you can go to other directories using `../` – bfris Jan 14 '22 at 18:16

0 Answers0