QGIS API Documentation 3.99.0-Master (21b3aa880ba)
Loading...
Searching...
No Matches
QgsField Class Reference

Encapsulate a field in an attribute table or data source. More...

#include <qgsfield.h>

Public Member Functions

 QgsField (const QgsField &other)
Q_DECL_DEPRECATED QgsField (const QString &name, QVariant::Type type, const QString &typeName=QString(), int len=0, int prec=0, const QString &comment=QString(), QVariant::Type subType=QVariant::Invalid)
 Constructor.
 QgsField (const QString &name=QString(), QMetaType::Type type=QMetaType::Type::UnknownType, const QString &typeName=QString(), int len=0, int prec=0, const QString &comment=QString(), QMetaType::Type subType=QMetaType::Type::UnknownType)
 Constructor.
virtual ~QgsField ()
QString alias () const
 Returns the alias for the field (the friendly displayed name of the field ), or an empty string if there is no alias.
QString comment () const
 Returns the field comment.
Qgis::FieldConfigurationFlags configurationFlags () const
 Returns the Flags for the field (searchable, …).
const QgsFieldConstraintsconstraints () const
 Returns constraints which are present for the field.
bool convertCompatible (QVariant &v, QString *errorMessage=nullptr) const
 Converts the provided variant to a compatible format.
QgsDefaultValue defaultValueDefinition () const
 Returns the expression used when calculating the default value for the field.
QString displayName () const
 Returns the name to use when displaying this field.
QString displayNameWithAlias () const
 Returns the name to use when displaying this field and adds the alias in parenthesis if it is defined.
QString displayString (const QVariant &v) const
 Formats string for display.
QString displayType (bool showConstraints=false) const
 Returns the type to use when displaying this field, including the length and precision of the datatype if applicable.
Qgis::FieldDuplicatePolicy duplicatePolicy () const
 Returns the field's duplicate policy, which indicates how field values should be handled during a duplicate operation.
QgsEditorWidgetSetup editorWidgetSetup () const
 Gets the editor widget setup for the field.
QString friendlyTypeString () const
 Returns a user friendly, translated representation of the field type.
bool isDateOrTime () const
 Returns if this field is a date and/or time type.
bool isNumeric () const
 Returns if this field is numeric.
bool isReadOnly () const
 Returns true if this field is a read-only field.
int length () const
 Gets the length of the field.
Qgis::FieldDomainMergePolicy mergePolicy () const
 Returns the field's merge policy, which indicates how field values should be handled during a merge operation.
QMap< int, QVariant > metadata () const
 Returns the map of field metadata.
QVariant metadata (int property) const
 Returns a specific metadata property.
QVariant metadata (Qgis::FieldMetadataProperty property) const
 Returns a specific metadata property.
QString name () const
 Returns the name of the field.
 operator QVariant () const
 Allows direct construction of QVariants from fields.
bool operator!= (const QgsField &other) const
QgsFieldoperator= (const QgsField &other)
bool operator== (const QgsField &other) const
int precision () const
 Gets the precision of the field.
void setAlias (const QString &alias)
 Sets the alias for the field (the friendly displayed name of the field ).
void setComment (const QString &comment)
 Set the field comment.
void setConfigurationFlags (Qgis::FieldConfigurationFlags flags)
 Sets the Flags for the field (searchable, …).
void setConstraints (const QgsFieldConstraints &constraints)
 Sets constraints which are present for the field.
void setDefaultValueDefinition (const QgsDefaultValue &defaultValueDefinition)
 Sets an expression to use when calculating the default value for the field.
void setDuplicatePolicy (Qgis::FieldDuplicatePolicy policy)
 Sets the field's duplicate policy, which indicates how field values should be handled during a duplicate operation.
void setEditorWidgetSetup (const QgsEditorWidgetSetup &v)
 Set the editor widget setup for the field.
void setLength (int len)
 Set the field length.
void setMergePolicy (Qgis::FieldDomainMergePolicy policy)
 Sets the field's merge policy, which indicates how field values should be handled during a merge operation.
void setMetadata (const QMap< int, QVariant > metadata)
 Sets the map of field metadata.
void setMetadata (int property, const QVariant &value)
 Sets a metadata property to value.
void setMetadata (Qgis::FieldMetadataProperty property, const QVariant &value)
 Sets a metadata property to value.
void setName (const QString &name)
 Set the field name.
void setPrecision (int precision)
 Set the field precision.
void setReadOnly (bool readOnly)
 Make field read-only if readOnly is set to true.
