Unset attribute values are always compatible with all field types

Refs #38913
This commit is contained in:
Nyall Dawson 2025-02-06 11:46:21 +10:00
parent db546ff142
commit cc327f8503
2 changed files with 13 additions and 0 deletions

View File

@ -20,6 +20,7 @@
#include "qgsapplication.h"
#include "qgsreferencedgeometry.h"
#include "qgsvariantutils.h"
#include "qgsunsetattributevalue.h"
#include <QDataStream>
#include <QIcon>
@ -482,6 +483,11 @@ bool QgsField::convertCompatible( QVariant &v, QString *errorMessage ) const
return true;
}
if ( v.userType() == qMetaTypeId< QgsUnsetAttributeValue >() )
{
return true;
}
if ( d->type == QMetaType::Type::Int && v.toInt() != v.toLongLong() )
{
v = QgsVariantUtils::createNullVariant( d->type );

View File

@ -26,6 +26,7 @@
#include "qgsapplication.h"
#include "qgstest.h"
#include "qgsreferencedgeometry.h"
#include "qgsunsetattributevalue.h"
class TestQgsField : public QObject
{
@ -603,6 +604,9 @@ void TestQgsField::convertCompatible()
QVERIFY( stringField.convertCompatible( nullDouble ) );
QCOMPARE( static_cast<QMetaType::Type>( nullDouble.userType() ), QMetaType::Type::QString );
QVERIFY( nullDouble.isNull() );
QVariant unsetValue = QgsUnsetAttributeValue( QStringLiteral( "Autonumber" ) );
QVERIFY( stringField.convertCompatible( unsetValue ) );
QCOMPARE( static_cast<QMetaType::Type>( unsetValue.userType() ), qMetaTypeId< QgsUnsetAttributeValue >() );
//test double
const QgsField doubleField( QStringLiteral( "double" ), QMetaType::Type::Double, QStringLiteral( "double" ) );
@ -636,6 +640,9 @@ void TestQgsField::convertCompatible()
QVERIFY( doubleField.convertCompatible( nullDouble ) );
QCOMPARE( static_cast<QMetaType::Type>( nullDouble.userType() ), QMetaType::Type::Double );
QVERIFY( nullDouble.isNull() );
unsetValue = QgsUnsetAttributeValue( QStringLiteral( "Autonumber" ) );
QVERIFY( doubleField.convertCompatible( unsetValue ) );
QCOMPARE( static_cast<QMetaType::Type>( unsetValue.userType() ), qMetaTypeId< QgsUnsetAttributeValue >() );
//test special rules