0

I am developing a plugin in QGIS which involves the use of QgsFieldComboBox, what I want to do is that the combo box only shows fields(column names from attribute table), and from this QgsFieldComboBox i can select one field and then i wants to show features of that particular column in another combobox.

By using following code i unable to show feild names in combBox_2,and after selecting a particular field name unable to show features in comboBox_3.. please help.

def run(self):
    """Run method that performs all the real work"""
    if self.first_start == True:
        self.first_start = False
        self.dlg = GetMapDialog()
        # Connect signal to slot which will update comboBox_2 whenever selection in comboBox changes
        self.dlg.comboBox.layerChanged.connect(self.my_combobox_2)
        self.dlg.comboBox.layerChanged.connect(self.my_combobox_3)
        self.dlg.comboBox_2.fieldChanged.connect(self.my_combobox_3
       
    self.dlg.comboBox.setFilters(QgsMapLayerProxyModel.PolygonLayer)
    #self.dlg.comboBox_2.setFilters(QgsFieldProxyModel.AllTypes)

    self.my_combobox_2()
    self.my_combobox_3()
    # show the dialog
    self.dlg.show()

def my_combobox_2(self):
    self.dlg.comboBox_2.clear()
    selectedLayer = self.dlg.comboBox.currentLayer()
    #self.dlg.comboBox_2.addItems([field.name() for field in fields])
    if selectedLayer:
        self.dlg.comboBox_2.setFilters(QgsFieldProxyModel.AllTypes)

def my_combobox_3(self):
    self.dlg.comboBox_3.clear()
    selectedLayer = self.dlg.comboBox.currentLayer()
    selectedField = self.dlg.comboBox_2.currentField()
    featuredata=[]
    for feature in selectedLayer.getFeatures():
        featuredata.append(feature[selectedField])
    self.dlg.comboBox_3.addItems(featuredata)
3
  • here- comboBox is QgsMapLayerComboBox and comboBox_2 is QgsFeildComboBox.
    – raaj
    Commented Jul 11, 2022 at 6:06
  • Welcome to GIS SE! We're a little different from other sites; this isn't a discussion forum but a Q&A site. Your questions should as much as possible describe not just what you want to do, but precisely what you have tried and where you are stuck trying that. Please check out our short tour for more about how the site works
    – Ian Turton
    Commented Jul 11, 2022 at 7:29
  • Reading quickly through your code, it seems to make sense. What happens? Commented Jul 11, 2022 at 8:04

1 Answer 1

1

The basic idea is that you use the method setLayer() of QgsFieldComboBox inside the slot method which is called when the current layer in the map layer combo box changes. All fields of that layer will be loaded into the field combo box. The code below should be what you want.

def run(self):
    """Run method that performs all the real work"""
    # Create the dialog with elements (after translation) and keep reference
    # Only create GUI ONCE in callback, so that it will only load when the plugin is started
    if self.first_start == True:
        self.first_start = False
        self.dlg = GetMapDialog()
        # Connect signal to slot which will update comboBox_2 whenever selection in  layer comboBox changes
        self.dlg.comboBox.layerChanged.connect(self.my_combobox_2)
        # Connect signal to slot which will update comboBox_3 whenever selection in field comboBox changes
        self.dlg.comboBox_2.fieldChanged.connect(self.my_combobox_3)
    self.dlg.comboBox.setFilters(QgsMapLayerProxyModel.PolygonLayer)
    self.my_combobox_2()
    self.my_combobox_3()
    # show the dialog
    self.dlg.show()

def my_combobox_2(self):
    selectedLayer = self.dlg.comboBox.currentLayer()
    if selectedLayer:
        self.dlg.comboBox_2.setLayer(selectedLayer)
        self.dlg.comboBox_2.setFilters(QgsFieldProxyModel.AllTypes)


def my_combobox_3(self):
    self.dlg.comboBox_3.clear()
    selectedLayer = self.dlg.comboBox.currentLayer()
    currentField = self.dlg.comboBox_2.currentField()
    if selectedLayer:
        self.dlg.comboBox_3.addItems(sorted([str(i) for i in selectedLayer.uniqueValues(selectedLayer.fields().lookupField(currentField))]))
1
  • 1
    Its working.. thanks you..
    – raaj
    Commented Jul 11, 2022 at 9:28

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.