Skip to content

Commit c23de08

Browse files
committed
Setting a property to null in collections removes property
1 parent 5e9f6ac commit c23de08

4 files changed

Lines changed: 13 additions & 3 deletions

File tree

‎python/core/qgsproperty.sip‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,8 @@ class QgsPropertyCollection
630630
* @param key integer key for property. Any existing property with the same key will be deleted
631631
* and replaced by this property. The intended use case is that a context specific enum is cast to
632632
* int and used for the key value.
633-
* @param property property to add. Ownership is transferred to the collection.
633+
* @param property property to add. Ownership is transferred to the collection. Setting a property
634+
* to null will remove the property from the collection.
634635
*/
635636
void setProperty( int key, QgsAbstractProperty* property /Transfer/ );
636637

‎src/core/qgsproperty.cpp‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,9 @@ void QgsPropertyCollection::setProperty( int key, QgsAbstractProperty* property
724724
if ( hasProperty( key ) )
725725
delete mProperties.take( key );
726726

727-
mProperties.insert( key, property );
727+
if ( property )
728+
mProperties.insert( key, property );
729+
728730
mDirty = true;
729731
}
730732

‎src/core/qgsproperty.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,8 @@ class CORE_EXPORT QgsPropertyCollection
718718
* @param key integer key for property. Any existing property with the same key will be deleted
719719
* and replaced by this property. The intended use case is that a context specific enum is cast to
720720
* int and used for the key value.
721-
* @param property property to add. Ownership is transferred to the collection.
721+
* @param property property to add. Ownership is transferred to the collection. Setting a property
722+
* to null will remove the property from the collection.
722723
*/
723724
void setProperty( int key, QgsAbstractProperty* property );
724725

‎tests/src/core/testqgsproperty.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,12 @@ void TestQgsProperty::propertyCollection()
797797
QCOMPARE( collection.count(), 2 );
798798
QCOMPARE( collection.propertyKeys().toSet(), QSet<int>() << Property1 << Property3 );
799799

800+
//test removing a property
801+
collection.setProperty( Property1, nullptr );
802+
QVERIFY( !collection.property( Property1 ) );
803+
QVERIFY( !collection.hasProperty( Property1 ) );
804+
QCOMPARE( collection.propertyKeys(), QList<int>() << Property3 );
805+
800806
//clear
801807
collection.clear();
802808
QCOMPARE( collection.count(), 0 );

0 commit comments

Comments
 (0)