void setSplitPolicy (Qgis::FieldDomainSplitPolicy policy)
 Sets the field's split policy, which indicates how field values should be handled during a split operation.
void setSubType (QMetaType::Type subType)
 If the field is a collection, set its element's type.
Q_DECL_DEPRECATED void setSubType (QVariant::Type subType)
 If the field is a collection, set its element's type.
void setType (QMetaType::Type type)
 Set variant type.
Q_DECL_DEPRECATED void setType (QVariant::Type type)
 Set variant type.
void setTypeName (const QString &typeName)
 Set the field type.
Qgis::FieldDomainSplitPolicy splitPolicy () const
 Returns the field's split policy, which indicates how field values should be handled during a split operation.
QMetaType::Type subType () const
 If the field is a collection, gets its element's type.
QMetaType::Type type () const
 Gets variant type of the field as it will be retrieved from data source.
QString typeName () const
 Gets the field type.

Static Public Member Functions

static QString readableConfigurationFlag (Qgis::FieldConfigurationFlag flag)
 Returns the readable and translated value of the configuration flag.

Static Public Attributes

static constexpr int MAX_WKT_LENGTH = 999

Properties

QString alias
QString comment
Qgis::FieldConfigurationFlags configurationFlags
QgsFieldConstraints constraints
QgsDefaultValue defaultValueDefinition
bool isDateOrTime
bool isNumeric
bool isReadOnly
int length
QString name
int precision
QMetaType::Type type

Detailed Description

Encapsulate a field in an attribute table or data source.

QgsField stores metadata about an attribute field, including name, type length, and if applicable, precision.

Note
QgsField objects are implicitly shared.

Definition at line 53 of file qgsfield.h.

Constructor & Destructor Documentation

◆ QgsField() [1/3]

QgsField::QgsField ( const QString & name = QString(),
QMetaType::Type type = QMetaType::Type::UnknownType,
const QString & typeName = QString(),
int len = 0,
int prec = 0,
const QString & comment = QString(),
QMetaType::Type subType = QMetaType::Type::UnknownType )

Constructor.

Constructs a new QgsField object.

Parameters
nameField name
typeField variant type, currently supported: String / Int / Double
typeNameField type (e.g., char, varchar, text, int, serial, double). Field types are usually unique to the source and are stored exactly as returned from the data store.
lenField length
precField precision. Usually decimal places but may also be used in conjunction with other fields types (e.g., variable character fields)
commentComment for the field
subTypeIf the field is a collection, its element's type. When all the elements don't need to have the same type, leave this to QVariant::Invalid.

Definition at line 50 of file qgsfield.cpp.

◆ QgsField() [2/3]

QgsField::QgsField ( const QString & name,
QVariant::Type type,
const QString & typeName = QString(),
int len = 0,
int prec = 0,
const QString & comment = QString(),
QVariant::Type subType = QVariant::Invalid )

Constructor.

Constructs a new QgsField object.

Parameters
nameField name
typeField variant type, currently supported: String / Int / Double
typeNameField type (e.g., char, varchar, text, int, serial, double). Field types are usually unique to the source and are stored exactly as returned from the data store.
lenField length
precField precision. Usually decimal places but may also be used in conjunction with other fields types (e.g., variable character fields)
commentComment for the field
subTypeIf the field is a collection, its element's type. When all the elements don't need to have the same type, leave this to QVariant::Invalid.
Deprecated
QGIS 3.38. Use the method with a QMetaType::Type argument instead.

Definition at line 56 of file qgsfield.cpp.

◆ QgsField() [3/3]

QgsField::QgsField ( const QgsField & other)

Definition at line 62 of file qgsfield.cpp.

◆ ~QgsField()

QgsField::~QgsField ( )
virtualdefault

Member Function Documentation

◆ alias()

QString QgsField::alias ( ) const

Returns the alias for the field (the friendly displayed name of the field ), or an empty string if there is no alias.

See also
setAlias()

Definition at line 293 of file qgsfield.cpp.

◆ comment()

QString QgsField::comment ( ) const

Returns the field comment.

Definition at line 178 of file qgsfield.cpp.

◆ configurationFlags()

Qgis::FieldConfigurationFlags QgsField::configurationFlags ( ) const

Returns the Flags for the field (searchable, …).

See also
setConfigurationFlags()
Since
QGIS 3.34

Definition at line 303 of file qgsfield.cpp.

◆ constraints()

const QgsFieldConstraints & QgsField::constraints ( ) const

Returns constraints which are present for the field.

See also
setConstraints()

Definition at line 288 of file qgsfield.cpp.

◆ convertCompatible()

