Skip to content

Commit

Permalink
Add qgsVariantEqual for null-aware comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Oct 1, 2018
1 parent 27e4b19 commit 90e2c45
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 1 deletion.
11 changes: 11 additions & 0 deletions python/core/auto_generated/qgis.sip.in
Expand Up @@ -187,6 +187,17 @@ QVariant data types (such as strings, numeric values, dates and times)
.. seealso:: :py:func:`qgsVariantGreaterThan`
%End

bool qgsVariantEqual( const QVariant &lhs, const QVariant &rhs );
%Docstring
Compares two QVariant values and return whether they are equal, it takes into
account null values.

:param lhs: first value
@param rhs second value
@return true if values are equal
%End


bool qgsVariantGreaterThan( const QVariant &lhs, const QVariant &rhs );
%Docstring
Compares two QVariant values and returns whether the first is greater than the second.
Expand Down
5 changes: 5 additions & 0 deletions src/core/qgis.cpp
Expand Up @@ -306,3 +306,8 @@ uint qHash( const QVariant &variant )

return std::numeric_limits<uint>::max();
}

bool qgsVariantEqual( const QVariant &lhs, const QVariant &rhs )
{
return lhs.isNull() == rhs.isNull() && lhs == rhs;
}
11 changes: 11 additions & 0 deletions src/core/qgis.h
Expand Up @@ -448,6 +448,17 @@ CORE_EXPORT qlonglong qgsPermissiveToLongLong( QString string, bool &ok );
*/
CORE_EXPORT bool qgsVariantLessThan( const QVariant &lhs, const QVariant &rhs );

/**
* Compares two QVariant values and return whether they are equal, it takes into
* account null values.
*
* \param lhs first value
* @param rhs second value
* @return true if values are equal
*/
CORE_EXPORT bool qgsVariantEqual( const QVariant &lhs, const QVariant &rhs );


/**
* Compares two QVariant values and returns whether the first is greater than the second.
* Useful for sorting lists of variants, correctly handling sorting of the various
Expand Down
2 changes: 1 addition & 1 deletion src/gui/editorwidgets/qgsvaluerelationwidgetwrapper.cpp
Expand Up @@ -185,7 +185,7 @@ void QgsValueRelationWidgetWrapper::setValue( const QVariant &value )
for ( int i = 0; i < mComboBox->count(); i++ )
{
QVariant v( mComboBox->itemData( i ) );
if ( v.isNull() == value.isNull() && v == value )
if ( qgsVariantEqual( v, value ) )
{
idx = i;
break;
Expand Down
16 changes: 16 additions & 0 deletions tests/src/core/testqgis.cpp
Expand Up @@ -45,6 +45,7 @@ class TestQgis : public QObject
void qVariantCompare();
void testQgsAsConst();
void testQgsRound();
void testQgsVariantEqual();

private:
QString mReport;
Expand Down Expand Up @@ -359,6 +360,21 @@ void TestQgis::testQgsRound()
QGSCOMPARENEAR( qgsRound( 9999999.87654321987654321, 14 ), 9999999.876543219876543, 0.00000000000001 );
}

void TestQgis::testQgsVariantEqual()
{
QVariant lhs;
QVariant rhs;

QVERIFY( lhs == rhs );
lhs.setValue( 0 );
QVERIFY( lhs != rhs );
rhs.setValue( 0 );
QVERIFY( lhs == rhs );
lhs.setValue( 1.2345 );
rhs.setValue( 1.2345 );
QVERIFY( lhs == rhs );
}


QGSTEST_MAIN( TestQgis )
#include "testqgis.moc"

0 comments on commit 90e2c45

Please sign in to comment.