From c23de083ce854db9d293da0596621d3993be4f04 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Thu, 17 Mar 2016 17:36:13 +1100 Subject: [PATCH] Setting a property to null in collections removes property --- python/core/qgsproperty.sip | 3 ++- src/core/qgsproperty.cpp | 4 +++- src/core/qgsproperty.h | 3 ++- tests/src/core/testqgsproperty.cpp | 6 ++++++ 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/python/core/qgsproperty.sip b/python/core/qgsproperty.sip index 78184b2f88a..a8927b98655 100644 --- a/python/core/qgsproperty.sip +++ b/python/core/qgsproperty.sip @@ -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/ ); diff --git a/src/core/qgsproperty.cpp b/src/core/qgsproperty.cpp index b3334e67a1a..d8cefeada0c 100644 --- a/src/core/qgsproperty.cpp +++ b/src/core/qgsproperty.cpp @@ -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; } diff --git a/src/core/qgsproperty.h b/src/core/qgsproperty.h index 630d508f7ec..dfa1a2e8dbe 100644 --- a/src/core/qgsproperty.h +++ b/src/core/qgsproperty.h @@ -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 ); diff --git a/tests/src/core/testqgsproperty.cpp b/tests/src/core/testqgsproperty.cpp index 7c19d5ad345..ce47353cec9 100644 --- a/tests/src/core/testqgsproperty.cpp +++ b/tests/src/core/testqgsproperty.cpp @@ -797,6 +797,12 @@ void TestQgsProperty::propertyCollection() QCOMPARE( collection.count(), 2 ); QCOMPARE( collection.propertyKeys().toSet(), QSet() << Property1 << Property3 ); + //test removing a property + collection.setProperty( Property1, nullptr ); + QVERIFY( !collection.property( Property1 ) ); + QVERIFY( !collection.hasProperty( Property1 ) ); + QCOMPARE( collection.propertyKeys(), QList() << Property3 ); + //clear collection.clear(); QCOMPARE( collection.count(), 0 );