Skip to content

Commit 90e2c45

Browse files
committed
Add qgsVariantEqual for null-aware comparison
1 parent 27e4b19 commit 90e2c45

File tree

5 files changed

+44
-1
lines changed

5 files changed

+44
-1
lines changed

python/core/auto_generated/qgis.sip.in

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,17 @@ QVariant data types (such as strings, numeric values, dates and times)
187187
.. seealso:: :py:func:`qgsVariantGreaterThan`
188188
%End
189189

190+
bool qgsVariantEqual( const QVariant &lhs, const QVariant &rhs );
191+
%Docstring
192+
Compares two QVariant values and return whether they are equal, it takes into
193+
account null values.
194+
195+
:param lhs: first value
196+
@param rhs second value
197+
@return true if values are equal
198+
%End
199+
200+
190201
bool qgsVariantGreaterThan( const QVariant &lhs, const QVariant &rhs );
191202
%Docstring
192203
Compares two QVariant values and returns whether the first is greater than the second.

src/core/qgis.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,3 +306,8 @@ uint qHash( const QVariant &variant )
306306

307307
return std::numeric_limits<uint>::max();
308308
}
309+
310+
bool qgsVariantEqual( const QVariant &lhs, const QVariant &rhs )
311+
{
312+
return lhs.isNull() == rhs.isNull() && lhs == rhs;
313+
}

src/core/qgis.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,17 @@ CORE_EXPORT qlonglong qgsPermissiveToLongLong( QString string, bool &ok );
448448
*/
449449
CORE_EXPORT bool qgsVariantLessThan( const QVariant &lhs, const QVariant &rhs );
450450

451+
/**
452+
* Compares two QVariant values and return whether they are equal, it takes into
453+
* account null values.
454+
*
455+
* \param lhs first value
456+
* @param rhs second value
457+
* @return true if values are equal
458+
*/
459+
CORE_EXPORT bool qgsVariantEqual( const QVariant &lhs, const QVariant &rhs );
460+
461+
451462
/**
452463
* Compares two QVariant values and returns whether the first is greater than the second.
453464
* Useful for sorting lists of variants, correctly handling sorting of the various

src/gui/editorwidgets/qgsvaluerelationwidgetwrapper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ void QgsValueRelationWidgetWrapper::setValue( const QVariant &value )
185185
for ( int i = 0; i < mComboBox->count(); i++ )
186186
{
187187
QVariant v( mComboBox->itemData( i ) );
188-
if ( v.isNull() == value.isNull() && v == value )
188+
if ( qgsVariantEqual( v, value ) )
189189
{
190190
idx = i;
191191
break;

tests/src/core/testqgis.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class TestQgis : public QObject
4545
void qVariantCompare();
4646
void testQgsAsConst();
4747
void testQgsRound();
48+
void testQgsVariantEqual();
4849

4950
private:
5051
QString mReport;
@@ -359,6 +360,21 @@ void TestQgis::testQgsRound()
359360
QGSCOMPARENEAR( qgsRound( 9999999.87654321987654321, 14 ), 9999999.876543219876543, 0.00000000000001 );
360361
}
361362

363+
void TestQgis::testQgsVariantEqual()
364+
{
365+
QVariant lhs;
366+
QVariant rhs;
367+
368+
QVERIFY( lhs == rhs );
369+
lhs.setValue( 0 );
370+
QVERIFY( lhs != rhs );
371+
rhs.setValue( 0 );
372+
QVERIFY( lhs == rhs );
373+
lhs.setValue( 1.2345 );
374+
rhs.setValue( 1.2345 );
375+
QVERIFY( lhs == rhs );
376+
}
377+
362378

363379
QGSTEST_MAIN( TestQgis )
364380
#include "testqgis.moc"

0 commit comments

Comments
 (0)