How to replace QWidget name dynamically with variable
-
i am working on little qgis extension with a better ui for the users and i am stuck on the QWidgets and getting the information i want into them
So basically i am trying to simplify my stupidly (but working) repetitive code with some loops. i am trying to replace the QWidget names with a variable[i] in my looplayer = self.iface.activeLayer() selected_features = layer.selectedFeatures() for feature in selected_features: feature_values = feature.attributes() feature_names = feature.fields() attributes = feature_names.names() i = 0 for attribute in attributes: print(attribute + " " + str(feature_values[i])) #print name and value of a feature into the qgis console for reference try: #problematic part test = "self.dlg."+attribute+".setText(str("+feature_values[i]+"))" print (test) except: print("QWidget does not exist") else: print("QWidget does exist")
currently i fill the values into QWidgets like this...
self.dlg.name1.setText(str(feature_value["name1"])) self.dlg.name2.setText(str(feature_value["name2"])) ...
and want to replace it with something like:
self.dlg.variable[i].setText(str(feature_value["name1"])) self.dlg.variable[i].setText(str(feature_value["name2"])) ...
i hope i made the point, where i am stuck at, clear
Thanks in advance :)
-
@qtnoob420 As I said: use real name of the attribute, not the word "attribute". Example: if you have attribute called "a" then do:
a_dict[attribute] = self.dlg.a
But it seems that you only have strings containing attribute names, right?
Then you could do it like this:f = getattr(self.dlg, attribute) f() # Here we call attribute
You do not even need a dictionary.
One example for string in Python:s="abc" f=getattr(s, "upper") f() # prints ABC
-
Hi
Cant you just put
self.dlg.name1
self.dlg.name2
etc
into a list ?You can also build this list automatically from DLG
https://doc.qt.io/qt-5/qobject.html#findChildren -
@qtnoob420 If you want to access your widgets using a string then you could simply put your widgets in a dictionary. This would be way faster than using findChildren.
-
@qtnoob420
HiSomething like
a_dict = dict()
a_dict["name"] = self.dlg.name1
a_dict["name3"] = self.dlg.name2and you can look up the self.dlg.name1 widget with "name"
"name" is the actual name of the widget. (objectname)
-
@qtnoob420
Python has adict
type, to store an arbitrary set of key-value pairs.If you want to do as @jsulm said and "access your widgets using a string" you could do something like:
widgets = dict() widgets["name1"] = self.dlg.name1 widgets["name2"] = self.dlg.name2 ...
You can then access, say,
self.dlg.name2
viawidgets["name2"]
. You can also iterate through all of them, keys and values, viafor key in widgets: print(key, '->', widgets[key])
If you don't want key names you can of course do similar by just storing them in a list/array instead of a dictionary.
-
Hi,
There might be some stuff doable with some Python introspection or even just using QObject's objectName method.
Can you show the code of the class of your self.dlg variable ?
-
@qtnoob420 said in How to replace QWidget name dynamically with variable:
a_dict["name"]
What did you store there? Can you show how (and what) you set it (a_dict["name"]=...).
What does type(a_dict["name"]) print out?
It seems you put a string there... -
@jsulm oh youre right. but idk how i could assign them in an other way:
i assigned them like:
a_dict[attribute] = "self.dlg."+attribute
attribute
is the name of the attributes from QGIS. and i named my QWidgets in a similar way to match them. -
@qtnoob420 Well, you are assigning a string. So, how should that work?
Why don't you assign self.dlg.attribute? Where attribute is the name of the attribute.
Also, what type is self.dlg? -
@jsulm it makes completely sense, i just dont know how :(
i tried stuff like...
a_dict[attribute] = self.dlg.attribute AttributeError: 'BaumkatasterDialog' object has no attribute 'attribute' feature_dict[attribute] = self.dlg.+attribute SyntaxError: invalid syntax
... which doesnt work either. i just dont know no better
i used the PluginBuilder3 so it was created by the plugin. i can access all of my UI Widgets from the QtDesigner with "self.dlg.widget_name"
-
@qtnoob420 As I said: use real name of the attribute, not the word "attribute". Example: if you have attribute called "a" then do:
a_dict[attribute] = self.dlg.a
But it seems that you only have strings containing attribute names, right?
Then you could do it like this:f = getattr(self.dlg, attribute) f() # Here we call attribute
You do not even need a dictionary.
One example for string in Python:s="abc" f=getattr(s, "upper") f() # prints ABC