QGIS API Documentation 3.99.0-Master (d270888f95f)
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
72 public:
73
89 QgsField( const QString &name = QString(),
90 QMetaType::Type type = QMetaType::Type::UnknownType,
91 const QString &typeName = QString(),
92 int len = 0,
93 int prec = 0,
94 const QString &comment = QString(),
95 QMetaType::Type subType = QMetaType::Type::UnknownType ) SIP_HOLDGIL;
96
97
114 Q_DECL_DEPRECATED QgsField( const QString &name,
115 QVariant::Type type,
116 const QString &typeName = QString(),
117 int len = 0,
118 int prec = 0,
119 const QString &comment = QString(),
120 QVariant::Type subType = QVariant::Invalid ) SIP_HOLDGIL SIP_DEPRECATED;
121
122 QgsField( const QgsField &other ) SIP_HOLDGIL;
123 QgsField &operator =( const QgsField &other ) SIP_SKIP;
124
125 virtual ~QgsField();
126
127 bool operator==( const QgsField &other ) const SIP_HOLDGIL;
128 bool operator!=( const QgsField &other ) const SIP_HOLDGIL;
129
135 QString name() const SIP_HOLDGIL;
136
143 QString displayName() const SIP_HOLDGIL;
144
156 QString displayNameWithAlias() const SIP_HOLDGIL;
157
168 QString displayType( bool showConstraints = false ) const SIP_HOLDGIL;
169
180 QString friendlyTypeString() const SIP_HOLDGIL;
181
183 QMetaType::Type type() const SIP_HOLDGIL;
184
190 QMetaType::Type subType() const SIP_HOLDGIL;
191
198 QString typeName() const SIP_HOLDGIL;
199
204 int length() const SIP_HOLDGIL;
205
210 int precision() const SIP_HOLDGIL;
211
215 QString comment() const SIP_HOLDGIL;
216
225 QMap< int, QVariant > metadata() const SIP_HOLDGIL;
226
233 QVariant metadata( Qgis::FieldMetadataProperty property ) const SIP_SKIP;
234
241 QVariant metadata( int property ) const SIP_HOLDGIL;
242
251 void setMetadata( const QMap< int, QVariant > metadata ) SIP_HOLDGIL;
252
259 void setMetadata( Qgis::FieldMetadataProperty property, const QVariant &value ) SIP_SKIP;
260
267 void setMetadata( int property, const QVariant &value ) SIP_HOLDGIL;
268
273 bool isNumeric() const SIP_HOLDGIL;
274
280 bool isDateOrTime() const SIP_HOLDGIL;
281
286 void setName( const QString &name ) SIP_HOLDGIL;
287
291 void setType( QMetaType::Type type ) SIP_HOLDGIL;
292
297 Q_DECL_DEPRECATED void setType( QVariant::Type type ) SIP_HOLDGIL SIP_DEPRECATED;
298
304 void setSubType( QMetaType::Type subType ) SIP_HOLDGIL;
305
312 Q_DECL_DEPRECATED void setSubType( QVariant::Type subType ) SIP_HOLDGIL SIP_DEPRECATED;
313
318 void setTypeName( const QString &typeName ) SIP_HOLDGIL;
319
324 void setLength( int len ) SIP_HOLDGIL;
325
331
335 void setComment( const QString &comment ) SIP_HOLDGIL;
336
344
352
358
364
370 QString alias() const SIP_HOLDGIL;
371
377 void setAlias( const QString &alias ) SIP_HOLDGIL;
378
384 Qgis::FieldConfigurationFlags configurationFlags() const SIP_HOLDGIL;
385
391 void setConfigurationFlags( Qgis::FieldConfigurationFlags flags ) SIP_HOLDGIL;
392
394 QString displayString( const QVariant &v ) const;
395
400 static QString readableConfigurationFlag( Qgis::FieldConfigurationFlag flag ) SIP_HOLDGIL;
401
402#ifndef SIP_RUN
403
412 bool convertCompatible( QVariant &v, QString *errorMessage = nullptr ) const;
413#else
414
422 bool convertCompatible( QVariant &v ) const;
423 % MethodCode
424 PyObject *sipParseErr = NULL;
425
426 {
427 QVariant *a0;
428 int a0State = 0;
429 const QgsField *sipCpp;
430
431 if ( sipParseArgs( &sipParseErr, sipArgs, "BJ1", &sipSelf, sipType_QgsField, &sipCpp, sipType_QVariant, &a0, &a0State ) )
432 {
433 bool sipRes;
434 QString errorMessage;
435
436 Py_BEGIN_ALLOW_THREADS
437 try
438 {
439 sipRes = sipCpp->convertCompatible( *a0, &errorMessage );
440 }
441 catch ( ... )
442 {
443 Py_BLOCK_THREADS
444
445 sipReleaseType( a0, sipType_QVariant, a0State );
446 sipRaiseUnknownException();
447 return NULL;
448 }
449
450 Py_END_ALLOW_THREADS
451
452 if ( !sipRes )
453 {
454 PyErr_SetString( PyExc_ValueError,
455 QString( "Value could not be converted to field type %1: %2" ).arg( QMetaType::typeName( sipCpp->type() ), errorMessage ).toUtf8().constData() );
456 sipIsErr = 1;
457 }
458 else
459 {
460 PyObject *res = sipConvertFromType( a0, sipType_QVariant, NULL );
461 sipReleaseType( a0, sipType_QVariant, a0State );
462 return res;
463 }
464 }
465 else
466 {
467 // Raise an exception if the arguments couldn't be parsed.
468 sipNoMethod( sipParseErr, sipName_QgsField, sipName_convertCompatible, doc_QgsField_convertCompatible );
469
470 return nullptr;
471 }
472 }
473
474 % End
475#endif
476
478 operator QVariant() const;
479
485 void setEditorWidgetSetup( const QgsEditorWidgetSetup &v ) SIP_HOLDGIL;
486
495 QgsEditorWidgetSetup editorWidgetSetup() const SIP_HOLDGIL;
496
502 void setReadOnly( bool readOnly ) SIP_HOLDGIL;
503
509 bool isReadOnly() const SIP_HOLDGIL;
510
519 Qgis::FieldDomainSplitPolicy splitPolicy() const SIP_HOLDGIL;
520
529 void setSplitPolicy( Qgis::FieldDomainSplitPolicy policy ) SIP_HOLDGIL;
530
539 Qgis::FieldDuplicatePolicy duplicatePolicy() const SIP_HOLDGIL;
540
549 void setDuplicatePolicy( Qgis::FieldDuplicatePolicy policy ) SIP_HOLDGIL;
550
559 Qgis::FieldDomainMergePolicy mergePolicy() const SIP_HOLDGIL;
560
569 void setMergePolicy( Qgis::FieldDomainMergePolicy policy ) SIP_HOLDGIL;
570
571#ifdef SIP_RUN
572 SIP_PYOBJECT __repr__();
573 % MethodCode
574 QString str = u"<QgsField: %1 (%2)>"_s.arg( sipCpp->name() ).arg( sipCpp->typeName() );
575 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
576 % End
577#endif
578
579#ifndef SIP_RUN
580 static constexpr int MAX_WKT_LENGTH = 999;
581#endif
582
583 private:
584
585 QSharedDataPointer<QgsFieldPrivate> d;
586
587
588}; // class QgsField
589
591
592
593CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsField &field );
595CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsField &field );
596
597
598#endif
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:59
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:167
void setConstraints(const QgsFieldConstraints &constraints)
Sets constraints which are present for the field.
Definition qgsfield.cpp:287
void setAlias(const QString &alias)
Sets the alias for the field (the friendly displayed name of the field ).
Definition qgsfield.cpp:302
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:323
void setPrecision(int precision)
Set the field precision.
Definition qgsfield.cpp:267
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:109
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:479
void setSubType(QMetaType::Type subType)
If the field is a collection, set its element's type.
Definition qgsfield.cpp:248
void setName(const QString &name)
Set the field name.
Definition qgsfield.cpp:233
void setComment(const QString &comment)
Set the field comment.
Definition qgsfield.cpp:272
void setType(QMetaType::Type type)
Set variant type.
Definition qgsfield.cpp:238
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:118
void setConfigurationFlags(Qgis::FieldConfigurationFlags flags)
Sets the Flags for the field (searchable, …).
Definition qgsfield.cpp:312
void setLength(int len)
Set the field length.
Definition qgsfield.cpp:263
QString displayName() const
Returns the name to use when displaying this field.
Definition qgsfield.cpp:101
void setDefaultValueDefinition(const QgsDefaultValue &defaultValueDefinition)
Sets an expression to use when calculating the default value for the field.
Definition qgsfield.cpp:282
QString friendlyTypeString() const
Returns a user friendly, translated representation of the field type.
Definition qgsfield.cpp:145
void setReadOnly(bool readOnly)
Make field read-only if readOnly is set to true.
Definition qgsfield.cpp:756
QMap< int, QVariant > metadata() const
Returns the map of field metadata.
Definition qgsfield.cpp:192
static QString readableConfigurationFlag(Qgis::FieldConfigurationFlag flag)
Returns the readable and translated value of the configuration flag.
Definition qgsfield.cpp:457
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:162
QString alias
Definition qgsfield.h:66
static constexpr int MAX_WKT_LENGTH
Definition qgsfield.h:580
QgsDefaultValue defaultValueDefinition
Definition qgsfield.h:67
bool isNumeric
Definition qgsfield.h:59
void setMetadata(const QMap< int, QVariant > metadata)
Sets the map of field metadata.
Definition qgsfield.cpp:202
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:258
#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: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:829
CORE_EXPORT QDataStream & operator<<(QDataStream &out, const QgsField &field)
Writes the field to stream out. QGIS version compatibility is not guaranteed.
Definition qgsfield.cpp:802