QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
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 <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
123 QString displayName() const;
124
136 QString displayNameWithAlias() const;
137
148 QString displayType( bool showConstraints = false ) const;
149
160 QString friendlyTypeString() const;
161
163 QVariant::Type type() const;
164
171 QVariant::Type subType() const;
172
179 QString typeName() const;
180
185 int length() const;
186
191 int precision() const;
192
196 QString comment() const;
197
206 QMap< int, QVariant > metadata() const;
207
214 QVariant metadata( Qgis::FieldMetadataProperty property ) const SIP_SKIP;
215
222 QVariant metadata( int property ) const;
223
232 void setMetadata( const QMap< int, QVariant > metadata );
233
240 void setMetadata( Qgis::FieldMetadataProperty property, const QVariant &value ) SIP_SKIP;
241
248 void setMetadata( int property, const QVariant &value );
249
256 bool isNumeric() const;
257
263 bool isDateOrTime() const;
264
269 void setName( const QString &name );
270
274 void setType( QVariant::Type type );
275
282 void setSubType( QVariant::Type subType );
283
288 void setTypeName( const QString &typeName );
289
294 void setLength( int len );
295
300 void setPrecision( int precision );
301
305 void setComment( const QString &comment );
306
314 QgsDefaultValue defaultValueDefinition() const;
315
323 void setDefaultValueDefinition( const QgsDefaultValue &defaultValueDefinition );
324
330 const QgsFieldConstraints &constraints() const;
331
337 void setConstraints( const QgsFieldConstraints &constraints );
338
345 QString alias() const;
346
353 void setAlias( const QString &alias );
354
360 Qgis::FieldConfigurationFlags configurationFlags() const;
361
367 void setConfigurationFlags( Qgis::FieldConfigurationFlags flags );
368
370 QString displayString( const QVariant &v ) const;
371
376 static QString readableConfigurationFlag( Qgis::FieldConfigurationFlag flag );
377
378#ifndef SIP_RUN
379
388 bool convertCompatible( QVariant &v, QString *errorMessage = nullptr ) const;
389#else
390
398 bool convertCompatible( QVariant &v ) const;
399 % MethodCode
400 PyObject *sipParseErr = NULL;
401
402 {
403 QVariant *a0;
404 int a0State = 0;
405 const QgsField *sipCpp;
406
407 if ( sipParseArgs( &sipParseErr, sipArgs, "BJ1", &sipSelf, sipType_QgsField, &sipCpp, sipType_QVariant, &a0, &a0State ) )
408 {
409 bool sipRes;
410 QString errorMessage;
411
412 Py_BEGIN_ALLOW_THREADS
413 try
414 {
415 sipRes = sipCpp->convertCompatible( *a0, &errorMessage );
416 }
417 catch ( ... )
418 {
419 Py_BLOCK_THREADS
420
421 sipReleaseType( a0, sipType_QVariant, a0State );
422 sipRaiseUnknownException();
423 return NULL;
424 }
425
426 Py_END_ALLOW_THREADS
427
428 if ( !sipRes )
429 {
430 PyErr_SetString( PyExc_ValueError,
431 QString( "Value could not be converted to field type %1: %2" ).arg( QMetaType::typeName( sipCpp->type() ), errorMessage ).toUtf8().constData() );
432 sipIsErr = 1;
433 }
434 else
435 {
436 PyObject *res = sipConvertFromType( a0, sipType_QVariant, NULL );
437 sipReleaseType( a0, sipType_QVariant, a0State );
438 return res;
439 }
440 }
441 else
442 {
443 // Raise an exception if the arguments couldn't be parsed.
444 sipNoMethod( sipParseErr, sipName_QgsField, sipName_convertCompatible, doc_QgsField_convertCompatible );
445
446 return nullptr;
447 }
448 }
449
450 % End
451#endif
452
454 operator QVariant() const
455 {
456 return QVariant::fromValue( *this );
457 }
458
464 void setEditorWidgetSetup( const QgsEditorWidgetSetup &v );
465
474 QgsEditorWidgetSetup editorWidgetSetup() const;
475
481 void setReadOnly( bool readOnly );
482
488 bool isReadOnly() const;
489
498 Qgis::FieldDomainSplitPolicy splitPolicy() const;
499
508 void setSplitPolicy( Qgis::FieldDomainSplitPolicy policy );
509
510#ifdef SIP_RUN
511 SIP_PYOBJECT __repr__();
512 % MethodCode
513 QString str = QStringLiteral( "<QgsField: %1 (%2)>" ).arg( sipCpp->name() ).arg( sipCpp->typeName() );
514 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
515 % End
516#endif
517
518#ifndef SIP_RUN
519 static constexpr int MAX_WKT_LENGTH = 999;
520#endif
521
522 private:
523
524 QSharedDataPointer<QgsFieldPrivate> d;
525
526
527}; // class QgsField
528
530
531
532CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsField &field );
534CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsField &field );
535
536
537#endif
The Qgis class provides global constants for use throughout the application.
Definition qgis.h:54
FieldDomainSplitPolicy
Split policy for field domains.
Definition qgis.h:2632
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
#define str(x)
Definition qgis.cpp:38
#define SIP_SKIP
Definition qgis_sip.h:126
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)
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