QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
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
26typedef QList<int> QgsAttributeList SIP_SKIP;
27
28/***************************************************************************
29 * This class is considered CRITICAL and any change MUST be accompanied with
30 * full unit tests in testqgsfield.cpp.
31 * See details in QEP #17
32 ****************************************************************************/
33
35#include "qgsfieldconstraints.h"
36#include "qgsdefaultvalue.h"
37#include "qgis.h"
38
39class QgsFieldPrivate;
40
51class CORE_EXPORT QgsField
52{
53 Q_GADGET
54
55 Q_PROPERTY( bool isNumeric READ isNumeric )
56 Q_PROPERTY( bool isDateOrTime READ isDateOrTime )
57 Q_PROPERTY( int length READ length WRITE setLength )
58 Q_PROPERTY( int precision READ precision WRITE setPrecision )
59 Q_PROPERTY( QVariant::Type type READ type WRITE setType )
60 Q_PROPERTY( QString comment READ comment WRITE setComment )
61 Q_PROPERTY( QString name READ name WRITE setName )
62 Q_PROPERTY( QString alias READ alias WRITE setAlias )
63 Q_PROPERTY( QgsDefaultValue defaultValueDefinition READ defaultValueDefinition WRITE setDefaultValueDefinition )
64 Q_PROPERTY( QgsFieldConstraints constraints READ constraints WRITE setConstraints )
65 Q_PROPERTY( ConfigurationFlags configurationFlags READ configurationFlags WRITE setConfigurationFlags )
66 Q_PROPERTY( bool isReadOnly READ isReadOnly WRITE setReadOnly )
67
68
69 public:
70
71#ifndef SIP_RUN
72
80 enum class ConfigurationFlag : int
81 {
82 None = 0,
83 NotSearchable = 1 << 1,
84 HideFromWms = 1 << 2,
85 HideFromWfs = 1 << 3,
86 };
87 Q_ENUM( ConfigurationFlag )
88 Q_DECLARE_FLAGS( ConfigurationFlags, ConfigurationFlag )
89 Q_FLAG( ConfigurationFlags )
90#endif
91
107 QgsField( const QString &name = QString(),
108 QVariant::Type type = QVariant::Invalid,
109 const QString &typeName = QString(),
110 int len = 0,
111 int prec = 0,
112 const QString &comment = QString(),
113 QVariant::Type subType = QVariant::Invalid );
114
118 QgsField( const QgsField &other );
119
123 QgsField &operator =( const QgsField &other ) SIP_SKIP;
124
125 virtual ~QgsField();
126
127 bool operator==( const QgsField &other ) const;
128 bool operator!=( const QgsField &other ) const;
129
135 QString name() const;
136
144 QString displayName() const;
145
157 QString displayNameWithAlias() const;
158
169 QString displayType( bool showConstraints = false ) const;
170
181 QString friendlyTypeString() const;
182
184 QVariant::Type type() const;
185
192 QVariant::Type subType() const;
193
200 QString typeName() const;
201
206 int length() const;
207
212 int precision() const;
213
217 QString comment() const;
218
225 bool isNumeric() const;
226
232 bool isDateOrTime() const;
233
238 void setName( const QString &name );
239
243 void setType( QVariant::Type type );
244
251 void setSubType( QVariant::Type subType );
252
257 void setTypeName( const QString &typeName );
258
263 void setLength( int len );
264
269 void setPrecision( int precision );
270
274 void setComment( const QString &comment );
275
283 QgsDefaultValue defaultValueDefinition() const;
284
292 void setDefaultValueDefinition( const QgsDefaultValue &defaultValueDefinition );
293
299 const QgsFieldConstraints &constraints() const;
300
306 void setConstraints( const QgsFieldConstraints &constraints );
307
314 QString alias() const;
315
322 void setAlias( const QString &alias );
323
328 QgsField::ConfigurationFlags configurationFlags() const SIP_SKIP;
329
334 void setConfigurationFlags( QgsField::ConfigurationFlags configurationFlags ) SIP_SKIP;
335
337 QString displayString( const QVariant &v ) const;
338
343 static QString readableConfigurationFlag( QgsField::ConfigurationFlag flag ) SIP_SKIP;
344
345#ifndef SIP_RUN
346
355 bool convertCompatible( QVariant &v, QString *errorMessage = nullptr ) const;
356#else
357
365 bool convertCompatible( QVariant &v ) const;
366 % MethodCode
367 PyObject *sipParseErr = NULL;
368
369 {
370 QVariant *a0;
371 int a0State = 0;
372 const QgsField *sipCpp;
373
374 if ( sipParseArgs( &sipParseErr, sipArgs, "BJ1", &sipSelf, sipType_QgsField, &sipCpp, sipType_QVariant, &a0, &a0State ) )
375 {
376 bool sipRes;
377 QString errorMessage;
378
379 Py_BEGIN_ALLOW_THREADS
380 try
381 {
382 sipRes = sipCpp->convertCompatible( *a0, &errorMessage );
383 }
384 catch ( ... )
385 {
386 Py_BLOCK_THREADS
387
388 sipReleaseType( a0, sipType_QVariant, a0State );
389 sipRaiseUnknownException();
390 return NULL;
391 }
392
393 Py_END_ALLOW_THREADS
394
395 if ( !sipRes )
396 {
397 PyErr_SetString( PyExc_ValueError,
398 QString( "Value could not be converted to field type %1: %2" ).arg( QMetaType::typeName( sipCpp->type() ), errorMessage ).toUtf8().constData() );
399 sipIsErr = 1;
400 }
401 else
402 {
403 PyObject *res = sipConvertFromType( a0, sipType_QVariant, NULL );
404 sipReleaseType( a0, sipType_QVariant, a0State );
405 return res;
406 }
407 }
408 else
409 {
410 // Raise an exception if the arguments couldn't be parsed.
411 sipNoMethod( sipParseErr, sipName_QgsField, sipName_convertCompatible, doc_QgsField_convertCompatible );
412
413 return nullptr;
414 }
415 }
416
417 % End
418#endif
419
421 operator QVariant() const
422 {
423 return QVariant::fromValue( *this );
424 }
425
431 void setEditorWidgetSetup( const QgsEditorWidgetSetup &v );
432
441 QgsEditorWidgetSetup editorWidgetSetup() const;
442
448 void setReadOnly( bool readOnly );
449
455 bool isReadOnly() const;
456
465 Qgis::FieldDomainSplitPolicy splitPolicy() const;
466
475 void setSplitPolicy( Qgis::FieldDomainSplitPolicy policy );
476
477#ifdef SIP_RUN
478 SIP_PYOBJECT __repr__();
479 % MethodCode
480 QString str = QStringLiteral( "<QgsField: %1 (%2)>" ).arg( sipCpp->name() ).arg( sipCpp->typeName() );
481 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
482 % End
483#endif
484
485#ifndef SIP_RUN
486 static constexpr int MAX_WKT_LENGTH = 999;
487#endif
488
489 private:
490
491 QSharedDataPointer<QgsFieldPrivate> d;
492
493
494}; // class QgsField
495
497
499
501CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsField &field );
503CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsField &field );
504
505
506#endif
FieldDomainSplitPolicy
Split policy for field domains.
Definition: qgis.h:2273
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:52
QString typeName() const
Gets the field type.
Definition: qgsfield.cpp:150
QString name
Definition: qgsfield.h:61
bool convertCompatible(QVariant &v, QString *errorMessage=nullptr) const
Converts the provided variant to a compatible format.
Definition: qgsfield.cpp:422
QVariant::Type type
Definition: qgsfield.h:59
virtual ~QgsField()
ConfigurationFlag
Configuration flags for fields These flags are meant to be user-configurable and are not describing a...
Definition: qgsfield.h:81
#define str(x)
Definition: qgis.cpp:37
#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:26
const QgsField & field
Definition: qgsfield.h:501
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