I'm writing a plugin that has a form with a combo box that I fill with the unique values in a field of a vector layer in the project.
The problem is that there are NULL values in that column and I get this error when I run the code:
TypeError: index 0 has type 'QVariant' but 'str' is expected
I'm trying with this code, but I still get the same error:
layer = QgsProject.instance().mapLayersByName('MyLayer')[0]
fieldsLayer = layer.fields()
idxField = fieldsLayer.indexFromName('MyField')
unique_values = layer.uniqueValues(idxField)
combovalues = []
for value in unique_values:
if value == 'NULL':
combovalues.append('')
else:
combovalues.append(value)
self.dlg.cmbRuta.addItems(combovalues)