9

I am creating a plugin in QGIS where I want to read a Vector layer --> Select attribute column --> list unique values within the chosen attribute column in the selected attribute from the column.

I have been successful with the two first steps, but not with the third.

This is the code used so far, but I cannot get further than this.

self.layerComboManagerPoint = QgsMapLayerComboBox(self.dlg.comboBoxVector)
self.layerComboManagerPoint.setCurrentIndex(-1)
self.layerComboManagerPoint.setFilters(QgsMapLayerProxyModel.PolygonLayer)
self.layerComboManagerPoint.setFixedWidth(175)
self.layerComboManagerPointField = QgsFieldComboBox(self.dlg.comboBoxField)
self.layerComboManagerPointField.setFilters(QgsFieldProxyModel.AllTypes) 
self.layerComboManagerPoint.layerChanged.connect(self.layerComboManagerPointField.setLayer)

I have tried googling, but I might just not have a reasonable word to google on.

enter image description here

Kadir Şahbaz
78.6k57 gold badges260 silver badges407 bronze badges
asked May 21, 2021 at 10:04

1 Answer 1

10

Add the highlighted lines to your script:

self.layerComboManagerPoint = QgsMapLayerComboBox(self.dlg.comboBoxVector)
self.layerComboManagerPoint.setCurrentIndex(-1)
self.layerComboManagerPoint.setFilters(QgsMapLayerProxyModel.PolygonLayer)
self.layerComboManagerPoint.setFixedWidth(175)
self.layerComboManagerPointField = QgsFieldComboBox(self.dlg.comboBoxField)
self.layerComboManagerPointField.setFilters(QgsFieldProxyModel.AllTypes) 
self.layerComboManagerPoint.layerChanged.connect(self.layerComboManagerPointField.setLayer)
################## ADD THESE LINES #########################
def field_changed(field):
 # get current layer
 layer = self.layerComboManagerPoint.currentLayer()
 # get index of the field
 i = layer.fields().indexOf(field)
 # get unique values
 unique_values = layer.uniqueValues(i) 
 # remove all values from comboBoxAttribute
 self.dlg.comboBoxAttribute.clear() 
 # add unique values
 self.dlg.comboBoxAttribute.addItems(unique_values) 
self.layerComboManagerPointField.fieldChanged.connect(field_changed) 
#############################################################
answered May 21, 2021 at 19:38
1
  • OK, it works fine, however, I also now understand that I need to be able to read other types than 'string'. I tried self.dlg.comboBoxAttributesetFilters(QgsMapLayerProxyModel.AllTypes) But with no luck. Do you have any solution to this too? Commented May 24, 2021 at 8:57

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.