Skip to content

Commit

Permalink
Setting a property to null in collections removes property
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 23, 2017
1 parent 5e9f6ac commit c23de08
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
3 changes: 2 additions & 1 deletion python/core/qgsproperty.sip
Expand Up @@ -630,7 +630,8 @@ class QgsPropertyCollection
* @param key integer key for property. Any existing property with the same key will be deleted
* and replaced by this property. The intended use case is that a context specific enum is cast to
* int and used for the key value.
* @param property property to add. Ownership is transferred to the collection.
* @param property property to add. Ownership is transferred to the collection. Setting a property
* to null will remove the property from the collection.
*/
void setProperty( int key, QgsAbstractProperty* property /Transfer/ );

Expand Down
4 changes: 3 additions & 1 deletion src/core/qgsproperty.cpp
Expand Up @@ -724,7 +724,9 @@ void QgsPropertyCollection::setProperty( int key, QgsAbstractProperty* property
if ( hasProperty( key ) )
delete mProperties.take( key );

mProperties.insert( key, property );
if ( property )
mProperties.insert( key, property );

mDirty = true;
}

Expand Down
3 changes: 2 additions & 1 deletion src/core/qgsproperty.h
Expand Up @@ -718,7 +718,8 @@ class CORE_EXPORT QgsPropertyCollection
* @param key integer key for property. Any existing property with the same key will be deleted
* and replaced by this property. The intended use case is that a context specific enum is cast to
* int and used for the key value.
* @param property property to add. Ownership is transferred to the collection.
* @param property property to add. Ownership is transferred to the collection. Setting a property
* to null will remove the property from the collection.
*/
void setProperty( int key, QgsAbstractProperty* property );

Expand Down
6 changes: 6 additions & 0 deletions tests/src/core/testqgsproperty.cpp
Expand Up @@ -797,6 +797,12 @@ void TestQgsProperty::propertyCollection()
QCOMPARE( collection.count(), 2 );
QCOMPARE( collection.propertyKeys().toSet(), QSet<int>() << Property1 << Property3 );

//test removing a property
collection.setProperty( Property1, nullptr );
QVERIFY( !collection.property( Property1 ) );
QVERIFY( !collection.hasProperty( Property1 ) );
QCOMPARE( collection.propertyKeys(), QList<int>() << Property3 );

//clear
collection.clear();
QCOMPARE( collection.count(), 0 );
Expand Down

0 comments on commit c23de08

Please sign in to comment.