2

I have a layer of districts. Each district belongs to a different state. I want to write code that once you choose a state in the 2end combobox, you can only see districts from that certain state in the 3rd combobox. I wrote the following code:

       def myFunction(self, layer, featureSet2):
    Feature_list3 = []
    del Feature_list3[0:len(Feature_list3)]
    indx = self.dlg.comboBox.currentIndex()
    Data = featureSet2[indx]
    layer.removeSelection()

    # Fill 3rd combobox
    self.dlg.comboBox_3.clear()
    for feature in layer.getFeatures():
        name = feature["NAME_2"]
        if Data==feature["TYPE_2"]:
            #print (Data)
            Feature_list3.append(name)

    self.dlg.comboBox_3.addItems(Feature_list3)
    # Fill 3rd combobox



def run(self):
    """Run method that performs all the real work"""
    layer = iface.addVectorLayer("C:/Users/carmel.han/Documents/QGIS/qgis_sample_data/shapefiles/regions.shp",
                                 "regions", "ogr")

    Feature_list = []
    Feature_list2 = []

    selection = []

#Fill 1st combobox
    self.dlg.comboBox.clear()
    for feature in layer.getFeatures():
        name = feature["name_1"]

        Feature_list.append(name)
    featureSet = set(Feature_list)
    self.dlg.comboBox.addItems(featureSet)
# Fill 1st combobox

# Fill 2end combobox
    Feature_list2 = []
    self.dlg.comboBox_2.clear()
    for feature in layer.getFeatures():
        name = feature["TYPE_2"]

        Feature_list2.append(name)
    featureSet2 = set(Feature_list2)
    self.dlg.comboBox_2.addItems(featureSet2)
    Feature_set2list2 = list(featureSet2)
# Fill 2end combobox

    slotLambda = lambda: self.myFunction(layer, Feature_set2list2)
    self.dlg.comboBox_2.currentIndexChanged.connect(slotLambda)

The if statment does not seem to work,it only gives me the districts of the first state, and does not change after further changes. can somebody point out the problem or show me how it's done?

1

Below is an example of how to do this using two functions. One for populating the first combobox. Another that you connect to the currentIndexChanged signal of the first combobox that populates the second combobox with values depending on the first combobox (yes, I like to make complicated sentences...):

def populate_combobox1(self):
    #clear combobox 1 before populate it
    self.dlg.combobox1.clear()
    #retrieve the list to populate the combobox (unique values of field 'name_1')
    values = layer.uniqueValues(layerfieldNameIndex('name_1'))
    self.dlg.combobox1.addItems(values)

def on_combobox1_change(self):
    #clear combobox 2 before populate it
    self.dlg.combobox2.clear()
    # retrieve the values to populate combobox2
      # get the current value of combobox 1
    filter = self.dlg.combobox1.currentText()
      # get the corresponding values of TYPE_2 field to populate combobox2
    values = [feat['TYPE_2'] for feat in layer.getFeatures() if feat['name_1'] == filter]
    self.dlg.combobox2.addItems(values)


def run():
    #populate the first combobox
    self.populate_combobox1()
    #connect the function on the change of current index in combobox 1
    self.dlg.combobox1.currentIndexChanged.connect(self.on_combobox1_change)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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