After working with the introduction to PySide6, I began to develop a simple GUI for an app. One thing I noticed is the requirement to add a component, like a QLabel, then having to determine where the Layout should lie.
When I'm working with a list of things that need to be input, I end up with stuff like
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# Many declarations of attributes
self.line_name = QLabel("Name")
self.input_name = QInput("Example Name")
self.line_email = QLabel("Email")
self.input_email = QInput("[email protected]")
self.radio1 = QRadioButton("Option 1")
self.radio2 = QRadioButton("Option 2")
self.radio3 = QRadioButton("Option 3")
...
# Many calls to "add Layout" for each attribute
self.layout.addWidget(self.line_name)
self.layout.addWidget(self.input_name)
self.layout.addWidget(self.line_email)
self.layout.addWidget(self.input_email)
self.layout.addWidget(self.radio1)
self.layout.addWidget(self.radio2
self.layout.addWidget(self.radio3
...
Question is: How can I avoid repetitive layout.addWidget() calls in PySide6?
self.layout.addWidget(QLabel('whatever'))); 3. it's not modular and may lead to inappropriate assumptions. For the "ways to do it better", sorry, but it's not the scope of SO to provide opinions.addWidgetand others will use GUI programs to generate layout - i.e. QtDesigner, QtCreatorself.input_name = self.add(QInput("Name")). It allows to keep it also on list or in dictionary.