Skip to content

Commit

Permalink
When field has no explicit type string set, use field type instead
Browse files Browse the repository at this point in the history
Avoids missing field type information in eg the horizontal header
tooltips in attribute table
  • Loading branch information
nyalldawson committed May 31, 2024
1 parent 8e26f40 commit 5481dab
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/core/qgsfield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ QString QgsField::displayNameWithAlias() const
QString QgsField::displayType( const bool showConstraints ) const
{
QString typeStr = typeName();
if ( typeStr.isEmpty() )
{
typeStr = QgsVariantUtils::typeToDisplayString( type(), subType() );
}

if ( length() > 0 && precision() > 0 )
typeStr += QStringLiteral( "(%1, %2)" ).arg( length() ).arg( precision() );
Expand Down
8 changes: 8 additions & 0 deletions tests/src/core/testqgsfield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,14 @@ void TestQgsField::displayType()
constraints.setConstraint( QgsFieldConstraints::ConstraintUnique );
field.setConstraints( constraints );
QCOMPARE( field.displayType( true ), QString( "numeric(20, 10) NULL UNIQUE" ) );

// test field without an explicit type name, we should use the field type
QgsField field2;
field2.setType( QMetaType::Type::QString );
QCOMPARE( field2.displayType( false ), QString( "Text (string)" ) );
QCOMPARE( field2.displayType( true ), QString( "Text (string) NULL" ) );
field2.setType( QMetaType::Type::Int );
QCOMPARE( field2.displayType( false ), QString( "Integer (32 bit)" ) );
}

void TestQgsField::friendlyTypeString()
Expand Down

0 comments on commit 5481dab

Please sign in to comment.