QGIS API Documentation 3.39.0-Master (52f98f8c831)
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( QMetaType::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 QMetaType::Type type = QMetaType::Type::UnknownType,
88 const QString &typeName = QString(),
89 int len = 0,
90 int prec = 0,
91 const QString &comment = QString(),
92 QMetaType::Type subType = QMetaType::Type::UnknownType ) SIP_HOLDGIL;
93
94
111 Q_DECL_DEPRECATED QgsField( const QString &name,
112 QVariant::Type type,
113 const QString &typeName = QString(),
114 int len = 0,
115 int prec = 0,
116 const QString &comment = QString(),
117 QVariant::Type subType = QVariant::Invalid ) SIP_HOLDGIL SIP_DEPRECATED;
118
122 QgsField( const QgsField &other ) SIP_HOLDGIL;
123
127 QgsField &operator =( const QgsField &other ) SIP_SKIP;
128
129 virtual ~QgsField();
130
131 bool operator==( const QgsField &other ) const SIP_HOLDGIL;
132 bool operator!=( const QgsField &other ) const SIP_HOLDGIL;
133
139 QString name() const SIP_HOLDGIL;
140
147 QString displayName() const SIP_HOLDGIL;
148
160 QString displayNameWithAlias() const SIP_HOLDGIL;
161
172 QString displayType( bool showConstraints = false ) const SIP_HOLDGIL;
173
184 QString friendlyTypeString() const SIP_HOLDGIL;
185
187 QMetaType::Type type() const SIP_HOLDGIL;
188
194 QMetaType::Type subType() const SIP_HOLDGIL;
195
202 QString typeName() const SIP_HOLDGIL;
203
208 int length() const SIP_HOLDGIL;
209
214 int precision() const SIP_HOLDGIL;
215
219 QString comment() const SIP_HOLDGIL;
220
229 QMap< int, QVariant > metadata() const SIP_HOLDGIL;
230
237 QVariant metadata( Qgis::FieldMetadataProperty property ) const SIP_SKIP;
238
245 QVariant metadata( int property ) const SIP_HOLDGIL;
246
255 void setMetadata( const QMap< int, QVariant > metadata ) SIP_HOLDGIL;
256
263 void setMetadata( Qgis::FieldMetadataProperty property, const QVariant &value ) SIP_SKIP;
264
271 void setMetadata( int property, const QVariant &value ) SIP_HOLDGIL;
272
277 bool isNumeric() const SIP_HOLDGIL;
278
284 bool isDateOrTime() const SIP_HOLDGIL;
285
290 void setName( const QString &name ) SIP_HOLDGIL;
291
295 void setType( QMetaType::Type type ) SIP_HOLDGIL;
296
301 Q_DECL_DEPRECATED void setType( QVariant::Type type ) SIP_HOLDGIL SIP_DEPRECATED;
302
308 void setSubType( QMetaType::Type subType ) SIP_HOLDGIL;
309
316 Q_DECL_DEPRECATED void setSubType( QVariant::Type subType ) SIP_HOLDGIL SIP_DEPRECATED;
317
322 void setTypeName( const QString &typeName ) SIP_HOLDGIL;
323
328 void setLength( int len ) SIP_HOLDGIL;
329
334 void setPrecision( int precision ) SIP_HOLDGIL;
335
339 void setComment( const QString &comment ) SIP_HOLDGIL;
340
347 QgsDefaultValue defaultValueDefinition() const SIP_HOLDGIL;
348
355 void setDefaultValueDefinition( const QgsDefaultValue &defaultValueDefinition ) SIP_HOLDGIL;
356
361 const QgsFieldConstraints &constraints() const SIP_HOLDGIL;
362
367 void setConstraints( const QgsFieldConstraints &constraints ) SIP_HOLDGIL;
368
374 QString alias() const SIP_HOLDGIL;
375
381 void setAlias( const QString &alias ) SIP_HOLDGIL;
382
388 Qgis::FieldConfigurationFlags configurationFlags() const SIP_HOLDGIL;
389
395 void setConfigurationFlags( Qgis::FieldConfigurationFlags flags ) SIP_HOLDGIL;
396
398 QString displayString( const QVariant &v ) const;
399
404 static QString readableConfigurationFlag( Qgis::FieldConfigurationFlag flag ) SIP_HOLDGIL;
405
406#ifndef SIP_RUN
407
416 bool convertCompatible( QVariant &v, QString *errorMessage = nullptr ) const;
417#else
418
426 bool convertCompatible( QVariant &v ) const;
427 % MethodCode
428 PyObject *sipParseErr = NULL;
429
430 {
431 QVariant *a0;
432 int a0State = 0;
433 const QgsField *sipCpp;
434
435 if ( sipParseArgs( &sipParseErr, sipArgs, "BJ1", &sipSelf, sipType_QgsField, &sipCpp, sipType_QVariant, &a0, &a0State ) )
436 {
437 bool sipRes;
438 QString errorMessage;
439
440 Py_BEGIN_ALLOW_THREADS
441 try
442 {
443 sipRes = sipCpp->convertCompatible( *a0, &errorMessage );
444 }
445 catch ( ... )
446 {
447 Py_BLOCK_THREADS
448
449 sipReleaseType( a0, sipType_QVariant, a0State );
450 sipRaiseUnknownException();
451 return NULL;
452 }
453
454 Py_END_ALLOW_THREADS
455
456 if ( !sipRes )
457 {
458 PyErr_SetString( PyExc_ValueError,
459 QString( "Value could not be converted to field type %1: %2" ).arg( QMetaType::typeName( sipCpp->type() ), errorMessage ).toUtf8().constData() );
460 sipIsErr = 1;
461 }
462 else
463 {
464 PyObject *res = sipConvertFromType( a0, sipType_QVariant, NULL );
465 sipReleaseType( a0, sipType_QVariant, a0State );
466 return res;
467 }
468 }
469 else
470 {
471 // Raise an exception if the arguments couldn't be parsed.
472 sipNoMethod( sipParseErr, sipName_QgsField, sipName_convertCompatible, doc_QgsField_convertCompatible );
473
474 return nullptr;
475 }
476 }
477
478 % End
479#endif
480
482 operator QVariant() const;
483
489 void setEditorWidgetSetup( const QgsEditorWidgetSetup &v ) SIP_HOLDGIL;
490
499 QgsEditorWidgetSetup editorWidgetSetup() const SIP_HOLDGIL;
500
506 void setReadOnly( bool readOnly ) SIP_HOLDGIL;
507
513 bool isReadOnly() const SIP_HOLDGIL;
514
523 Qgis::FieldDomainSplitPolicy splitPolicy() const SIP_HOLDGIL;
524
533 void setSplitPolicy( Qgis::FieldDomainSplitPolicy policy ) SIP_HOLDGIL;
534
543 Qgis::FieldDuplicatePolicy duplicatePolicy() const SIP_HOLDGIL;
544
553 void setDuplicatePolicy( Qgis::FieldDuplicatePolicy policy ) SIP_HOLDGIL;
554
555#ifdef SIP_RUN
556 SIP_PYOBJECT __repr__();
557 % MethodCode
558 QString str = QStringLiteral( "<QgsField: %1 (%2)>" ).arg( sipCpp->name() ).arg( sipCpp->typeName() );
559 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
560 % End
561#endif
562
563#ifndef SIP_RUN
564 static constexpr int MAX_WKT_LENGTH = 999;
565#endif
566
567 private:
568
569 QSharedDataPointer<QgsFieldPrivate> d;
570
571
572}; // class QgsField
573
575
576
577CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsField &field );
579CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsField &field );
580
581
582#endif
The Qgis class provides global constants for use throughout the application.
Definition qgis.h:54
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
QMetaType::Type type
Definition qgsfield.h:60
QString typeName() const
Gets the field type.
Definition qgsfield.cpp:160
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:472
#define str(x)
Definition qgis.cpp:38
#define SIP_DEPRECATED
Definition qgis_sip.h:106
#define SIP_SKIP
Definition qgis_sip.h:126
#define SIP_HOLDGIL
Definition qgis_sip.h:171
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:807
CORE_EXPORT QDataStream & operator<<(QDataStream &out, const QgsField &field)
Writes the field to stream out. QGIS version compatibility is not guaranteed.
Definition qgsfield.cpp:780
const QString & typeName
int precision