0

I try to build a GUI with PyQt5 and currently I'm struggling with the following issue

I'd like to add a set of labels based on the items in a Dict

items: Dict = {'bli': False, 'bla': False, 'blub': False}

In fact the dict is much larger, but that shouldn't matter. So I thought I can achieve this by just iterating through the Dict and add each element as a QLabel.

def createGroup(self) -> QGroupBox:    
    group: QGroupBox = QGroupBox("Status")
    layout = QVBoxLayout()
    for element in self.items:
        layout.addWidget(QLabel(element))
    group.setLayout(layout)

    return group

This QGroupBox is then added to another layout and that works fine so far. The problem I now have is that I'd like to access these QLabels to e.g. change text. The only way I've found so far is to do something like

myGroup: QGroupBox = self.createGroup()
for child in statusBox.children():
    if type(child) is QLabel:
        child.setText("buuuh")

Altough this works, this doesn't seem to be a proper way. How would one do this to have better access to child widgets created in such a way?

Vlad Davidchenko
  • 454
  • 1
  • 7
  • 24
po.pe
  • 1,047
  • 1
  • 12
  • 27
  • How does the linked question correspond to this one? – po.pe Oct 23 '19 at 18:36
  • It's suggesting you keep a reference to the labels in a `dict`, so you can easily look them up by name. Another way would be to set a unique [objectName](https://doc.qt.io/qt-5/qobject.html#objectName-prop) for each label, so you can then do `statusBox.findChild('labelname')`. – ekhumoro Oct 24 '19 at 22:08

0 Answers0