0

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?

6
  • 1
    About the "why it isn't a default": 1. PyQt/PySide are bindings to the Qt library, the references you're creating are only relevant in Python and Qt has absolutely no awareness about them; 2. it's up to the developer to eventually create such attributes/references, as they're not automatically relevant (widgets can be added to a parent or layout without the need for any persistent reference, eg: 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. Commented Jul 14 at 0:52
  • 1
    probably for many people it is not problem to put few lines with addWidget and others will use GUI programs to generate layout - i.e. QtDesigner, QtCreator Commented Jul 14 at 2:46
  • I would use function which returns reference to widget so I could do self.input_name = self.add(QInput("Name")). It allows to keep it also on list or in dictionary. Commented Jul 14 at 2:58
  • 1
    There are many ways of solving this, all resulting in slightly different syntax and access methods after the fact - instead of being opinionated, PySide just gives you exactly what's behind and allows you to pick whatever you prefer, instead of making a specific choice like assigning to named attributes like you do. There's nothing wrong with that, it's just a choice that you can make for several reasons - it's a matter of opinion and context. Commented Jul 14 at 3:00
  • This has major disadvantage: autocompletion wont work for widget names Commented Jul 16 at 16:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.