QGIS API Documentation 4.1.0-Master (009143bf4b4)
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
28using namespace Qt::StringLiterals;
29
30typedef QList<int> QgsAttributeList SIP_SKIP;
31
32/***************************************************************************
33 * This class is considered CRITICAL and any change MUST be accompanied with
34 * full unit tests in testqgsfield.cpp.
35 * See details in QEP #17
36 ****************************************************************************/
37
39#include "qgsfieldconstraints.h"
40#include "qgsdefaultvalue.h"
41#include "qgis.h"
42
43class QgsFieldPrivate;
44
54
55class CORE_EXPORT QgsField
56{
57 Q_GADGET
58
59 Q_PROPERTY( bool isNumeric READ isNumeric )
60 Q_PROPERTY( bool isDateOrTime READ isDateOrTime )
61 Q_PROPERTY( int length READ length WRITE setLength )
62 Q_PROPERTY( int precision READ precision WRITE setPrecision )
63 Q_PROPERTY( QMetaType::Type type READ type WRITE setType )
64 Q_PROPERTY( QString comment READ comment WRITE setComment )
65 Q_PROPERTY( QString name READ name WRITE setName )
66 Q_PROPERTY( QString alias READ alias WRITE setAlias )
69 Q_PROPERTY( Qgis::FieldConfigurationFlags configurationFlags READ configurationFlags WRITE setConfigurationFlags )
70 Q_PROPERTY( bool isReadOnly READ isReadOnly WRITE setReadOnly )
71 Q_PROPERTY( QString customComment READ customComment WRITE setCustomComment )
72
73 public:
74 // clang-format off
90 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 ) SIP_HOLDGIL;
91 // clang-format on
92
93
110 Q_DECL_DEPRECATED QgsField( const QString &name,
111 QVariant::Type type,
112 const QString &typeName = QString(),
113 int len = 0,
114 int prec = 0,
115 const QString &comment = QString(),
116 QVariant::Type subType = QVariant::Invalid ) SIP_HOLDGIL SIP_DEPRECATED;
117
118 QgsField( const QgsField &other ) SIP_HOLDGIL;
119 QgsField &operator =( const QgsField &other ) SIP_SKIP;
120
121 virtual ~QgsField();
122
123 bool operator==( const QgsField &other ) const SIP_HOLDGIL;
124 bool operator!=( const QgsField &other ) const SIP_HOLDGIL;
125
131 QString name() const SIP_HOLDGIL;
132
139 QString displayName() const SIP_HOLDGIL;
140
152 QString displayNameWithAlias() const SIP_HOLDGIL;
153
164 QString displayType( bool showConstraints = false ) const SIP_HOLDGIL;
165
176 QString friendlyTypeString() const SIP_HOLDGIL;
177
179 QMetaType::Type type() const SIP_HOLDGIL;
180
186 QMetaType::Type subType() const SIP_HOLDGIL;
187
194 QString typeName() const SIP_HOLDGIL;
195
200 int length() const SIP_HOLDGIL;
201
206 int precision() const SIP_HOLDGIL;
207
211 QString comment() const SIP_HOLDGIL;
212
221 QMap< int, QVariant > metadata() const SIP_HOLDGIL;
222
229 QVariant metadata( Qgis::FieldMetadataProperty property ) const SIP_SKIP;
230
237 QVariant metadata( int property ) const SIP_HOLDGIL;
238
247 void setMetadata( const QMap< int, QVariant > metadata ) SIP_HOLDGIL;
248
255 void setMetadata( Qgis::FieldMetadataProperty property, const QVariant &value ) SIP_SKIP;
256
263 void setMetadata( int property, const QVariant &value ) SIP_HOLDGIL;
264
269 bool isNumeric() const SIP_HOLDGIL;
270
276 bool isDateOrTime() const SIP_HOLDGIL;
277
282 void setName( const QString &name ) SIP_HOLDGIL;
283
287 void setType( QMetaType::Type type ) SIP_HOLDGIL;
288
293 Q_DECL_DEPRECATED void setType( QVariant::Type type ) SIP_HOLDGIL SIP_DEPRECATED;
294
300 void setSubType( QMetaType::Type subType ) SIP_HOLDGIL;
301
308 Q_DECL_DEPRECATED void setSubType( QVariant::Type subType ) SIP_HOLDGIL SIP_DEPRECATED;
309
314 void setTypeName( const QString &typeName ) SIP_HOLDGIL;
315
320 void setLength( int len ) SIP_HOLDGIL;
321
327
331 void setComment( const QString &comment ) SIP_HOLDGIL;
332
340
348
354
360
366 QString alias() const SIP_HOLDGIL;
367
373 void setAlias( const QString &alias ) SIP_HOLDGIL;
374
380 QString customComment() const SIP_HOLDGIL;
381
388 void setCustomComment( const QString &customComment ) SIP_HOLDGIL;
389
395 Qgis::FieldConfigurationFlags configurationFlags() const SIP_HOLDGIL;
396
402 void setConfigurationFlags( Qgis::FieldConfigurationFlags flags ) SIP_HOLDGIL;
403
405 QString displayString( const QVariant &v ) const;
406
411 static QString readableConfigurationFlag( Qgis::FieldConfigurationFlag flag ) SIP_HOLDGIL;
412
413#ifndef SIP_RUN
414
423 bool convertCompatible( QVariant &v, QString *errorMessage = nullptr ) const;
424#else
425// clang-format off
426
434 bool convertCompatible( QVariant &v ) const;
435 % MethodCode
436 PyObject *sipParseErr = NULL;
437
438 {
439 QVariant *a0;
440 int a0State = 0;
441 const QgsField *sipCpp;
442
443 if ( sipParseArgs( &sipParseErr, sipArgs, "BJ1", &sipSelf, sipType_QgsField, &sipCpp, sipType_QVariant, &a0, &a0State ) )
444 {
445 bool sipRes;
446 QString errorMessage;
447
448 Py_BEGIN_ALLOW_THREADS
449 try
450 {
451 sipRes = sipCpp->convertCompatible( *a0, &errorMessage );
452 }
453 catch ( ... )
454 {
455 Py_BLOCK_THREADS
456
457 sipReleaseType( a0, sipType_QVariant, a0State );
458 sipRaiseUnknownException();
459 return NULL;
460 }
461
462 Py_END_ALLOW_THREADS
463
464 if ( !sipRes )
465 {
466 PyErr_SetString( PyExc_ValueError,
467 QString( "Value could not be converted to field type %1: %2" ).arg( QMetaType::typeName( sipCpp->type() ), errorMessage ).toUtf8().constData() );
468 sipIsErr = 1;
469 }
470 else
471 {
472 PyObject *res = sipConvertFromType( a0, sipType_QVariant, NULL );
473 sipReleaseType( a0, sipType_QVariant, a0State );
474 return res;
475 }
476 }
477 else
478 {
479 // Raise an exception if the arguments couldn't be parsed.
480 sipNoMethod( sipParseErr, sipName_QgsField, sipName_convertCompatible, doc_QgsField_convertCompatible );
481
482 return nullptr;
483 }
484 }
485
486 % End
487// clang-format on
488#endif
489
491 operator QVariant() const;
492
498 void setEditorWidgetSetup( const QgsEditorWidgetSetup &v ) SIP_HOLDGIL;
499
508 QgsEditorWidgetSetup editorWidgetSetup() const SIP_HOLDGIL;
509
515 void setReadOnly( bool readOnly ) SIP_HOLDGIL;
516
522 bool isReadOnly() const SIP_HOLDGIL;
523
532 Qgis::FieldDomainSplitPolicy splitPolicy() const SIP_HOLDGIL;
533
542 void setSplitPolicy( Qgis::FieldDomainSplitPolicy policy ) SIP_HOLDGIL;
543
552 Qgis::FieldDuplicatePolicy duplicatePolicy() const SIP_HOLDGIL;
553
562 void setDuplicatePolicy( Qgis::FieldDuplicatePolicy policy ) SIP_HOLDGIL;
563
572 Qgis::FieldDomainMergePolicy mergePolicy() const SIP_HOLDGIL;
573
582 void setMergePolicy( Qgis::FieldDomainMergePolicy policy ) SIP_HOLDGIL;
583
584#ifdef SIP_RUN
585// clang-format off
586 SIP_PYOBJECT __repr__();
587 % MethodCode
588 QString str = u"<QgsField: %1 (%2)>"_s.arg( sipCpp->name() ).arg( sipCpp->typeName() );
589 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
590 % End
591// clang-format on
592#endif
593
594#ifndef SIP_RUN
595 static constexpr int MAX_WKT_LENGTH = 999;
596#endif
597
598 private:
599
600 QSharedDataPointer<QgsFieldPrivate> d;
601
602
603}; // class QgsField
604
606
607
608CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsField &field );
610CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsField &field );
611
612
613#endif
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:62
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:56
QMetaType::Type type
Definition qgsfield.h:63
bool isDateOrTime
Definition qgsfield.h:60
QString typeName() const
Gets the field type.
Definition qgsfield.cpp:158
void setConstraints(const QgsFieldConstraints &constraints)
Sets constraints which are present for the field.
Definition qgsfield.cpp:278
void setAlias(const QString &alias)
Sets the alias for the field (the friendly displayed name of the field ).
Definition qgsfield.cpp:293
QString name
Definition qgsfield.h:65
int precision
Definition qgsfield.h:62
int length
Definition qgsfield.h:61
QString displayString(const QVariant &v) const
Formats string for display.
Definition qgsfield.cpp:324
void setPrecision(int precision)
Set the field precision.
Definition qgsfield.cpp:258
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:104
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:54
bool convertCompatible(QVariant &v, QString *errorMessage=nullptr) const
Converts the provided variant to a compatible format.
Definition qgsfield.cpp:480
void setSubType(QMetaType::Type subType)
If the field is a collection, set its element's type.
Definition qgsfield.cpp:239
void setName(const QString &name)
Set the field name.
Definition qgsfield.cpp:224
void setComment(const QString &comment)
Set the field comment.
Definition qgsfield.cpp:263
void setType(QMetaType::Type type)
Set variant type.
Definition qgsfield.cpp:229
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:113
void setConfigurationFlags(Qgis::FieldConfigurationFlags flags)
Sets the Flags for the field (searchable, …).
Definition qgsfield.cpp:313
void setLength(int len)
Set the field length.
Definition qgsfield.cpp:254
QString displayName() const
Returns the name to use when displaying this field.
Definition qgsfield.cpp:96
void setDefaultValueDefinition(const QgsDefaultValue &defaultValueDefinition)
Sets an expression to use when calculating the default value for the field.
Definition qgsfield.cpp:273
QString friendlyTypeString() const
Returns a user friendly, translated representation of the field type.
Definition qgsfield.cpp:136
void setReadOnly(bool readOnly)
Make field read-only if readOnly is set to true.
Definition qgsfield.cpp:754
QMap< int, QVariant > metadata() const
Returns the map of field metadata.
Definition qgsfield.cpp:183
static QString readableConfigurationFlag(Qgis::FieldConfigurationFlag flag)
Returns the readable and translated value of the configuration flag.
Definition qgsfield.cpp:458
void setCustomComment(const QString &customComment)
Sets the custom comment for the field.
Definition qgsfield.cpp:303
Qgis::FieldConfigurationFlags configurationFlags
Definition qgsfield.h:69
QMetaType::Type subType() const
If the field is a collection, gets its element's type.
Definition qgsfield.cpp:153
QString alias
Definition qgsfield.h:66
static constexpr int MAX_WKT_LENGTH
Definition qgsfield.h:595
QgsDefaultValue defaultValueDefinition
Definition qgsfield.h:67
bool isNumeric
Definition qgsfield.h:59
QString customComment
Definition qgsfield.h:71
void setMetadata(const QMap< int, QVariant > metadata)
Sets the map of field metadata.
Definition qgsfield.cpp:193
QString comment
Definition qgsfield.h:64
QgsFieldConstraints constraints
Definition qgsfield.h:68
bool isReadOnly
Definition qgsfield.h:70
void setTypeName(const QString &typeName)
Set the field type.
Definition qgsfield.cpp:249
#define SIP_DEPRECATED
Definition qgis_sip.h:113
#define SIP_SKIP
Definition qgis_sip.h:133
#define SIP_HOLDGIL
Definition qgis_sip.h:178
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)
QList< int > QgsAttributeList
Definition qgsfield.h:30
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:827
CORE_EXPORT QDataStream & operator<<(QDataStream &out, const QgsField &field)
Writes the field to stream out. QGIS version compatibility is not guaranteed.
Definition qgsfield.cpp:800