bool QgsField::convertCompatible ( QVariant & v,
QString * errorMessage = nullptr ) const

Converts the provided variant to a compatible format.

Parameters
vThe value to convert
errorMessageif specified, will be set to a descriptive error when a conversion failure occurs
Returns
true if the conversion was successful

Definition at line 475 of file qgsfield.cpp.

◆ defaultValueDefinition()

QgsDefaultValue QgsField::defaultValueDefinition ( ) const

Returns the expression used when calculating the default value for the field.

Returns
expression evaluated when calculating default values for field, or an empty string if no default is set
See also
setDefaultValueDefinition()

Definition at line 273 of file qgsfield.cpp.

◆ displayName()

QString QgsField::displayName ( ) const

Returns the name to use when displaying this field.

This will be the field alias if set, otherwise the field name.

See also
name()
alias()

Definition at line 97 of file qgsfield.cpp.

◆ displayNameWithAlias()

QString QgsField::displayNameWithAlias ( ) const

Returns the name to use when displaying this field and adds the alias in parenthesis if it is defined.

This will be used when working close to the data structure (i.e. building expressions and queries), when the real field name must be shown but the alias is also useful to understand what the field represents.

See also
name()
alias()
Since
QGIS 3.12

Definition at line 105 of file qgsfield.cpp.

◆ displayString()

QString QgsField::displayString ( const QVariant & v) const

Formats string for display.

Definition at line 319 of file qgsfield.cpp.

◆ displayType()

QString QgsField::displayType ( bool showConstraints = false) const

Returns the type to use when displaying this field, including the length and precision of the datatype if applicable.

This will be used when the full datatype with details has to displayed to the user.

See also
type()
friendlyTypeString()
Since
QGIS 3.14

Definition at line 114 of file qgsfield.cpp.

◆ duplicatePolicy()

Qgis::FieldDuplicatePolicy QgsField::duplicatePolicy ( ) const

Returns the field's duplicate policy, which indicates how field values should be handled during a duplicate operation.

See also
setDuplicatePolicy()
Since
QGIS 3.38

Definition at line 772 of file qgsfield.cpp.

◆ editorWidgetSetup()

QgsEditorWidgetSetup QgsField::editorWidgetSetup ( ) const

Gets the editor widget setup for the field.

Defaults may be set by the provider and can be overridden by manual field configuration.

Returns
the value

Definition at line 747 of file qgsfield.cpp.

◆ friendlyTypeString()

QString QgsField::friendlyTypeString ( ) const

Returns a user friendly, translated representation of the field type.

Unlike displayType(), this method only returns strings representing the field type and does not include length, precision or constraint information.

See also
type()
displayType()
Since
QGIS 3.14

Definition at line 141 of file qgsfield.cpp.

◆ isDateOrTime()

bool QgsField::isDateOrTime ( ) const

Returns if this field is a date and/or time type.

Since
QGIS 3.6

Definition at line 218 of file qgsfield.cpp.

◆ isNumeric()

bool QgsField::isNumeric ( ) const

Returns if this field is numeric.

Any integer or floating point type will return true for this.

Definition at line 213 of file qgsfield.cpp.

◆ isReadOnly()

bool QgsField::isReadOnly ( ) const

Returns true if this field is a read-only field.

This is the case for providers which support generated fields for instance.

Since
QGIS 3.18

Definition at line 757 of file qgsfield.cpp.

◆ length()

int QgsField::length ( ) const

Gets the length of the field.

Returns
int containing the length of the field

Definition at line 168 of file qgsfield.cpp.

◆ mergePolicy()

Qgis::FieldDomainMergePolicy QgsField::mergePolicy ( ) const

Returns the field's merge policy, which indicates how field values should be handled during a merge operation.

See also
setMergePolicy()
Since
QGIS 3.44

Definition at line 782 of file qgsfield.cpp.

◆ metadata() [1/3]

QMap< int, QVariant > QgsField::metadata ( ) const

Returns the map of field metadata.

Map keys should match values from the Qgis::FieldMetadataProperty enum.

See also
setMetadata()
Since
QGIS 3.32

Definition at line 188 of file qgsfield.cpp.

◆ metadata() [2/3]

QVariant QgsField::metadata ( int property) const

Returns a specific metadata property.

See also
setMetadata()
Since
QGIS 3.32

Definition at line 183 of file qgsfield.cpp.

◆ metadata() [3/3]

QVariant QgsField::metadata ( Qgis::FieldMetadataProperty property) const

Returns a specific metadata property.

See also
setMetadata()
Since
QGIS 3.32

Definition at line 193 of file qgsfield.cpp.

