Skip to content
Permalink
Browse files

Fix QgsFields::lookupField returns bad result for empty field name

QString::isNull vs ::isEmpty strikes again!
  • Loading branch information
nyalldawson committed Nov 24, 2016
1 parent dccd00d commit 7e0cd9af3dfb3e0ef08a465d3c9f47639a8030d2
Showing with 8 additions and 1 deletion.
  1. +4 −1 src/core/qgsfields.cpp
  2. +4 −0 tests/src/core/testqgsfields.cpp
@@ -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 )
@@ -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;
}

@@ -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 );

0 comments on commit 7e0cd9a

Please sign in to comment.
You can’t perform that action at this time.