Skip to content

Commit

Permalink
Fix QgsFields::lookupField returns bad result for empty field name
Browse files Browse the repository at this point in the history
QString::isNull vs ::isEmpty strikes again!
  • Loading branch information
nyalldawson committed Nov 24, 2016
1 parent dccd00d commit 7e0cd9a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/core/qgsfields.cpp
Expand Up @@ -290,6 +290,9 @@ QIcon QgsFields::iconForField( int fieldIdx ) const

int QgsFields::lookupField( const QString& fieldName ) const
{
if ( fieldName.isEmpty() ) //shortcut
return -1;

for ( int idx = 0; idx < count(); ++idx )
{
if ( d->fields[idx].field.name() == fieldName )
Expand All @@ -305,7 +308,7 @@ int QgsFields::lookupField( const QString& fieldName ) const
for ( int idx = 0; idx < count(); ++idx )
{
QString alias = d->fields[idx].field.alias();
if ( !alias.isNull() && QString::compare( alias, fieldName, Qt::CaseInsensitive ) == 0 )
if ( !alias.isEmpty() && QString::compare( alias, fieldName, Qt::CaseInsensitive ) == 0 )
return idx;
}

Expand Down
4 changes: 4 additions & 0 deletions tests/src/core/testqgsfields.cpp
Expand Up @@ -335,8 +335,12 @@ void TestQgsFields::indexFromName()
QgsField field2( QStringLiteral( "testfield2" ) );
fields.append( field2 );
QgsField field3( QStringLiteral( "testfield3" ) );
field3.setAlias( QStringLiteral( "" ) );
fields.append( field3 );

QCOMPARE( fields.lookupField( QString( "" ) ), -1 );
QCOMPARE( fields.lookupField( QString() ), -1 );

QCOMPARE( fields.indexFromName( QString( "bad" ) ), -1 );
QCOMPARE( fields.lookupField( QString( "bad" ) ), -1 );
QCOMPARE( fields.indexFromName( QString( "testfield" ) ), 0 );
Expand Down

0 comments on commit 7e0cd9a

Please sign in to comment.