23 #include <QDataStream>    26 #include <QJsonDocument>    37   : mName( nam ), mType( typ ), mLength( len ), mPrecision( prec ), mNumeric( num )
    48                     const QString &
typeName, 
int len, 
int prec, 
const QString &comment,
    49                     QVariant::Type subType )
    51   d = 
new QgsFieldPrivate( name, type, subType, typeName, len, prec, comment );
    74   return *( other.d ) == *d;
    79   return !( *
this == other );
    89   if ( !d->alias.isEmpty() )
   127   return d->type == QVariant::Double || d->type == QVariant::Int || d->type == QVariant::UInt || d->type == QVariant::LongLong || d->type == QVariant::ULongLong;
   132   return d->type == QVariant::Date || d->type == QVariant::Time || d->type == QVariant::DateTime;
   177   return d->defaultValueDefinition;
   192   return d->constraints;
   219   if ( d->type == QVariant::Double )
   222     if ( QLocale().decimalPoint() != 
'.' ||
   223          !( QLocale().numberOptions() & QLocale::NumberOption::OmitGroupSeparator ) )
   225       if ( d->precision > 0 )
   227         return QLocale().toString( v.toDouble(), 
'f', d->precision );
   233         QString s( v.toString() );
   234         int dotPosition( s.indexOf( 
'.' ) );
   236         if ( dotPosition < 0 )
   242           precision = s.length() - dotPosition - 1;
   244         return QLocale().toString( v.toDouble(), 
'f', 
precision );
   248     else if ( d->type == QVariant::Double && d->precision > 0 )
   250       return QString::number( v.toDouble(), 
'f', d->precision );
   255             !( QLocale().numberOptions() & QLocale::NumberOption::OmitGroupSeparator ) )
   258     qlonglong converted( v.toLongLong( &ok ) );
   260       return QLocale().toString( converted );
   262   else if ( d->typeName.compare( QLatin1String( 
"json" ), Qt::CaseInsensitive ) == 0 || d->typeName == QLatin1String( 
"jsonb" ) )
   264     QJsonDocument doc = QJsonDocument::fromVariant( v );
   265     return QString::fromUtf8( doc.toJson().data() );
   267   else if ( d->type == QVariant::ByteArray )
   269     return QObject::tr( 
"BLOB" );
   285     v.convert( d->type );
   289   if ( d->type == QVariant::Int && v.toInt() != v.toLongLong() )
   291     v = QVariant( d->type );
   297   if ( d->type == QVariant::Double && v.type() == QVariant::String )
   300     if ( !tmp.convert( d->type ) )
   311       if ( QLocale().decimalPoint() != 
'.' )
   313         d = QLocale( QLocale::C ).toDouble( v.toString(), &ok );
   324   if ( d->type == QVariant::Int && v.type() == QVariant::String )
   327     if ( !tmp.convert( d->type ) )
   341   if ( d->type == QVariant::LongLong && v.type() == QVariant::String )
   344     if ( !tmp.convert( d->type ) )
   359   if ( d->type == QVariant::Int && v.canConvert( QVariant::Double ) )
   362     double dbl = v.toDouble( &ok );
   366       v = QVariant( d->type );
   370     double round = std::round( dbl );
   371     if ( round  > std::numeric_limits<int>::max() || round < -std::numeric_limits<int>::max() )
   374       v = QVariant( d->type );
   377     v = QVariant( static_cast< int >( std::round( dbl ) ) );
   382   if ( !v.convert( d->type ) )
   384     v = QVariant( d->type );
   388   if ( d->type == QVariant::Double && d->precision > 0 )
   390     double s = std::pow( 10, d->precision );
   391     double d = v.toDouble() * s;
   392     v = QVariant( ( d < 0 ? std::ceil( d - 0.5 ) : std::floor( d + 0.5 ) ) / s );
   396   if ( d->type == QVariant::String && d->length > 0 && v.toString().length() > d->length )
   398     v = v.toString().left( d->length );
   407   d->editorWidgetSetup = v;
   412   return d->editorWidgetSetup;
   424   out << static_cast< quint32 >( field.
type() );
   429   out << field.
alias();
   441   out << static_cast< quint32 >( field.
subType() );
   452   quint32 originNotNull;
   453   quint32 originUnique;
   454   quint32 originExpression;
   455   quint32 strengthNotNull;
   456   quint32 strengthUnique;
   457   quint32 strengthExpression;
   465   QString defaultValueExpression;
   466   QString constraintExpression;
   467   QString constraintDescription;
   469   in >> name >> type >> typeName >> length >> precision >> comment >> alias
   470      >> defaultValueExpression >> applyOnUpdate >> constraints >> originNotNull >> originUnique >> originExpression >> strengthNotNull >> strengthUnique >> strengthExpression >>
   471      constraintExpression >> constraintDescription >> 
subType;
   473   field.
setType( static_cast< QVariant::Type >( type ) );
   475   field.
setLength( static_cast< int >( length ) );
   483     fieldConstraints.
setConstraint( QgsFieldConstraints::ConstraintNotNull, static_cast< QgsFieldConstraints::ConstraintOrigin>( originNotNull ) );
   484     fieldConstraints.
setConstraintStrength( QgsFieldConstraints::ConstraintNotNull, static_cast< QgsFieldConstraints::ConstraintStrength>( strengthNotNull ) );
   487     fieldConstraints.
removeConstraint( QgsFieldConstraints::ConstraintNotNull );
   490     fieldConstraints.
