Select to view content in your preferred language

How do I set an attribute value to NULL for a feature?

3697
2
Jump to solution
03-13-2019 11:49 AM
OrestHalustchak
Emerging Contributor

I'm having an issue when I try to set an attribute value to null in the process of adding a new feature to a feature table.

 

I'm using C++ Qt Runtime sdk 100.3.

 

I have a feature layer that is enabled for editing. The feature table has string and int attributes that are defined as Editable = yes and Allows Null Values = yes.

 

What I'm doing is this:

  • Create a new feature using ServiceFeatureTable::createFeature().
  • Create a QMap<QString, QVariant> of feature attributes.
  • Create a null QVariant value: QVariant qv = QVariant();
  • I verified that qv.IsNull() is true;
  • Add to the attributes list: featureAttributes.insert("MyAttrName", qv);
  • I used this for both string and int attributes.
  • Create a new feature: featureTable->createFeature(featureAttributes, geom, this);
  • If I go ahead and add that feature to the table and apply edits, then I see that the resulting attribute is not null but has a non-null empty string for string attributes and 0 for int attributes.
  • What I also found was that if right after creating the feature, I get the attributes back via the AttributeListModel, the attributes are no longer null.
  • AttributeListModel* alm = feature->attributes();
  • QVariant qv2 = alm->attributeValue(QString("MyAttrName"));
  • qDebug() << "qv2 is null =" << qv2.isNull();
  • This returns false. I expected it to return true.

 

If I leave out the attributes from the attribute list altogether, then I do get null values in the resulting feature in the table. However, that approach will not work if the attributes include default values that will set them to non-null values and that approach will not work for ServiceFeatureTable::updateFeature();

 

I need a way to set values to null explicitly.

 

Does anyone have suggestions as to what to try? I am probably missed something.

 

Regards.

0 Kudos
1 Solution

Accepted Solutions
JamesBallard1
Esri Regular Contributor

Orest Halustchak‌, yes this can be done. We make a distinction between invalid QVariant, for example if someone requested the value for a field that doesn't exist and legal, valid "null" values for fields whose value is actually "null". 

const QVariant jsonNull(QJsonValue()); // this is a "null" value.

It's explained here in more detail: AttributeListModel Class | ArcGIS for Developers 

Please let us know if that works for you.

View solution in original post

0 Kudos
2 Replies
JamesBallard1
Esri Regular Contributor

Orest Halustchak‌, yes this can be done. We make a distinction between invalid QVariant, for example if someone requested the value for a field that doesn't exist and legal, valid "null" values for fields whose value is actually "null". 

const QVariant jsonNull(QJsonValue()); // this is a "null" value.

It's explained here in more detail: AttributeListModel Class | ArcGIS for Developers 

Please let us know if that works for you.

0 Kudos
OrestHalustchak
Emerging Contributor

That was it. It worked.

Thanks!

Orest.

0 Kudos