◆ name()

QString QgsField::name ( ) const

Returns the name of the field.

See also
setName()
displayName()

Definition at line 92 of file qgsfield.cpp.

◆ operator QVariant()

QgsField::operator QVariant ( ) const

Allows direct construction of QVariants from fields.

Definition at line 737 of file qgsfield.cpp.

◆ operator!=()

bool QgsField::operator!= ( const QgsField & other) const

Definition at line 87 of file qgsfield.cpp.

◆ operator=()

QgsField & QgsField::operator= ( const QgsField & other)

Definition at line 76 of file qgsfield.cpp.

◆ operator==()

bool QgsField::operator== ( const QgsField & other) const

Definition at line 82 of file qgsfield.cpp.

◆ precision()

int QgsField::precision ( ) const

Gets the precision of the field.

Not all field types have a related precision.

Returns
int containing the precision or zero if not applicable to the field type.

Definition at line 173 of file qgsfield.cpp.

◆ readableConfigurationFlag()

QString QgsField::readableConfigurationFlag ( Qgis::FieldConfigurationFlag flag)
static

Returns the readable and translated value of the configuration flag.

Since
QGIS 3.16

Definition at line 453 of file qgsfield.cpp.

◆ setAlias()

void QgsField::setAlias ( const QString & alias)

Sets the alias for the field (the friendly displayed name of the field ).

Parameters
aliasfield alias, or empty string to remove an existing alias
See also
alias()

Definition at line 298 of file qgsfield.cpp.

◆ setComment()

void QgsField::setComment ( const QString & comment)

Set the field comment.

Definition at line 268 of file qgsfield.cpp.

◆ setConfigurationFlags()

void QgsField::setConfigurationFlags ( Qgis::FieldConfigurationFlags flags)

Sets the Flags for the field (searchable, …).

See also
configurationFlags()
Since
QGIS 3.34

Definition at line 308 of file qgsfield.cpp.

◆ setConstraints()

void QgsField::setConstraints ( const QgsFieldConstraints & constraints)

Sets constraints which are present for the field.

See also
constraints()

Definition at line 283 of file qgsfield.cpp.

◆ setDefaultValueDefinition()

void QgsField::setDefaultValueDefinition ( const QgsDefaultValue & defaultValueDefinition)

Sets an expression to use when calculating the default value for the field.

Parameters
defaultValueDefinitionexpression to evaluate when calculating default values for field. Pass a default constructed QgsDefaultValue to reset.
See also
defaultValueDefinition()

Definition at line 278 of file qgsfield.cpp.

◆ setDuplicatePolicy()

void QgsField::setDuplicatePolicy ( Qgis::FieldDuplicatePolicy policy)

Sets the field's duplicate policy, which indicates how field values should be handled during a duplicate operation.

See also
duplicatePolicy()
Since
QGIS 3.38

Definition at line 777 of file qgsfield.cpp.

◆ setEditorWidgetSetup()

void QgsField::setEditorWidgetSetup ( const QgsEditorWidgetSetup & v)

Set the editor widget setup for the field.

Parameters
vThe value to set

Definition at line 742 of file qgsfield.cpp.

◆ setLength()

void QgsField::setLength ( int len)

Set the field length.

Parameters
lenLength of the field

Definition at line 259 of file qgsfield.cpp.

◆ setMergePolicy()

void QgsField::setMergePolicy ( Qgis::FieldDomainMergePolicy policy)

Sets the field's merge policy, which indicates how field values should be handled during a merge operation.

See also
mergePolicy()
Since
QGIS 3.44

Definition at line 787 of file qgsfield.cpp.

◆ setMetadata() [1/3]

void QgsField::setMetadata ( const QMap< int, QVariant > metadata)

Sets the map of field metadata.

Map keys should match values from the Qgis::FieldMetadataProperty enum.

See also
metadata()
Since
QGIS 3.32

Definition at line 198 of file qgsfield.cpp.

◆ setMetadata() [2/3]

void QgsField::setMetadata ( int property,
const QVariant & value )

Sets a metadata property to value.

See also
metadata()
Since
QGIS 3.32

Definition at line 208 of file qgsfield.cpp.

◆ setMetadata() [3/3]

void QgsField::setMetadata ( Qgis::FieldMetadataProperty property,
const QVariant & value )

Sets a metadata property to value.

See also
metadata()
Since
QGIS 3.32

Definition at line 203 of file qgsfield.cpp.

◆ setName()

void QgsField::setName ( const QString & name)

Set the field name.

Parameters
nameName of the field

Definition at line 229 of file qgsfield.cpp.

