1

I am new to Python and QGIS. I want to populate the comboBox with the first column of the table attribute of the park's layer

Code:

 #Populate park's comboBox
        park = layers[0].layer()
        park.startEditing()
        for p in park.getFeatures():
                self.dlg.comboBox_2.addItem(p.attributes()[0])

Error:

2022-01-03T01:39:23 WARNING Traceback (most recent call last):

File "C:/Users/ADS/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\urban_parks\urban_parks.py", line 217, in run

self.dlg.comboBox_2.addItem(p.attributes()[0]) TypeError: arguments did not match any overloaded call: addItem(self, str, userData: Any = None): argument 1 has unexpected type 'int'

addItem(self, QIcon, str, userData: Any = None): argument 1 has unexpected type 'int'

2
  • 4
    if you're just going to enumerate the park layer's features to populate the comboBox you don't need to call startEditing. In fact, you probably should avoid it.
    – Llaves
    Jan 3 at 3:36
  • Welcome to GIS SE! As a new user be sure to take the Tour to learn about our focussed Q&A format. You appear to have created two accounts which inevitably leads to a frustrating experience for you, potential answerers and reviewers so please follow these instructions ASAP to merge your accounts.
    – PolyGeo
    Jan 3 at 4:45

1 Answer 1

5

The problem is that the feature attributes which you are trying to add to your combo box are evidently of an integer datatype. The addItem() method of QComboBox expects a string argument.

Therefore, you need to cast the attribute value argument to a string:

...
for p in park.getFeatures():
    self.dlg.comboBox_2.addItem(str(p.attributes()[0]))
...
1
  • 2
    You will also need to remember that when you fetch the selected value from the combo box, it will be returned as a string. To be consistent with the type in the attribute, you will want to convert it back to an int using the int() function.
    – Llaves
    Jan 3 at 3:39

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.