QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Properties | List of all members
QgsField Class Reference

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

#include <qgsfield.h>

Public Types

enum class  ConfigurationFlag : int { None = 0 , NotSearchable = 1 << 1 , HideFromWms = 1 << 2 , HideFromWfs = 1 << 3 }
 Configuration flags for fields These flags are meant to be user-configurable and are not describing any information from the data provider. More...
 

Public Member Functions

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

Static Public Member Functions

static QString readableConfigurationFlag (QgsField::ConfigurationFlag flag)
 Returns the readable and translated value of the configuration flag. More...
 

Static Public Attributes

static constexpr int MAX_WKT_LENGTH = 999
 

Properties

QString alias
 
QString comment
 
ConfigurationFlags configurationFlags
 
QgsFieldConstraints constraints
 
QgsDefaultValue defaultValueDefinition
 
bool isDateOrTime
 
Q_GADGET bool isNumeric
 
bool isReadOnly
 
int length
 
QString name
 
int precision
 
QVariant::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 52 of file qgsfield.h.

Member Enumeration Documentation

◆ ConfigurationFlag

enum class QgsField::ConfigurationFlag : int
strong

Configuration flags for fields These flags are meant to be user-configurable and are not describing any information from the data provider.

Note
Flags are expressed in the negative forms so that default flags is None.
Since
QGIS 3.16
Enumerator
None 

No flag is defined.

NotSearchable 

Defines if the field is searchable (used in the locator search for instance)

HideFromWms 

Field is not available if layer is served as WMS from QGIS server.

HideFromWfs 

Field is not available if layer is served as WFS from QGIS server.

Definition at line 81 of file qgsfield.h.

Constructor & Destructor Documentation

◆ QgsField() [1/2]

QgsField::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.

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 47 of file qgsfield.cpp.

◆ QgsField() [2/2]

QgsField::QgsField ( const QgsField other)

Copy constructor.

Definition at line 53 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()
Since
QGIS 3.0

Definition at line 270 of file qgsfield.cpp.

◆ comment()

QString QgsField::comment ( ) const

Returns the field comment.

Definition at line 165 of file qgsfield.cpp.

◆ configurationFlags()

QgsField::ConfigurationFlags QgsField::configurationFlags ( ) const

Returns the Flags for the field (searchable, …)

Since
QGIS 3.16

Definition at line 280 of file qgsfield.cpp.

◆ constraints()

const QgsFieldConstraints & QgsField::constraints ( ) const

Returns constraints which are present for the field.

See also
setConstraints()
Since
QGIS 3.0

Definition at line 265 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 452 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()
Since
QGIS 3.0

Definition at line 250 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()
Since
QGIS 3.0

Definition at line 88 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 96 of file qgsfield.cpp.

◆ displayString()

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

Formats string for display.

Definition at line 296 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 105 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 714 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 128 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 205 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.

Since
QGIS 2.18

Definition at line 200 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 724 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 155 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 175 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 170 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 180 of file qgsfield.cpp.

◆ name()

QString QgsField::name ( ) const

Returns the name of the field.

See also
setName()
displayName()

Definition at line 83 of file qgsfield.cpp.

◆ operator QVariant()

QgsField::operator QVariant ( ) const
inline

Allows direct construction of QVariants from fields.

Definition at line 474 of file qgsfield.h.

◆ operator!=()

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

Definition at line 78 of file qgsfield.cpp.

◆ operator=()

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

Assignment operator.

Definition at line 67 of file qgsfield.cpp.

◆ operator==()

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

Definition at line 73 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 160 of file qgsfield.cpp.

◆ readableConfigurationFlag()

QString QgsField::readableConfigurationFlag ( QgsField::ConfigurationFlag  flag)
static

Returns the readable and translated value of the configuration flag.

Since
QGIS 3.16

Definition at line 430 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()
Since
QGIS 3.0

Definition at line 275 of file qgsfield.cpp.

◆ setComment()

void QgsField::setComment ( const QString &  comment)

Set the field comment.

Definition at line 245 of file qgsfield.cpp.

◆ setConfigurationFlags()

void QgsField::setConfigurationFlags ( QgsField::ConfigurationFlags  configurationFlags)

Sets the Flags for the field (searchable, …)

Since
QGIS 3.16

Definition at line 285 of file qgsfield.cpp.

◆ setConstraints()

void QgsField::setConstraints ( const QgsFieldConstraints constraints)

Sets constraints which are present for the field.

See also
constraints()
Since
QGIS 3.0

Definition at line 260 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()
Since
QGIS 3.0

Definition at line 255 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 709 of file qgsfield.cpp.

◆ setLength()

void QgsField::setLength ( int  len)

Set the field length.

Parameters
lenLength of the field

Definition at line 236 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 185 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 195 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 190 of file qgsfield.cpp.

◆ setName()

void QgsField::setName ( const QString &  name)

Set the field name.

Parameters
nameName of the field

Definition at line 216 of file qgsfield.cpp.

◆ setPrecision()

void QgsField::setPrecision ( int  precision)

Set the field precision.

Parameters
precisionPrecision of the field

Definition at line 240 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 719 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 734 of file qgsfield.cpp.

◆ setSubType()

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.

Since
QGIS 3.0

Definition at line 226 of file qgsfield.cpp.

◆ setType()

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

Set variant type.

Definition at line 221 of file qgsfield.cpp.

◆ setTypeName()

void QgsField::setTypeName ( const QString &  typeName)

Set the field type.

Parameters
typeNameField type

Definition at line 231 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 729 of file qgsfield.cpp.

◆ subType()

QVariant::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.

Since
QGIS 3.0

Definition at line 145 of file qgsfield.cpp.

◆ type()

QVariant::Type QgsField::type ( ) const

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

Definition at line 140 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 150 of file qgsfield.cpp.

Member Data Documentation

◆ MAX_WKT_LENGTH

constexpr int QgsField::MAX_WKT_LENGTH = 999
staticconstexpr

Definition at line 539 of file qgsfield.h.

Property Documentation

◆ alias

QString QgsField::alias
readwrite

Definition at line 54 of file qgsfield.h.

◆ comment

QString QgsField::comment
readwrite

Definition at line 54 of file qgsfield.h.

◆ configurationFlags

ConfigurationFlags QgsField::configurationFlags
readwrite

Definition at line 54 of file qgsfield.h.

◆ constraints

QgsFieldConstraints QgsField::constraints
readwrite

Definition at line 54 of file qgsfield.h.

◆ defaultValueDefinition

QgsDefaultValue QgsField::defaultValueDefinition
readwrite

Definition at line 54 of file qgsfield.h.

◆ isDateOrTime

bool QgsField::isDateOrTime
read

Definition at line 54 of file qgsfield.h.

◆ isNumeric

Q_GADGET bool QgsField::isNumeric
read

Definition at line 54 of file qgsfield.h.

◆ isReadOnly

bool QgsField::isReadOnly
readwrite

Definition at line 54 of file qgsfield.h.

◆ length

int QgsField::length
readwrite

Definition at line 54 of file qgsfield.h.

◆ name

QString QgsField::name
readwrite

Definition at line 54 of file qgsfield.h.

◆ precision

int QgsField::precision
readwrite

Definition at line 54 of file qgsfield.h.

◆ type

QVariant::Type QgsField::type
readwrite

Definition at line 54 of file qgsfield.h.


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