I have a Problem in this function:
def show_selected_button(self):
attrbs = required_attributes[self.modeComboBox.currentText()]
layers = QgsProject.instance().mapLayersByName(self.layerComboBox.currentText())
layer = layers[0]
layer.selectAll()
selected_features = layer.getSelectedFeatures()
data = []
for i in selected_features:
attribute_row = []
for a in attrbs:
attrb = i.attribute(name=a)
if a == "MITTLEREBR":
attrb = round(attrb, 2)
attribute_row.append(attrb)
laenge = i.geometry().length()
vol = laenge * i.attribute(name="DICKE") / 100 * i.attribute(name="MITTLEREBR")
attribute_row.append(round(laenge, 2))
attribute_row.append(round(vol, 2))
data.append(attribute_row)
attrbs.append("LÄNGE")
attrbs.append("VOLUMEN")
self.tableWidget.setRowCount(len(data))
self.tableWidget.setColumnCount(len(attrbs))
self.tableWidget.setHorizontalHeaderLabels(attrbs)
for i, x in enumerate(data):
for j, y in enumerate(x):
self.tableWidget.setItem(i, j, QtWidgets.QTableWidgetItem(str(y)))
When I execute this function for the first time (when I click the button), everything works fine but when I use one more time after the first usage, required_attributes["Strasse"]
includes the values "LÄNGE" and "VOLUMEN" that originally got added to attrbs
and never got added to the dictionary.
Content of required_attributes["Strasse"]
when function is run for the first time:
['EBENE', 'SCHICHTTYP', 'SCHICHTT01', 'SCHICHTT02', 'DICKE', 'MITTLEREBR']
Content of required_attributest["Strasse"]
when function is run a second time:
['EBENE', 'SCHICHTTYP', 'SCHICHTT01', 'SCHICHTT02', 'DICKE', 'MITTLEREBR', 'LÄNGE', 'VOLUMEN']
required_attributes
is a variable stored in a different file.
I don't know why this is happening, can anyone help me?
CodePudding user response:
By assigning to attrbs
value of required_attributes[self.modeComboBox.currentText()]
you've created a link to the same object in memory, so whenever you change attrbs
now you are changing list inside dictionary.
attrbs = required_attributes[self.modeComboBox.currentText()]