0

I've done a lot of research, but could not find anyone with this specific problem so far. Very sorry if there is already a topic, but couldnt find it.

What im trying to do is to find a QLabel in a QGridLayout, by importing a .txt file and reading out names and values. For example: power 5

Now i need to find the QLabel named "power_fw" to set the text of this Label to "5"

set_value("power 5")

def set_value(talent):
     list_value = talent.split(" ")       # Now i have a list with talent[0] = "power" and its value at talent[1] = "5"
     talent_now = talent[0] + "_fw"       # I already have the exact name of the QLabel i am trying to find, which would be "power_fw" at this moment
     ui.talent_now.setText(talent[1])     # So the idea is that "talent_now" is a string whichs value is the name of the QLabel i am trying to set a new text. Obviously it does not work, because talent_now is not a QLabel but just its name. 

I hope someone can help me with this specific problem and again, sorry if this topic already exists in some way. I could not find anything that worked out yet.

topics i found interesting in context of this problem:

get widgets by name from layout

findChild on object created within pyqt designer

None of the solutions provided in these topics made me able to get this right.

Simon
  • 13
  • 1
  • 5
  • You mean `self.ui.findChild(QLabel, talent_now).setText(talent[1])` doesn't work? – p-a-o-l-o Mar 21 '19 at 10:41
  • Give us a bit more info. Is this a regular method inside a class? In that case you're missing `self` as a parameter (`def set_value(self, talent):`) and as the instance of the ui (`self.ui.talent_now...`). The way you wrote it, your function `set_value` is in the global scope (thus expecting `ui` as being equivalent to `App.ui`, or anything you decide to call your UI instance) and you're calling it before its definition. Is this how you're trying to run your code? – jfaccioni Mar 21 '19 at 10:43

2 Answers2

2

You can use the QWidget::findChild method to find a specific object. It is useful when you need to search it recursively:

    label1 = QLabel()
    label1.setObjectName("power")
    label2 = QLabel()
    label2.setObjectName("status")
    label3 = QLabel()
    label3.setObjectName("info")

    w = QWidget()
    layout = QVBoxLayout(w)
    layout.addWidget(label1)
    layout.addWidget(label2)
    layout.addWidget(label3)

    label = w.findChild(QLabel, "power")
    label.setText("12")
    w.show()

Dimitry Ernot
  • 6,256
  • 2
  • 25
  • 37
0

You always can traverse layout items and check every widget objectName.

Is this a valid solution for your requirements ?

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QGridLayout, QLabel, QPushButton
from PyQt5.QtCore import pyqtSlot

if __name__ == '__main__':
    app = QApplication(sys.argv)

    label1 = QLabel()
    label1.setText('label1')
    label1.setObjectName('label1')

    label2 = QLabel()
    label2.setText('label2')
    label2.setObjectName('label2')

    btn = QPushButton("find label")

    layout = QGridLayout()
    layout.addWidget(label1, 0, 0)
    layout.addWidget(label2, 0, 1)
    layout.addWidget(btn, 1, 0, 1, 2)

    w = QWidget()
    w.setLayout(layout)
    w.resize(500, 400)
    w.show()

    def find_label(event):
        print('click')
        childs_count = layout.count()
        for c in range(childs_count):
            w = layout.itemAt(c)
            if w.widget().objectName() == 'label2':
                print('FOUND widget: {}'.format(w.widget().objectName()))

    btn.clicked.connect(find_label)

    sys.exit(app.exec())
jgoday
  • 2,768
  • 1
  • 18
  • 17