snu-quiqcl / qiwis

QuIqcl Widget Integration Software
MIT License
6 stars 2 forks source link

[CODE] `QWidget` layout initialization #35

Closed kangz12345 closed 1 year ago

kangz12345 commented 1 year ago

Problem

Currently, we use _initWidget() method to initialize the layout of the custom widgets. This approach has controversial properties:

PROS

Suggestion

Do not use _initWidget(). Instead, write them directly in __init__() and use comments for readability. For example, if the original code below

class Foo(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent=parent)
        self._initWidget()

    def _initWidget(self):
        self.button = QPushButton("button", self)
        # layout
        layout = QHBoxLayout(self)
        layout.addWidget(self.button)
        # signal connection
        self.button.click.connect(self._buttonSlot)

    def _buttonSlot(self):
        pass

would become

class Foo(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent=parent)
        # widgets
        self.button = QPushButton("button", self)
        # layout
        layout = QHBoxLayout(self)
        layout.addWidget(self.button)
        # connect signals
        self.button.click.connect(self._buttonSlot)

    def _buttonSlot(self):
        pass

If you have any opinion, please leave comments!

BECATRUE commented 1 year ago

It looks a good idea! I applied this after numgen.

Ahrrri commented 1 year ago

I agree! In some of my previous codes, I used to write like that.

BECATRUE commented 1 year ago

The origianl apps should be updated in accordance with this form.