-1

My goal is to assign the values of all QLineEdits, and QSpinBox's to a dictionary object such as:

editable_objects = {
"lineEdit_1" : "Value_1",
"QSpinBox_1" : "1234"
}

Is there a function to get a list of these objects?

The purpose of this is to create a JSON data file to load, and save settings, as well as record all settings data for a report.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
SubZero
  • 123
  • 1
  • 7

1 Answers1

0

The answer is to use the findChildren() method.

Assuming your GUI is in a class object which is typical for GUI

You can call self.findChildren(QObject) where QObject in this case would be the type of object you are looking for.

If you are looking for QSpinBox objects then you would call self.findChildren(QSpinBox). This returns a generator object that you would then loop over.

Next - How do we get the object names now that we have the objects?

children = self.findChildren(QSpinBox)
for child in children:
    print(child.objectName())

What we are now doing is accessing each object "child" with method objectName() which will return the name set in QtDesigner such as spinBox_1, spinBox_2, etc.

This can then be extended to get our values, child.value() for example.

See also: https://doc.qt.io/qt-5/qobject.html#findChildren

SubZero
  • 123
  • 1
  • 7