setConstraint( QgsFieldConstraints::ConstraintUnique, static_cast< QgsFieldConstraints::ConstraintOrigin>( originUnique ) );
   491     fieldConstraints.
setConstraintStrength( QgsFieldConstraints::ConstraintUnique, static_cast< QgsFieldConstraints::ConstraintStrength>( strengthUnique ) );
   497     fieldConstraints.
setConstraint( QgsFieldConstraints::ConstraintExpression, static_cast< QgsFieldConstraints::ConstraintOrigin>( originExpression ) );
   498     fieldConstraints.
setConstraintStrength( QgsFieldConstraints::ConstraintExpression, static_cast< QgsFieldConstraints::ConstraintStrength>( strengthExpression ) );
   501     fieldConstraints.
removeConstraint( QgsFieldConstraints::ConstraintExpression );
   504   field.
setSubType( static_cast< QVariant::Type >( subType ) );
 bool isNumeric() const
Returns if this field is numeric. 
 
QgsField & operator=(const QgsField &other)
Assignment operator. 
 
void setConstraintStrength(Constraint constraint, ConstraintStrength strength)
Sets the strength of a constraint. 
 
QgsField(const QString &name=QString(), QVariant::Type type=QVariant::Invalid, const QString &typeName=QString(), int len=0, int prec=0, const QString &comment=QString(), QVariant::Type subType=QVariant::Invalid)
Constructor. 
 
QString comment() const
Returns the field comment. 
 
bool isDateOrTime() const
Returns if this field is a date and/or time type. 
 
ConstraintStrength constraintStrength(Constraint constraint) const
Returns the strength of a field constraint, or ConstraintStrengthNotSet if the constraint is not pres...
 
QString alias() const
Returns the alias for the field (the friendly displayed name of the field ), or an empty string if th...
 
The QgsDefaultValue class provides a container for managing client side default values for fields...
 
void setPrecision(int precision)
Set the field precision. 
 
double qgsPermissiveToDouble(QString string, bool &ok)
Converts a string to a double in a permissive way, e.g., allowing for incorrect numbers of digits bet...
 
QDataStream & operator>>(QDataStream &in, QgsField &field)
Reads a field from stream in into field. QGIS version compatibility is not guaranteed. 
 
void setDefaultValueDefinition(const QgsDefaultValue &defaultValueDefinition)
Sets an expression to use when calculating the default value for the field. 
 
bool convertCompatible(QVariant &v) const
Converts the provided variant to a compatible format. 
 
void setName(const QString &name)
Set the field name. 
 
QgsEditorWidgetSetup editorWidgetSetup() const
Gets the editor widget setup for the field. 
 
int precision() const
Gets the precision of the field. 
 
Stores information about constraints which may be present on a field. 
 
int qgsPermissiveToInt(QString string, bool &ok)
Converts a string to an integer in a permissive way, e.g., allowing for incorrect numbers of digits b...
 
QString name() const
Returns the name of the field. 
 
Field has an expression constraint set. See constraintExpression(). 
 
void setLength(int len)
Set the field length. 
 
QString typeName() const
Gets the field type. 
 
QString displayName() const
Returns the name to use when displaying this field. 
 
void setTypeName(const QString &typeName)
Set the field type. 
 
QString constraintDescription() const
Returns the descriptive name for the constraint expression. 
 
qlonglong qgsPermissiveToLongLong(QString string, bool &ok)
Converts a string to an qlonglong in a permissive way, e.g., allowing for incorrect numbers of digits...
 
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS. 
 
bool operator!=(const QgsField &other) const
 
QString displayString(const QVariant &v) const
Formats string for display. 
 
Encapsulate a field in an attribute table or data source. 
 
void setConstraint(Constraint constraint, ConstraintOrigin origin=ConstraintOriginLayer)
Sets a constraint on the field. 
 
void setSubType(QVariant::Type subType)
If the field is a collection, set its element's type. 
 
QgsFieldConstraints constraints
 
void setType(QVariant::Type type)
Set variant type. 
 
bool operator==(const QgsField &other) const
 
void setAlias(const QString &alias)
Sets the alias for the field (the friendly displayed name of the field ). 
 
void setConstraintExpression(const QString &expression, const QString &description=QString())
Set the constraint expression for the field. 
 
int length() const
Gets the length of the field. 
 
QVariant::Type subType() const
If the field is a collection, gets its element's type. 
 
ConstraintOrigin constraintOrigin(Constraint constraint) const
Returns the origin of a field constraint, or ConstraintOriginNotSet if the constraint is not present ...
 
const QgsFieldConstraints & constraints() const
Returns constraints which are present for the field. 
 
QString constraintExpression() const
Returns the constraint expression for the field, if set. 
 
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source. 
 
void setConstraints(const QgsFieldConstraints &constraints)
Sets constraints which are present for the field. 
 
void setComment(const QString &comment)
Set the field comment. 
 
QDataStream & operator<<(QDataStream &out, const QgsField &field)
Writes the field to stream out. QGIS version compatibility is not guaranteed. 
 
QgsDefaultValue defaultValueDefinition
 
void removeConstraint(Constraint constraint)
Removes a constraint from the field. 
 
QgsDefaultValue defaultValueDefinition() const
Returns the expression used when calculating the default value for the field. 
 
Field must have a unique value. 
 
void setEditorWidgetSetup(const QgsEditorWidgetSetup &v)
Set the editor widget setup for the field.