[Qgis-developer] Problem using QgsFeature.attributes() instead of QgsFeature.attributeMap() in QGIS 1.9

Alexandre Neto senhor.neto at gmail.com
Thu Mar 14 02:13:38 PDT 2013


Hello Martin,

Thanks for your answer. You are right, I was not doing it correctly in the
first place.

For 1.8 it "worked" cause I was able to swap each QgsField for a QVariant,
in 1.9 with the QgsFields that is not possible anymore.

So I have changed it for:

                    if QGis.QGIS_VERSION_INT < 10900:
                        new_attributes = feature.attributeMap()
                        for j in range(new_attributes.__len__()):
                            if not provider.defaultValue(j).isNull():
                                new_attributes[j] = provider.defaultValue(j)
                        temp_feature.setAttributeMap(new_attributes)
                    else:
                        new_attributes = feature.attributes()
                        for j in range(new_attributes.__len__()):
                            if not provider.defaultValue(j).isNull():
                                new_attributes[j] = provider.defaultValue(j)
                        temp_feature.setAttributes(new_attributes)

I don't like the repeated code, but I was not sure if checking for QGIS
version for each attribute was much better.

Thanks again, and sorry for the time wasted, I know you guys are quite busy.

Alexandre Neto

On Wed, Mar 13, 2013 at 8:48 PM, Martin Dobias <wonder.sk at gmail.com> wrote:

> Hi Alexandre
>
> On Tue, Mar 12, 2013 at 1:22 AM, Alexandre Neto <senhor.neto at gmail.com>
> wrote:
> > ...
> > new_attributes = layer.pendingFields()
> >
> > [...]
> >
> > But when using it in 1.9 I got the following error in the line:
> > new_attributes[j] = feature.attributes()[j]
> >
> > Traceback (most recent call last):
> >   File "<input>", line x, in <module>
> > TypeError: QgsFields.__setitem__(): argument 2 has unexpected type
> > 'QVariant'
>
> What are you actually trying to do?
>
> layer.pendingFields() returns QgsFields instance, which basically acts
> as a list of QgsField instances (attribute definitions). Then, you are
> trying to assign an attribute value (QVariant) to a field - that's
> clearly an error and it is reported correctly. Probably you want to
> copy the attributes to another feature?
>
> Martin
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20130314/905be53e/attachment.html>


More information about the Qgis-developer mailing list