In addition to my previous question, I am struggling to implement the possibility to choose an attribute from a layer within QgsPropertyOverrideButton.
I have a combobox where one can choose a layer to use. Getting attributes and geometries from this selected layer works just fine, but it is not recognized by QgsPropertyOverrideButton when I use .setVectorLayer()
as described in docs. For example self.dlg.Isochrones_WalkSpeed_Override.setVectorLayer(selectedLayer)
A reduced code snipped:
layers = QgsProject.instance().layerTreeRoot().children() # Fetch the currently loaded layers
selectedLayerIndex = self.dlg.Isochrones_SelectInputLayer.currentIndex() # Fetch the selected layer in combobox
selectedLayer = layers[selectedLayerIndex].layer() # Use the in combobox selected layer
fieldnames = [field.name() for field in selectedLayer.fields()] # Receive fieldnames from selected layer
features = selectedLayer.getFeatures() # <-- Works just fine
self.dlg.Isochrones_WalkSpeed_Override.setVectorLayer(selectedLayer) # <-- Seems to not work
self.dlg.Isochrones_WalkSpeed_Override.updateFieldLists() # <-- Seems to not work
# Do some stuff here
for feature in features:
# retrieve every feature with its geometry and attributes
print("Feature ID: ", feature.id())
# fetch geometry
# show some information about the feature geometry
geom = feature.geometry()
pointgeom = geom.asPoint() #Read Point geometry
x = pointgeom.x() #Read X-Value
y = pointgeom.y() #Read Y-Value
print("PointX: ", x, " | PointY: ", y)
# fetch attributes
attrs = feature.attributes()
# attrs is a list. It contains all the attribute values of this feature
print(attrs)
#Check where to gather attributes from: GUI or Layer?
#WalkSpeed
if self.dlg.Isochrones_WalkSpeed_Override.isActive() == True:
Isochrones_WalkSpeed, IrrelevantSuccessStorage = self.dlg.Isochrones_WalkSpeed_Override.toProperty().value(QgsExpressionContext()) #Receiving Value from GUI: DataDefinedOverride
else:
Isochrones_WalkSpeed = self.dlg.Isochrones_WalkSpeed.value() #Receiving Value from GUI: SpinBox
# Do more stuff here
Why does .getFeatures()
recognize the layer but .setVectorLayer()
doesnt? And how would I need to write my code to make .setVectorLayer()
recognizing the selected layer, so users are able to choose an attribute from the selected layer within QgsPropertyOverrideButton?