QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
qgsfield.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsfield.h - Describes a field in a layer or table
3 --------------------------------------
4 Date : 01-Jan-2004
5 Copyright : (C) 2004 by Gary E.Sherman
6 email : sherman at mrcc.com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#ifndef QGSFIELD_H
17#define QGSFIELD_H
18
19#include "qgis.h"
20#include "qgis_core.h"
21#include "qgis_sip.h"
22
23#include <QSharedDataPointer>
24#include <QString>
25#include <QVariant>
26#include <QVector>
27
28typedef QList<int> QgsAttributeList SIP_SKIP;
29
30/***************************************************************************
31 * This class is considered CRITICAL and any change MUST be accompanied with
32 * full unit tests in testqgsfield.cpp.
33 * See details in QEP #17
34 ****************************************************************************/
35
37#include "qgsfieldconstraints.h"
38#include "qgsdefaultvalue.h"
39#include "qgis.h"
40
41class QgsFieldPrivate;
42
52
53class CORE_EXPORT QgsField
54{
55 Q_GADGET
56
57 Q_PROPERTY( bool isNumeric READ isNumeric )
58 Q_PROPERTY( bool isDateOrTime READ isDateOrTime )
59 Q_PROPERTY( int length READ length WRITE setLength )
60 Q_PROPERTY( int precision READ precision WRITE setPrecision )
61 Q_PROPERTY( QMetaType::Type type READ type WRITE setType )
62 Q_PROPERTY( QString comment READ comment WRITE setComment )
63 Q_PROPERTY( QString name READ name WRITE setName )
64 Q_PROPERTY( QString alias READ alias WRITE setAlias )
67 Q_PROPERTY( Qgis::FieldConfigurationFlags configurationFlags READ configurationFlags WRITE setConfigurationFlags )
68 Q_PROPERTY( bool isReadOnly READ isReadOnly WRITE setReadOnly )
69
70 public:
71
87 QgsField( const QString &name = QString(),
88 QMetaType::Type type = QMetaType::Type::UnknownType,
89 const QString &typeName = QString(),
90 int len = 0,
91 int prec = 0,
92 const QString &comment = QString(),
93 QMetaType::Type subType = QMetaType::Type::UnknownType ) SIP_HOLDGIL;
94
95
112 Q_DECL_DEPRECATED QgsField( const QString &name,
113 QVariant::Type type,
114 const QString &typeName = QString(),
115 int len = 0,
116 int prec = 0,
117 const QString &comment = QString(),
118 QVariant::Type subType = QVariant::Invalid ) SIP_HOLDGIL SIP_DEPRECATED;
119
120 QgsField( const QgsField &other ) SIP_HOLDGIL;
121 QgsField &operator =( const QgsField &other ) SIP_SKIP;
122
123 virtual ~QgsField();
124
125 bool operator==( const QgsField &other ) const SIP_HOLDGIL;
126 bool operator!=( const QgsField &other ) const SIP_HOLDGIL;
127
133 QString name() const SIP_HOLDGIL;
134
141 QString displayName() const SIP_HOLDGIL;
142
154 QString displayNameWithAlias() const SIP_HOLDGIL;
155
166 QString displayType( bool showConstraints = false ) const SIP_HOLDGIL;
167
178 QString friendlyTypeString() const SIP_HOLDGIL;
179
181 QMetaType::Type type() const SIP_HOLDGIL;
182
188 QMetaType::Type subType() const SIP_HOLDGIL;
189
196 QString typeName() const SIP_HOLDGIL;
197
202 int length() const SIP_HOLDGIL;
203
208 int precision() const SIP_HOLDGIL;
209
213 QString comment() const SIP_HOLDGIL;
214
223 QMap< int, QVariant > metadata() const SIP_HOLDGIL;
224
231 QVariant metadata( Qgis::FieldMetadataProperty property ) const SIP_SKIP;
232
239 QVariant metadata( int property ) const SIP_HOLDGIL;
240
249 void setMetadata( const QMap< int, QVariant > metadata ) SIP_HOLDGIL;
250
257 void setMetadata( Qgis::FieldMetadataProperty property, const QVariant &value ) SIP_SKIP;
258
265 void setMetadata( int property, const QVariant &value ) SIP_HOLDGIL;
266
271 bool isNumeric() const SIP_HOLDGIL;
272
278 bool isDateOrTime() const SIP_HOLDGIL;
279
284 void setName( const QString &name ) SIP_HOLDGIL;
285
289 void setType( QMetaType::Type type ) SIP_HOLDGIL;
290
295 Q_DECL_DEPRECATED void setType( QVariant::Type type ) SIP_HOLDGIL SIP_DEPRECATED;
296
302 void setSubType( QMetaType::Type subType ) SIP_HOLDGIL;
303
310 Q_DECL_DEPRECATED void setSubType( QVariant::Type subType ) SIP_HOLDGIL SIP_DEPRECATED;
311
316 void setTypeName( const QString &typeName ) SIP_HOLDGIL;
317
322 void setLength( int len ) SIP_HOLDGIL;
323
329
333 void setComment( const QString &comment ) SIP_HOLDGIL;
334
342
350
356
362
368 QString alias() const SIP_HOLDGIL;
369
375 void setAlias( const QString &alias ) SIP_HOLDGIL;
376
382 Qgis::FieldConfigurationFlags configurationFlags() const SIP_HOLDGIL;
383
389 void setConfigurationFlags( Qgis::FieldConfigurationFlags flags ) SIP_HOLDGIL;
390
392 QString displayString( const QVariant &v ) const;
393
398 static QString readableConfigurationFlag( Qgis::FieldConfigurationFlag flag ) SIP_HOLDGIL;
399
400#ifndef SIP_RUN
401
410 bool convertCompatible( QVariant &v, QString *errorMessage = nullptr ) const;
411#else
412
420 bool convertCompatible( QVariant &v ) const;
421 % MethodCode
422 PyObject *sipParseErr = NULL;
423
424 {
425 QVariant *a0;
426 int a0State = 0;
427 const QgsField *sipCpp;
428
429 if ( sipParseArgs( &sipParseErr, sipArgs, "BJ1", &sipSelf, sipType_QgsField, &sipCpp, sipType_QVariant, &a0, &a0State ) )
430 {
431 bool sipRes;
432 QString errorMessage;
433
434 Py_BEGIN_ALLOW_THREADS
435 try
436 {
437 sipRes = sipCpp->convertCompatible( *a0, &errorMessage );
438 }
439 catch ( ... )
440 {
441 Py_BLOCK_THREADS
442
443 sipReleaseType( a0, sipType_QVariant, a0State );
444 sipRaiseUnknownException();
445 return NULL;
446 }
447
448 Py_END_ALLOW_THREADS
449
450 if ( !sipRes )
451 {
452 PyErr_SetString( PyExc_ValueError,
453 QString( "Value could not be converted to field type %1: %2" ).arg( QMetaType::typeName( sipCpp->type() ), errorMessage ).toUtf8().constData() );
454 sipIsErr = 1;
455 }
456 else
457 {
458 PyObject *res = sipConvertFromType( a0, sipType_QVariant, NULL );
459 sipReleaseType( a0, sipType_QVariant, a0State );
460 return res;
461 }
462 }
463 else
464 {
465 // Raise an exception if the arguments couldn't be parsed.
466 sipNoMethod( sipParseErr, sipName_QgsField, sipName_convertCompatible, doc_QgsField_convertCompatible );
467
468 return nullptr;
469 }
470 }
471
472 % End
473#endif
474
476 operator QVariant() const;
477
483 void setEditorWidgetSetup( const QgsEditorWidgetSetup &v ) SIP_HOLDGIL;
484
493 QgsEditorWidgetSetup editorWidgetSetup() const SIP_HOLDGIL;
494
500 void setReadOnly( bool readOnly ) SIP_HOLDGIL;
501
507 bool isReadOnly() const SIP_HOLDGIL;
508
517 Qgis::FieldDomainSplitPolicy splitPolicy() const SIP_HOLDGIL;
518
527 void setSplitPolicy( Qgis::FieldDomainSplitPolicy policy ) SIP_HOLDGIL;
528
537 Qgis::FieldDuplicatePolicy duplicatePolicy() const SIP_HOLDGIL;
538
547 void setDuplicatePolicy( Qgis::FieldDuplicatePolicy policy ) SIP_HOLDGIL;
548
557 Qgis::FieldDomainMergePolicy mergePolicy() const SIP_HOLDGIL;
558
567 void setMergePolicy( Qgis::FieldDomainMergePolicy policy ) SIP_HOLDGIL;
568
569#ifdef SIP_RUN
570 SIP_PYOBJECT __repr__();
571 % MethodCode
572 QString str = QStringLiteral( "<QgsField: %1 (%2)>" ).arg( sipCpp->name() ).arg( sipCpp->typeName() );
573 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
574 % End
575#endif
576
577#ifndef SIP_RUN
578 static constexpr int MAX_WKT_LENGTH = 999;
579#endif
580
581 private:
582
583 QSharedDataPointer<QgsFieldPrivate> d;
584
585
586}; // class QgsField
587
589
590
591CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsField &field );
593CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsField &field );
594
595
596#endif
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:56
Provides a container for managing client side default values for fields.
Stores information about constraints which may be present on a field.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:54
QMetaType::Type type
Definition qgsfield.h:61
bool isDateOrTime
Definition qgsfield.h:58
QString typeName() const
Gets the field type.
Definition qgsfield.cpp:163
void setConstraints(const QgsFieldConstraints &constraints)
Sets constraints which are present for the field.
Definition qgsfield.cpp:283
void setAlias(const QString &alias)
Sets the alias for the field (the friendly displayed name of the field ).
Definition qgsfield.cpp:298
QString name
Definition qgsfield.h:63
int precision
Definition qgsfield.h:60
int length
Definition qgsfield.h:59
QString displayString(const QVariant &v) const
Formats string for display.
Definition qgsfield.cpp:319
void setPrecision(int precision)
Set the field precision.
Definition qgsfield.cpp:263
QString displayNameWithAlias() const
Returns the name to use when displaying this field and adds the alias in parenthesis if it is defined...
Definition qgsfield.cpp:105
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.
Definition qgsfield.cpp:50
bool convertCompatible(QVariant &v, QString *errorMessage=nullptr) const
Converts the provided variant to a compatible format.
Definition qgsfield.cpp:475
void setSubType(QMetaType::Type subType)
If the field is a collection, set its element's type.
Definition qgsfield.cpp:244
void setName(const QString &name)
Set the field name.
Definition qgsfield.cpp:229
void setComment(const QString &comment)
Set the field comment.
Definition qgsfield.cpp:268
void setType(QMetaType::Type type)
Set variant type.
Definition qgsfield.cpp:234
QString displayType(bool showConstraints=false) const
Returns the type to use when displaying this field, including the length and precision of the datatyp...
Definition qgsfield.cpp:114
void setConfigurationFlags(Qgis::FieldConfigurationFlags flags)
Sets the Flags for the field (searchable, …).
Definition qgsfield.cpp:308
void setLength(int len)
Set the field length.
Definition qgsfield.cpp:259
QString displayName() const
Returns the name to use when displaying this field.
Definition qgsfield.cpp:97
void setDefaultValueDefinition(const QgsDefaultValue &defaultValueDefinition)
Sets an expression to use when calculating the default value for the field.
Definition qgsfield.cpp:278
QString friendlyTypeString() const
Returns a user friendly, translated representation of the field type.
Definition qgsfield.cpp:141
void setReadOnly(bool readOnly)
Make field read-only if readOnly is set to true.
Definition qgsfield.cpp:752
QMap< int, QVariant > metadata() const
Returns the map of field metadata.
Definition qgsfield.cpp:188
static QString readableConfigurationFlag(Qgis::FieldConfigurationFlag flag)
Returns the readable and translated value of the configuration flag.
Definition qgsfield.cpp:453
Qgis::FieldConfigurationFlags configurationFlags
Definition qgsfield.h:67
QMetaType::Type subType() const
If the field is a collection, gets its element's type.
Definition qgsfield.cpp:158
QString alias
Definition qgsfield.h:64
static constexpr int MAX_WKT_LENGTH
Definition qgsfield.h:578
QgsDefaultValue defaultValueDefinition
Definition qgsfield.h:65
bool isNumeric
Definition qgsfield.h:57
void setMetadata(const QMap< int, QVariant > metadata)
Sets the map of field metadata.
Definition qgsfield.cpp:198
QString comment
Definition qgsfield.h:62
QgsFieldConstraints constraints
Definition qgsfield.h:66
bool isReadOnly
Definition qgsfield.h:68
void setTypeName(const QString &typeName)
Set the field type.
Definition qgsfield.cpp:254
#define SIP_DEPRECATED
Definition qgis_sip.h:114
#define SIP_SKIP
Definition qgis_sip.h:134
#define SIP_HOLDGIL
Definition qgis_sip.h:179
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)
QList< int > QgsAttributeList
Definition qgsfield.h:28
CORE_EXPORT QDataStream & operator>>(QDataStream &in, QgsField &field)
Reads a field from stream in into field. QGIS version compatibility is not guaranteed.
Definition qgsfield.cpp:825
CORE_EXPORT QDataStream & operator<<(QDataStream &out, const QgsField &field)
Writes the field to stream out. QGIS version compatibility is not guaranteed.
Definition qgsfield.cpp:798