QgsField class
Encapsulate a field in an attribute table or data source.
Contents
QgsField stores metadata about an attribute field, including name, type length, and if applicable, precision.
Constructors, destructors, conversion operators
- 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.
- QgsField(const QgsField& other)
- Copy constructor.
- operator QVariant() const
- Allows direct construction of QVariants from fields.
Public functions
- auto alias() const -> QString
- Returns the alias for the field (the friendly displayed name of the field ), or an empty string if there is no alias.
- auto comment() const -> QString
- Returns the field comment.
- auto constraints() const -> const QgsFieldConstraints&
- Returns constraints which are present for the field.
- auto convertCompatible(QVariant& v) const -> bool
- Converts the provided variant to a compatible format.
- auto defaultValueDefinition() const -> QgsDefaultValue
- Returns the expression used when calculating the default value for the field.
- auto displayName() const -> QString
- Returns the name to use when displaying this field.
- auto displayString(const QVariant& v) const -> QString
- Formats string for display.
- auto editorWidgetSetup() const -> QgsEditorWidgetSetup
- Gets the editor widget setup for the field.
- auto isNumeric() const -> bool
- Returns if this field is numeric.
- auto length() const -> int
- Gets the length of the field.
- auto name() const -> QString
- Returns the name of the field.
- auto operator=(const QgsField& other) -> QgsField&
- Assignment operator.
- auto precision() const -> int
- 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 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 setEditorWidgetSetup(const QgsEditorWidgetSetup& v)
- Set the editor widget setup for the field.
- void setLength(int len)
- Set the field length.
- void setName(const QString& name)
- Set the field name.
- void setPrecision(int precision)
- Set the field precision.
- void setSubType(QVariant::Type subType)
- If the field is a collection, set its element's type.
- void setType(QVariant::Type type)
- Set variant type.
- void setTypeName(const QString& typeName)
- Set the field type.
- auto subType() const -> QVariant::Type
- If the field is a collection, gets its element's type.
- auto type() const -> QVariant::Type
- Gets variant type of the field as it will be retrieved from data source.
- auto typeName() const -> QString
- Gets the field type.
Function documentation
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.
| Parameters | |
|---|---|
| name | Field name |
| type | Field variant type, currently supported: String / Int / Double |
| typeName | Field 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. |
| len | Field length |
| prec | Field precision. Usually decimal places but may also be used in conjunction with other fields types (e.g., variable character fields) |
| comment | Comment for the field |
| subType | If 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. |
Constructs a new QgsField object.
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.
const QgsFieldConstraints& QgsField:: constraints() const
Returns constraints which are present for the field.
bool QgsField:: convertCompatible(QVariant& v) const
Converts the provided variant to a compatible format.
| Parameters | |
|---|---|
| v | The value to convert |
| Returns | True if the conversion was successful |
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 |
|---|
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.
QgsEditorWidgetSetup QgsField:: editorWidgetSetup() const
Gets the editor widget setup for the field.
| Returns | the value |
|---|
Defaults may be set by the provider and can be overridden by manual field configuration.
bool QgsField:: isNumeric() const
Returns if this field is numeric.
Any integer or floating point type will return true for this.
int QgsField:: length() const
Gets the length of the field.
| Returns | int containing the length of the field |
|---|
QString QgsField:: name() const
Returns the name of the field.
int QgsField:: precision() const
Gets the precision of the field.
| Returns | int containing the precision or zero if not applicable to the field type. |
|---|
Not all field types have a related precision.
void QgsField:: setAlias(const QString& alias)
Sets the alias for the field (the friendly displayed name of the field ).
| Parameters | |
|---|---|
| alias | field alias, or empty string to remove an existing alias |
void QgsField:: setConstraints(const QgsFieldConstraints& constraints)
Sets constraints which are present for the field.
void QgsField:: setDefaultValueDefinition(const QgsDefaultValue& defaultValueDefinition)
Sets an expression to use when calculating the default value for the field.
| Parameters | |
|---|---|
| defaultValueDefinition | expression to evaluate when calculating default values for field. Pass a default constructed QgsDefaultValue() to reset. |
void QgsField:: setEditorWidgetSetup(const QgsEditorWidgetSetup& v)
Set the editor widget setup for the field.
| Parameters | |
|---|---|
| v | The value to set |
void QgsField:: setPrecision(int precision)
Set the field precision.
| Parameters | |
|---|---|
| precision | Precision of the field |
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.
void QgsField:: setTypeName(const QString& typeName)
Set the field type.
| Parameters | |
|---|---|
| typeName | Field type |
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.
QString QgsField:: typeName() const
Gets the field type.
| Returns | QString containing 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.