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月03日T01: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'

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jan 3, 2022 at 2:01
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. Commented Jan 3, 2022 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. Commented Jan 3, 2022 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]))
...
answered Jan 3, 2022 at 3:23
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. Commented Jan 3, 2022 at 3:39

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.