Skip to content
Permalink
Browse files

Fix storing string representations of doubles in an int field

results in NULL rather than converting value to int

(cherry-picked from 1c76b93)
  • Loading branch information
nyalldawson committed Nov 19, 2015
1 parent 3ffdb6c commit ca127f58ae5146a916399c601c2c7c9e6e57715a
Showing with 25 additions and 0 deletions.
  1. +25 −0 src/core/qgsfield.cpp
@@ -18,6 +18,7 @@

#include <QSettings>
#include <QtCore/qmath.h>
#include "qgis.h"

#if 0
QgsField::QgsField( QString nam, QString typ, int len, int prec, bool num,
@@ -143,6 +144,30 @@ bool QgsField::convertCompatible( QVariant& v ) const
return false;
}

//String representations of doubles in QVariant will return false to convert( QVariant::Int )
//work around this by first converting to double, and then checking whether the double is convertible to int
if ( mType == QVariant::Int && v.canConvert( QVariant::Double ) )
{
bool ok = false;
double dbl = v.toDouble( &ok );
if ( !ok )
{
//couldn't convert to number
v = QVariant( mType );
return false;
}

double round = qgsRound( dbl );
if ( round > INT_MAX || round < -INT_MAX )
{
//double too large to fit in int
v = QVariant( mType );
return false;
}
v = QVariant( qRound( dbl ) );
return true;
}

if ( !v.convert( mType ) )
{
v = QVariant( mType );

0 comments on commit ca127f5

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