I have displayed in a QListWidget several features ranked according to their values from a field named "CODE". Based on this list I would like to select a feature and after a click on a QpushButton to zoom to the extent of this feature in the canvas.
Here is my crude attempt (I am still a beginner):
def append_features_to_QlistWidget (self): #associated to QpushButton_1
#----------------function sample--------------------
self.layer = QgsProject.instance().mapLayersByName('my_layer')[0]
self.selection = []
for self.feat in self.layer.getFeatures():
self.attrs = self.feat.attributes()
self.code= self.attrs[0]
self.selection.append(self.code)
self.dlg.listWidget.addItems(self.selection)
def zoom (self): #associated to QpushButton_2
inds = [i.text() for i in self.dlg.listWidget.selectedItems()]
attribute_value = {}
for i in inds:
attribute_value[i] = [f for f in self.layer.getFeatures() if f["code"] == i][0]
self.layer.selectByIds(attribute_value)
iface.mapCanvas().zoomToSelected(self.layer)
##Other incomplete attempt:
# ids = [i.id() for i in self.selection]
# self.layer.selectByIds(ids)
#iface.mapCanvas().zoomToSelected(self.layer)