QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
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( ConfigurationFlags configurationFlags READ configurationFlags WRITE setConfigurationFlags )
67 Q_PROPERTY( bool isReadOnly READ isReadOnly WRITE setReadOnly )
68
69
70 public:
71
72#ifndef SIP_RUN
73
81 enum class ConfigurationFlag : int
82 {
83 None = 0,
84 NotSearchable = 1 << 1,
85 HideFromWms = 1 << 2,
86 HideFromWfs = 1 << 3,
87 };
88 Q_ENUM( ConfigurationFlag )
89 Q_DECLARE_FLAGS( ConfigurationFlags, ConfigurationFlag )
90 Q_FLAG( ConfigurationFlags )
91#endif
92
108 QgsField( const QString &name = QString(),
109 QVariant::Type type = QVariant::Invalid,
110 const QString &typeName = QString(),
111 int len = 0,
112 int prec = 0,
113 const QString &comment = QString(),
114 QVariant::Type subType = QVariant::Invalid );
115
119 QgsField( const QgsField &other );
120
124 QgsField &operator =( const QgsField &other ) SIP_SKIP;
125
126 virtual ~QgsField();
127
128 bool operator==( const QgsField &other ) const;
129 bool operator!=( const QgsField &other ) const;
130
136 QString name() const;
137
145 QString displayName() const;
146
158 QString displayNameWithAlias() const;
159
170 QString displayType( bool showConstraints = false ) const;
171
182 QString friendlyTypeString() const;
183
185 QVariant::Type type() const;
186
193 QVariant::Type subType() const;
194
201 QString typeName() const;
202
207 int length() const;
208
213 int precision() const;
214
218 QString comment() const;
219
228 QMap< int, QVariant > metadata() const;
229
236 QVariant metadata( Qgis::FieldMetadataProperty property ) const SIP_SKIP;
237
244 QVariant metadata( int property ) const;
245
254 void setMetadata( const QMap< int, QVariant > metadata );
255
262 void setMetadata( Qgis::FieldMetadataProperty property, const QVariant &value ) SIP_SKIP;
263
270 void setMetadata( int property, const QVariant &value );
271
278 bool isNumeric() const;
279
285 bool isDateOrTime() const;
286
291 void setName( const QString &name );
292
296 void setType( QVariant::Type type );
297
304 void setSubType( QVariant::Type subType );
305
310 void setTypeName( const QString &typeName );
311
316 void setLength( int len );
317
322 void setPrecision( int precision );
323
327 void setComment( const QString &comment );
328
336 QgsDefaultValue defaultValueDefinition() const;
337
345 void setDefaultValueDefinition( const QgsDefaultValue &defaultValueDefinition );
346
352 const QgsFieldConstraints &constraints() const;
353
359 void setConstraints( const QgsFieldConstraints &constraints );
360
367 QString alias() const;
368
375 void setAlias( const QString &alias );
376
381 QgsField::ConfigurationFlags configurationFlags() const SIP_SKIP;
382
387 void setConfigurationFlags( QgsField::ConfigurationFlags configurationFlags ) SIP_SKIP;
388
390 QString displayString( const QVariant &v ) const;
391
396 static QString readableConfigurationFlag( QgsField::ConfigurationFlag flag ) SIP_SKIP;
397
398#ifndef SIP_RUN
399
408 bool convertCompatible( QVariant &v, QString *errorMessage = nullptr ) const;
409#else
410
418 bool convertCompatible( QVariant &v ) const;
419 % MethodCode
420 PyObject *sipParseErr = NULL;
421
422 {
423 QVariant *a0;
424 int a0State = 0;
425 const QgsField *sipCpp;
426
427 if ( sipParseArgs( &sipParseErr, sipArgs, "BJ1", &sipSelf, sipType_QgsField, &sipCpp, sipType_QVariant, &a0, &a0State ) )
428 {
429 bool sipRes;
430 QString errorMessage;
431
432 Py_BEGIN_ALLOW_THREADS
433 try
434 {
435 sipRes = sipCpp->convertCompatible( *a0, &errorMessage );
436 }
437 catch ( ... )
438 {
439 Py_BLOCK_THREADS
440
441 sipReleaseType( a0, sipType_QVariant, a0State );
442 sipRaiseUnknownException();
443 return NULL;
444 }
445
446 Py_END_ALLOW_THREADS
447
448 if ( !sipRes )
449 {
450 PyErr_SetString( PyExc_ValueError,
451 QString( "Value could not be converted to field type %1: %2" ).arg( QMetaType::typeName( sipCpp->type() ), errorMessage ).toUtf8().constData() );
452 sipIsErr = 1;
453 }
454 else
455 {
456 PyObject *res = sipConvertFromType( a0, sipType_QVariant, NULL );
457 sipReleaseType( a0, sipType_QVariant, a0State );
458 return res;
459 }
460 }
461 else
462 {
463 // Raise an exception if the arguments couldn't be parsed.
464 sipNoMethod( sipParseErr, sipName_QgsField, sipName_convertCompatible, doc_QgsField_convertCompatible );
465
466 return nullptr;
467 }
468 }
469
470 % End
471#endif
472
474 operator QVariant() const
475 {
476 return QVariant::fromValue( *this );
477 }
478
484 void setEditorWidgetSetup( const QgsEditorWidgetSetup &v );
485
494 QgsEditorWidgetSetup editorWidgetSetup() const;
495
501 void setReadOnly( bool readOnly );
502
508 bool isReadOnly() const;
509
518 Qgis::FieldDomainSplitPolicy splitPolicy() const;
519
528 void setSplitPolicy( Qgis::FieldDomainSplitPolicy policy );
529
530#ifdef SIP_RUN
531 SIP_PYOBJECT __repr__();
532 % MethodCode
533 QString str = QStringLiteral( "<QgsField: %1 (%2)>" ).arg( sipCpp->name() ).arg( sipCpp->typeName() );
534 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
535 % End
536#endif
537
538#ifndef SIP_RUN
539 static constexpr int MAX_WKT_LENGTH = 999;
540#endif
541
542 private:
543
544 QSharedDataPointer<QgsFieldPrivate> d;
545
546
547}; // class QgsField
548
550
552
554CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsField &field );
556CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsField &field );
557
558
559#endif
FieldDomainSplitPolicy
Split policy for field domains.
Definition: qgis.h:2446
FieldMetadataProperty
Standard field metadata values.
Definition: qgis.h:1154
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()
ConfigurationFlag
Configuration flags for fields These flags are meant to be user-configurable and are not describing a...
Definition: qgsfield.h:82
#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
const QgsField & field
Definition: qgsfield.h:554
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsField::ConfigurationFlags) CORE_EXPORT QDataStream &operator<<(QDataStream &out
Writes the field to stream out. QGIS version compatibility is not guaranteed.
const QString & typeName
int precision