QGIS API Documentation 3.37.0-Master (fa0c88a90b0)
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 <QString>
20#include <QVariant>
21#include <QVector>
22#include <QSharedDataPointer>
23#include "qgis_core.h"
24#include "qgis_sip.h"
25#include "qgis.h"
26
27typedef QList<int> QgsAttributeList SIP_SKIP;
28
29/***************************************************************************
30 * This class is considered CRITICAL and any change MUST be accompanied with
31 * full unit tests in testqgsfield.cpp.
32 * See details in QEP #17
33 ****************************************************************************/
34
36#include "qgsfieldconstraints.h"
37#include "qgsdefaultvalue.h"
38#include "qgis.h"
39
40class QgsFieldPrivate;
41
52class CORE_EXPORT QgsField
53{
54 Q_GADGET
55
56 Q_PROPERTY( bool isNumeric READ isNumeric )
57 Q_PROPERTY( bool isDateOrTime READ isDateOrTime )
58 Q_PROPERTY( int length READ length WRITE setLength )
59 Q_PROPERTY( int precision READ precision WRITE setPrecision )
60 Q_PROPERTY( QVariant::Type type READ type WRITE setType )
61 Q_PROPERTY( QString comment READ comment WRITE setComment )
62 Q_PROPERTY( QString name READ name WRITE setName )
63 Q_PROPERTY( QString alias READ alias WRITE setAlias )
64 Q_PROPERTY( QgsDefaultValue defaultValueDefinition READ defaultValueDefinition WRITE setDefaultValueDefinition )
65 Q_PROPERTY( QgsFieldConstraints constraints READ constraints WRITE setConstraints )
66 Q_PROPERTY( Qgis::FieldConfigurationFlags configurationFlags READ configurationFlags WRITE setConfigurationFlags )
67 Q_PROPERTY( bool isReadOnly READ isReadOnly WRITE setReadOnly )
68
69 public:
70
86 QgsField( const QString &name = QString(),
87 QVariant::Type type = QVariant::Invalid,
88 const QString &typeName = QString(),
89 int len = 0,
90 int prec = 0,
91 const QString &comment = QString(),
92 QVariant::Type subType = QVariant::Invalid );
93
97 QgsField( const QgsField &other );
98
102 QgsField &operator =( const QgsField &other ) SIP_SKIP;
103
104 virtual ~QgsField();
105
106 bool operator==( const QgsField &other ) const;
107 bool operator!=( const QgsField &other ) const;
108
114 QString name() const;
115
122 QString displayName() const;
123
135 QString displayNameWithAlias() const;
136
147 QString displayType( bool showConstraints = false ) const;
148
159 QString friendlyTypeString() const;
160
162 QVariant::Type type() const;
163
169 QVariant::Type subType() const;
170
177 QString typeName() const;
178
183 int length() const;
184
189 int precision() const;
190
194 QString comment() const;
195
204 QMap< int, QVariant > metadata() const;
205
212 QVariant metadata( Qgis::FieldMetadataProperty property ) const SIP_SKIP;
213
220 QVariant metadata( int property ) const;
221
230 void setMetadata( const QMap< int, QVariant > metadata );
231
238 void setMetadata( Qgis::FieldMetadataProperty property, const QVariant &value ) SIP_SKIP;
239
246 void setMetadata( int property, const QVariant &value );
247
253 bool isNumeric() const;
254
260 bool isDateOrTime() const;
261
266 void setName( const QString &name );
267
271 void setType( QVariant::Type type );
272
278 void setSubType( QVariant::Type subType );
279
284 void setTypeName( const QString &typeName );
285
290 void setLength( int len );
291
296 void setPrecision( int precision );
297
301 void setComment( const QString &comment );
302
309 QgsDefaultValue defaultValueDefinition() const;
310
317 void setDefaultValueDefinition( const QgsDefaultValue &defaultValueDefinition );
318
323 const QgsFieldConstraints &constraints() const;
324
329 void setConstraints( const QgsFieldConstraints &constraints );
330
336 QString alias() const;
337
343 void setAlias( const QString &alias );
344
350 Qgis::FieldConfigurationFlags configurationFlags() const;
351
357 void setConfigurationFlags( Qgis::FieldConfigurationFlags flags );
358
360 QString displayString( const QVariant &v ) const;
361
366 static QString readableConfigurationFlag( Qgis::FieldConfigurationFlag flag );
367
368#ifndef SIP_RUN
369
378 bool convertCompatible( QVariant &v, QString *errorMessage = nullptr ) const;
379#else
380
388 bool convertCompatible( QVariant &v ) const;
389 % MethodCode
390 PyObject *sipParseErr = NULL;
391
392 {
393 QVariant *a0;
394 int a0State = 0;
395 const QgsField *sipCpp;
396
397 if ( sipParseArgs( &sipParseErr, sipArgs, "BJ1", &sipSelf, sipType_QgsField, &sipCpp, sipType_QVariant, &a0, &a0State ) )
398 {
399 bool sipRes;
400 QString errorMessage;
401
402 Py_BEGIN_ALLOW_THREADS
403 try
404 {
405 sipRes = sipCpp->convertCompatible( *a0, &errorMessage );
406 }
407 catch ( ... )
408 {
409 Py_BLOCK_THREADS
410
411 sipReleaseType( a0, sipType_QVariant, a0State );
412 sipRaiseUnknownException();
413 return NULL;
414 }
415
416 Py_END_ALLOW_THREADS
417
418 if ( !sipRes )
419 {
420 PyErr_SetString( PyExc_ValueError,
421 QString( "Value could not be converted to field type %1: %2" ).arg( QMetaType::typeName( sipCpp->type() ), errorMessage ).toUtf8().constData() );
422 sipIsErr = 1;
423 }
424 else
425 {
426 PyObject *res = sipConvertFromType( a0, sipType_QVariant, NULL );
427 sipReleaseType( a0, sipType_QVariant, a0State );
428 return res;
429 }
430 }
431 else
432 {
433 // Raise an exception if the arguments couldn't be parsed.
434 sipNoMethod( sipParseErr, sipName_QgsField, sipName_convertCompatible, doc_QgsField_convertCompatible );
435
436 return nullptr;
437 }
438 }
439
440 % End
441#endif
442
444 operator QVariant() const
445 {
446 return QVariant::fromValue( *this );
447 }
448
454 void setEditorWidgetSetup( const QgsEditorWidgetSetup &v );
455
464 QgsEditorWidgetSetup editorWidgetSetup() const;
465
471 void setReadOnly( bool readOnly );
472
478 bool isReadOnly() const;
479
488 Qgis::FieldDomainSplitPolicy splitPolicy() const;
489
498 void setSplitPolicy( Qgis::FieldDomainSplitPolicy policy );
499
500#ifdef SIP_RUN
501 SIP_PYOBJECT __repr__();
502 % MethodCode
503 QString str = QStringLiteral( "<QgsField: %1 (%2)>" ).arg( sipCpp->name() ).arg( sipCpp->typeName() );
504 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
505 % End
506#endif
507
508#ifndef SIP_RUN
509 static constexpr int MAX_WKT_LENGTH = 999;
510#endif
511
512 private:
513
514 QSharedDataPointer<QgsFieldPrivate> d;
515
516
517}; // class QgsField
518
520
521
522CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsField &field );
524CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsField &field );
525
526
527#endif
FieldDomainSplitPolicy
Split policy for field domains.
Definition: qgis.h:3162
FieldMetadataProperty
Configuration flags for fields These flags are meant to be user-configurable and are not describing a...
Definition: qgis.h:1313
FieldConfigurationFlag
Configuration flags for fields These flags are meant to be user-configurable and are not describing a...
Definition: qgis.h:1289
The QgsDefaultValue class provides a container for managing client side default values for fields.
Holder for the widget type and its configuration for a field.
Stores information about constraints which may be present on a field.
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:53
QString typeName() const
Gets the field type.
Definition: qgsfield.cpp:150
QString name
Definition: qgsfield.h:62
bool convertCompatible(QVariant &v, QString *errorMessage=nullptr) const
Converts the provided variant to a compatible format.
Definition: qgsfield.cpp:452
QVariant::Type type
Definition: qgsfield.h:60
virtual ~QgsField()
#define str(x)
Definition: qgis.cpp:38
#define SIP_SKIP
Definition: qgis_sip.h:126
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
QList< int > QgsAttributeList
Definition: qgsfield.h:27
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:771
CORE_EXPORT QDataStream & operator<<(QDataStream &out, const QgsField &field)
Writes the field to stream out. QGIS version compatibility is not guaranteed.
Definition: qgsfield.cpp:745
const QString & typeName
int precision