I have a ComboBox shows feature ids of selected features in a layer. I am trying to select one feature using ids in ComboBox currentIndex. I used setSelectedFeature(), but it didn't worked.I can't figure out how to get it.

Here is my code:

layer = iface.activeaLayer()
feats = layer.selectedFeatures()
for feat in feats:
    name = feat["id"]
    self.dlg.cb_Feature.addItem(name, feat.id())
    onefeat = self.dlg.cb_Feature.itemData(self.dlg.cb_Feature.currentIndex())
  • Can you clarify more what you are trying to do? You have a set of selected features from which your populating a combo box by the selected feature id, then you want to only select the feature from the current value selected in the combo box?
    – artwork21
    Commented Aug 13, 2018 at 11:16
  • yes thats right
    – Virat ABD
    Commented Aug 13, 2018 at 11:18
  • From API docs, it's better to use selectByIds() method instead of setSelectedFeatures(). These methods expects a list of feature to select so you need to put your feature in a list even if you just have one feature...
    – YoLecomte
    Commented Aug 13, 2018 at 11:49

1 Answer 1


I think your trying to do too much in the one loop. Try splitting out the logic like this:

First within the plugin run() function make the function call to the function for populating the combobox based on feature selection. This will populate the combo box each time the form is opened based on features selected in the map. Then, in the initGui() function here is where you need the event listener for the index change on the combo box which then calls the selectFeatures() function, see code snippet below.

def selectFeatures(self):
    # continue with feature selection logic

def populateCombo(self):
    selectedFeatureID = []
    layer = iface.activeaLayer()
    feats = layer.selectedFeatures()
    for feat in feats:
        name = feat["id"]

def initGui(self):

def run(self):

To get the combo box current text see:

How do you get the current text contents of a QComboBox?

To select feature based on attribute see:

How to select features in QGIS using python

  • getting error like 'TypeError: arguments did not match any overloaded call: QComboBox.addItem(QString, QVariant userData=QVariant()): argument 1 has unexpected type 'list''
    – Virat ABD
    Commented Aug 13, 2018 at 12:07
  • Updated answer, it needed changed to addItems()
    – artwork21
    Commented Aug 13, 2018 at 12:11
  • got it, but how can i select the particular feature with combobox current value(id)
    – Virat ABD
    Commented Aug 13, 2018 at 12:13
  • Updated answer with some additional q/a links.
    – artwork21
    Commented Aug 13, 2018 at 12:24
  • still getting the same issue
    – Virat ABD
    Commented Aug 14, 2018 at 9:55

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.