Skip to content

Commit 3cd3e5d

Browse files
committed
Allow storing QgsProperty in QVariant
1 parent 499a35d commit 3cd3e5d

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

python/core/qgsproperty.sip

+2
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ class QgsProperty
137137

138138
bool convertToTransformer();
139139

140+
operator QVariant() const;
141+
140142
};
141143

142144

src/core/qgsproperty.h

+8
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,12 @@ class CORE_EXPORT QgsProperty
434434
*/
435435
bool convertToTransformer();
436436

437+
//! Allows direct construction of QVariants from properties.
438+
operator QVariant() const
439+
{
440+
return QVariant::fromValue( *this );
441+
}
442+
437443
private:
438444

439445
mutable QExplicitlySharedDataPointer<QgsPropertyPrivate> d;
@@ -446,4 +452,6 @@ class CORE_EXPORT QgsProperty
446452

447453
};
448454

455+
Q_DECLARE_METATYPE( QgsProperty )
456+
449457
#endif // QGSPROPERTY_H

tests/src/core/testqgsproperty.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ class TestQgsProperty : public QObject
9292
void propertyCollection(); //test for QgsPropertyCollection
9393
void collectionStack(); //test for QgsPropertyCollectionStack
9494
void curveTransform();
95+
void asVariant();
9596

9697
private:
9798

@@ -1764,6 +1765,20 @@ void TestQgsProperty::curveTransform()
17641765
QGSCOMPARENEAR( dest2.y( 0.5 ), 0.1, 0.638672 );
17651766
}
17661767

1768+
void TestQgsProperty::asVariant()
1769+
{
1770+
QgsProperty original = QgsProperty::fromField( QStringLiteral( "field1" ), true );
1771+
1772+
//convert to and from a QVariant
1773+
QVariant var = QVariant::fromValue( original );
1774+
QVERIFY( var.isValid() );
1775+
1776+
QgsProperty fromVar = qvariant_cast<QgsProperty>( var );
1777+
QCOMPARE( fromVar.propertyType(), QgsProperty::FieldBasedProperty );
1778+
QVERIFY( fromVar.isActive() );
1779+
QCOMPARE( fromVar.field(), QStringLiteral( "field1" ) );
1780+
}
1781+
17671782
void TestQgsProperty::checkCurveResult( const QList<QgsPoint> &controlPoints, const QVector<double> &x, const QVector<double> &y )
17681783
{
17691784
// build transform

0 commit comments

Comments
 (0)