QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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
119 QgsField( const QgsField &other ) SIP_HOLDGIL;
120 QgsField &operator =( const QgsField &other ) SIP_SKIP;
121
122 virtual ~QgsField();
123
124 bool operator==( const QgsField &other ) const SIP_HOLDGIL;
125 bool operator!=( const QgsField &other ) const SIP_HOLDGIL;
126
132 QString name() const SIP_HOLDGIL;
133
140 QString displayName() const SIP_HOLDGIL;
141
153 QString displayNameWithAlias() const SIP_HOLDGIL;
154
165 QString displayType( bool showConstraints = false ) const SIP_HOLDGIL;
166
177 QString friendlyTypeString() const SIP_HOLDGIL;
178
180 QMetaType::Type type() const SIP_HOLDGIL;
181
187 QMetaType::Type subType() const SIP_HOLDGIL;
188
195 QString typeName() const SIP_HOLDGIL;
196
201 int length() const SIP_HOLDGIL;
202
207 int precision() const SIP_HOLDGIL;
208
212 QString comment() const SIP_HOLDGIL;
213
222 QMap< int, QVariant > metadata() const SIP_HOLDGIL;
223
230 QVariant metadata( Qgis::FieldMetadataProperty property ) const SIP_SKIP;
231
238 QVariant metadata( int property ) const SIP_HOLDGIL;
239
248 void setMetadata( const QMap< int, QVariant > metadata ) SIP_HOLDGIL;
249
256 void setMetadata( Qgis::FieldMetadataProperty property, const QVariant &value ) SIP_SKIP;
257
264 void setMetadata( int property, const QVariant &value ) SIP_HOLDGIL;
265
270 bool isNumeric() const SIP_HOLDGIL;
271
277 bool isDateOrTime() const SIP_HOLDGIL;
278
283 void setName( const QString &name ) SIP_HOLDGIL;
284
288 void setType( QMetaType::Type type ) SIP_HOLDGIL;
289
294 Q_DECL_DEPRECATED void setType( QVariant::Type type ) SIP_HOLDGIL SIP_DEPRECATED;
295
301 void setSubType( QMetaType::Type subType ) SIP_HOLDGIL;
302
309 Q_DECL_DEPRECATED void setSubType( QVariant::Type subType ) SIP_HOLDGIL SIP_DEPRECATED;
310
315 void setTypeName( const QString &typeName ) SIP_HOLDGIL;
316
321 void setLength( int len ) SIP_HOLDGIL;
322
327 void setPrecision( int precision ) SIP_HOLDGIL;
328
332 void setComment( const QString &comment ) SIP_HOLDGIL;
333
340 QgsDefaultValue defaultValueDefinition() const SIP_HOLDGIL;
341
348 void setDefaultValueDefinition( const QgsDefaultValue &defaultValueDefinition ) SIP_HOLDGIL;
349
354 const QgsFieldConstraints &constraints() const SIP_HOLDGIL;
355
360 void setConstraints( const QgsFieldConstraints &constraints ) SIP_HOLDGIL;
361
367 QString alias() const SIP_HOLDGIL;
368
374 void setAlias( const QString &alias ) SIP_HOLDGIL;
375
381 Qgis::FieldConfigurationFlags configurationFlags() const SIP_HOLDGIL;
382
388 void setConfigurationFlags( Qgis::FieldConfigurationFlags flags ) SIP_HOLDGIL;
389
391 QString displayString( const QVariant &v ) const;
392
397 static QString readableConfigurationFlag( Qgis::FieldConfigurationFlag flag ) SIP_HOLDGIL;
398
399#ifndef SIP_RUN
400
409 bool convertCompatible( QVariant &v, QString *errorMessage = nullptr ) const;
410#else
411
419 bool convertCompatible( QVariant &v ) const;
420 % MethodCode
421 PyObject *sipParseErr = NULL;
422
423 {
424 QVariant *a0;
425 int a0State = 0;
426 const QgsField *sipCpp;
427
428 if ( sipParseArgs( &sipParseErr, sipArgs, "BJ1", &sipSelf, sipType_QgsField, &sipCpp, sipType_QVariant, &a0, &a0State ) )
429 {
430 bool sipRes;
431 QString errorMessage;
432
433 Py_BEGIN_ALLOW_THREADS
434 try
435 {
436 sipRes = sipCpp->convertCompatible( *a0, &errorMessage );
437 }
438 catch ( ... )
439 {
440 Py_BLOCK_THREADS
441
442 sipReleaseType( a0, sipType_QVariant, a0State );
443 sipRaiseUnknownException();
444 return NULL;
445 }
446
447 Py_END_ALLOW_THREADS
448
449 if ( !sipRes )
450 {
451 PyErr_SetString( PyExc_ValueError,
452 QString( "Value could not be converted to field type %1: %2" ).arg( QMetaType::typeName( sipCpp->type() ), errorMessage ).toUtf8().constData() );
453 sipIsErr = 1;
454 }
455 else
456 {
457 PyObject *res = sipConvertFromType( a0, sipType_QVariant, NULL );
458 sipReleaseType( a0, sipType_QVariant, a0State );
459 return res;
460 }
461 }
462 else
463 {
464 // Raise an exception if the arguments couldn't be parsed.
465 sipNoMethod( sipParseErr, sipName_QgsField, sipName_convertCompatible, doc_QgsField_convertCompatible );
466
467 return nullptr;
468 }
469 }
470
471 % End
472#endif
473
475 operator QVariant() const;
476
482 void setEditorWidgetSetup( const QgsEditorWidgetSetup &v ) SIP_HOLDGIL;
483
492 QgsEditorWidgetSetup editorWidgetSetup() const SIP_HOLDGIL;
493
499 void setReadOnly( bool readOnly ) SIP_HOLDGIL;
500
506 bool isReadOnly() const SIP_HOLDGIL;
507
516 Qgis::FieldDomainSplitPolicy splitPolicy() const SIP_HOLDGIL;
517
526 void setSplitPolicy( Qgis::FieldDomainSplitPolicy policy ) SIP_HOLDGIL;
527
536 Qgis::FieldDuplicatePolicy duplicatePolicy() const SIP_HOLDGIL;
537
546 void setDuplicatePolicy( Qgis::FieldDuplicatePolicy policy ) SIP_HOLDGIL;
547
548#ifdef SIP_RUN
549 SIP_PYOBJECT __repr__();
550 % MethodCode
551 QString str = QStringLiteral( "<QgsField: %1 (%2)>" ).arg( sipCpp->name() ).arg( sipCpp->typeName() );
552 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
553 % End
554#endif
555
556#ifndef SIP_RUN
557 static constexpr int MAX_WKT_LENGTH = 999;
558#endif
559
560 private:
561
562 QSharedDataPointer<QgsFieldPrivate> d;
563
564
565}; // class QgsField
566
568
569
570CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsField &field );
572CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsField &field );
573
574
575#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:161
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:473
#define str(x)
Definition qgis.cpp:39
#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:808
CORE_EXPORT QDataStream & operator<<(QDataStream &out, const QgsField &field)
Writes the field to stream out. QGIS version compatibility is not guaranteed.
Definition qgsfield.cpp:781
const QString & typeName
int precision