◆ setPrecision()

void QgsField::setPrecision ( int precision)

Set the field precision.

Parameters
precisionPrecision of the field

Definition at line 263 of file qgsfield.cpp.

◆ setReadOnly()

void QgsField::setReadOnly ( bool readOnly)

Make field read-only if readOnly is set to true.

This is the case for providers which support generated fields for instance.

Since
QGIS 3.18

Definition at line 752 of file qgsfield.cpp.

◆ setSplitPolicy()

void QgsField::setSplitPolicy ( Qgis::FieldDomainSplitPolicy policy)

Sets the field's split policy, which indicates how field values should be handled during a split operation.

See also
splitPolicy()
Since
QGIS 3.30

Definition at line 767 of file qgsfield.cpp.

◆ setSubType() [1/2]

void QgsField::setSubType ( QMetaType::Type subType)

If the field is a collection, set its element's type.

When all the elements don't need to have the same type, set this to QVariant::Invalid.

Definition at line 244 of file qgsfield.cpp.

◆ setSubType() [2/2]

void QgsField::setSubType ( QVariant::Type subType)

If the field is a collection, set its element's type.

When all the elements don't need to have the same type, set this to QVariant::Invalid.

Deprecated
QGIS 3.38. Use the method with a QMetaType::Type argument instead.

Definition at line 249 of file qgsfield.cpp.

◆ setType() [1/2]

void QgsField::setType ( QMetaType::Type type)

Set variant type.

Definition at line 234 of file qgsfield.cpp.

◆ setType() [2/2]

void QgsField::setType ( QVariant::Type type)

Set variant type.

Deprecated
QGIS 3.38. Use the method with a QMetaType::Type argument instead.

Definition at line 239 of file qgsfield.cpp.

◆ setTypeName()

void QgsField::setTypeName ( const QString & typeName)

Set the field type.

Parameters
typeNameField type

Definition at line 254 of file qgsfield.cpp.

◆ splitPolicy()

Qgis::FieldDomainSplitPolicy QgsField::splitPolicy ( ) const

Returns the field's split policy, which indicates how field values should be handled during a split operation.

See also
setSplitPolicy()
Since
QGIS 3.30

Definition at line 762 of file qgsfield.cpp.

◆ subType()

QMetaType::Type QgsField::subType ( ) const

If the field is a collection, gets its element's type.

When all the elements don't need to have the same type, this returns QVariant::Invalid.

Definition at line 158 of file qgsfield.cpp.

◆ type()

QMetaType::Type QgsField::type ( ) const

Gets variant type of the field as it will be retrieved from data source.

Definition at line 153 of file qgsfield.cpp.

◆ typeName()

QString QgsField::typeName ( ) const

Gets the field type.

Field types vary depending on the data source. Examples are char, int, double, blob, geometry, etc. The type is stored exactly as the data store reports it, with no attempt to standardize the value.

Returns
QString containing the field type

Definition at line 163 of file qgsfield.cpp.

Member Data Documentation

◆ MAX_WKT_LENGTH

int QgsField::MAX_WKT_LENGTH = 999
staticconstexpr

Definition at line 578 of file qgsfield.h.

Property Documentation

◆ alias

QString QgsField::alias
readwrite

Definition at line 64 of file qgsfield.h.

◆ comment

QString QgsField::comment
readwrite

Definition at line 62 of file qgsfield.h.

◆ configurationFlags

Qgis::FieldConfigurationFlags QgsField::configurationFlags
readwrite

Definition at line 67 of file qgsfield.h.

◆ constraints

QgsFieldConstraints QgsField::constraints
readwrite

Definition at line 66 of file qgsfield.h.

◆ defaultValueDefinition

QgsDefaultValue QgsField::defaultValueDefinition
readwrite

Definition at line 65 of file qgsfield.h.

◆ isDateOrTime

bool QgsField::isDateOrTime
read

Definition at line 58 of file qgsfield.h.

◆ isNumeric

bool QgsField::isNumeric
read

Definition at line 57 of file qgsfield.h.

◆ isReadOnly

bool QgsField::isReadOnly
readwrite

Definition at line 68 of file qgsfield.h.

◆ length

int QgsField::length
readwrite

Definition at line 59 of file qgsfield.h.

◆ name

QString QgsField::name
readwrite

Definition at line 63 of file qgsfield.h.

◆ precision

int QgsField::precision
readwrite

Definition at line 60 of file qgsfield.h.

◆ type

QMetaType::Type QgsField::type
readwrite

Definition at line 61 of file qgsfield.h.


The documentation for this class was generated from the following files: