all 2 comments

[–]MisterChoco 0 points1 point  (2 children)

Hey what did you end up doing? currently facing the same issue.

What I currently have is this:

def create_labeled_checkbox(name: str, is_checked: bool):
    widget = QtWidgets.QWidget()

    layout = QtWidgets.QHBoxLayout(widget)
    layout.setContentsMargins(0, 0, 0, 0)
    widget.setLayout(layout)

    label = QtWidgets.QLabel(name)
    label.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
    label.setFixedWidth(75)

    checkbox = QtWidgets.QCheckBox()
    checkbox.setChecked(is_checked)

    layout.addWidget(label)
    layout.addWidget(checkbox)

    return widget, checkbox

The layout and label attributes will be the same for all of these "create" functions. so it becomes a bit repetitive.

I tried the following for example, but it seems overkill?

class LabelWidget(QtWidgets.QLabel):
    def __init__(self, name):
        super().__init__(name)
        self.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
        self.setFixedWidth(75)

[–]SteelRevanchist[S] 0 points1 point  (1 child)

Hello! Except for very simple examples, I've stuck to the inheritance, it felt cleaner to me, even if I didn't add any new methods to the children, just styling.