QGIS API Documentation 3.28.14-Firenze (exported)
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
26typedef QList<int> QgsAttributeList SIP_SKIP;
27
28/***************************************************************************
29 * This class is considered CRITICAL and any change MUST be accompanied with
30 * full unit tests in testqgsfield.cpp.
31 * See details in QEP #17
32 ****************************************************************************/
33
35#include "qgsfieldconstraints.h"
36#include "qgsdefaultvalue.h"
37
38class QgsFieldPrivate;
39
50class CORE_EXPORT QgsField
51{
52 Q_GADGET
53
54 Q_PROPERTY( bool isNumeric READ isNumeric )
55 Q_PROPERTY( bool isDateOrTime READ isDateOrTime )
56 Q_PROPERTY( int length READ length WRITE setLength )
57 Q_PROPERTY( int precision READ precision WRITE setPrecision )
58 Q_PROPERTY( QVariant::Type type READ type WRITE setType )
59 Q_PROPERTY( QString comment READ comment WRITE setComment )
60 Q_PROPERTY( QString name READ name WRITE setName )
61 Q_PROPERTY( QString alias READ alias WRITE setAlias )
62 Q_PROPERTY( QgsDefaultValue defaultValueDefinition READ defaultValueDefinition WRITE setDefaultValueDefinition )
63 Q_PROPERTY( QgsFieldConstraints constraints READ constraints WRITE setConstraints )
64 Q_PROPERTY( ConfigurationFlags configurationFlags READ configurationFlags WRITE setConfigurationFlags )
65 Q_PROPERTY( bool isReadOnly READ isReadOnly WRITE setReadOnly )
66
67
68 public:
69
70#ifndef SIP_RUN
71
79 enum class ConfigurationFlag : int
80 {
81 None = 0,
82 NotSearchable = 1 << 1,
83 HideFromWms = 1 << 2,
84 HideFromWfs = 1 << 3,
85 };
86 Q_ENUM( ConfigurationFlag )
87 Q_DECLARE_FLAGS( ConfigurationFlags, ConfigurationFlag )
88 Q_FLAG( ConfigurationFlags )
89#endif
90
106 QgsField( const QString &name = QString(),
107 QVariant::Type type = QVariant::Invalid,
108 const QString &typeName = QString(),
109 int len = 0,
110 int prec = 0,
111 const QString &comment = QString(),
112 QVariant::Type subType = QVariant::Invalid );
113
117 QgsField( const QgsField &other );
118
122 QgsField &operator =( const QgsField &other ) SIP_SKIP;
123
124 virtual ~QgsField();
125
126 bool operator==( const QgsField &other ) const;
127 bool operator!=( const QgsField &other ) const;
128
134 QString name() const;
135
143 QString displayName() const;
144
156 QString displayNameWithAlias() const;
157
168 QString displayType( bool showConstraints = false ) const;
169
180 QString friendlyTypeString() const;
181
183 QVariant::Type type() const;
184
191 QVariant::Type subType() const;
192
199 QString typeName() const;
200
205 int length() const;
206
211 int precision() const;
212
216 QString comment() const;
217
224 bool isNumeric() const;
225
231 bool isDateOrTime() const;
232
237 void setName( const QString &name );
238
242 void setType( QVariant::Type type );
243
250 void setSubType( QVariant::Type subType );
251
256 void setTypeName( const QString &typeName );
257
262 void setLength( int len );
263
268 void setPrecision( int precision );
269
273 void setComment( const QString &comment );
274
282 QgsDefaultValue defaultValueDefinition() const;
283
291 void setDefaultValueDefinition( const QgsDefaultValue &defaultValueDefinition );
292
298 const QgsFieldConstraints &constraints() const;
299
305 void setConstraints( const QgsFieldConstraints &constraints );
306
313 QString alias() const;
314
321 void setAlias( const QString &alias );
322
327 QgsField::ConfigurationFlags configurationFlags() const SIP_SKIP;
328
333 void setConfigurationFlags( QgsField::ConfigurationFlags configurationFlags ) SIP_SKIP;
334
336 QString displayString( const QVariant &v ) const;
337
342 static QString readableConfigurationFlag( QgsField::ConfigurationFlag flag ) SIP_SKIP;
343
344#ifndef SIP_RUN
345
354 bool convertCompatible( QVariant &v, QString *errorMessage = nullptr ) const;
355#else
356
364 bool convertCompatible( QVariant &v ) const;
365 % MethodCode
366 PyObject *sipParseErr = NULL;
367
368 {
369 QVariant *a0;
370 int a0State = 0;
371 const QgsField *sipCpp;
372
373 if ( sipParseArgs( &sipParseErr, sipArgs, "BJ1", &sipSelf, sipType_QgsField, &sipCpp, sipType_QVariant, &a0, &a0State ) )
374 {
375 bool sipRes;
376 QString errorMessage;
377
378 Py_BEGIN_ALLOW_THREADS
379 try
380 {
381 sipRes = sipCpp->convertCompatible( *a0, &errorMessage );
382 }
383 catch ( ... )
384 {
385 Py_BLOCK_THREADS
386
387 sipReleaseType( a0, sipType_QVariant, a0State );
388 sipRaiseUnknownException();
389 return NULL;
390 }
391
392 Py_END_ALLOW_THREADS
393
394 if ( !sipRes )
395 {
396 PyErr_SetString( PyExc_ValueError,
397 QString( "Value could not be converted to field type %1: %2" ).arg( QMetaType::typeName( sipCpp->type() ), errorMessage ).toUtf8().constData() );
398 sipIsErr = 1;
399 }
400 else
401 {
402 PyObject *res = sipConvertFromType( a0, sipType_QVariant, NULL );
403 sipReleaseType( a0, sipType_QVariant, a0State );
404 return res;
405 }
406 }
407 else
408 {
409 // Raise an exception if the arguments couldn't be parsed.
410 sipNoMethod( sipParseErr, sipName_QgsField, sipName_convertCompatible, doc_QgsField_convertCompatible );
411
412 return nullptr;
413 }
414 }
415
416 % End
417#endif
418
420 operator QVariant() const
421 {
422 return QVariant::fromValue( *this );
423 }
424
430 void setEditorWidgetSetup( const QgsEditorWidgetSetup &v );
431
440 QgsEditorWidgetSetup editorWidgetSetup() const;
441
447 void setReadOnly( bool readOnly );
448
454 bool isReadOnly() const;
455
456#ifdef SIP_RUN
457 SIP_PYOBJECT __repr__();
458 % MethodCode
459 QString str = QStringLiteral( "<QgsField: %1 (%2)>" ).arg( sipCpp->name() ).arg( sipCpp->typeName() );
460 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
461 % End
462#endif
463
464 private:
465
466 QSharedDataPointer<QgsFieldPrivate> d;
467
468
469}; // class QgsField
470
472
474
476CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsField &field );
478CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsField &field );
479
480
481#endif
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:51
QString typeName() const
Gets the field type.
Definition qgsfield.cpp:152
QString name
Definition qgsfield.h:60
bool convertCompatible(QVariant &v, QString *errorMessage=nullptr) const
Converts the provided variant to a compatible format.
Definition qgsfield.cpp:424
QVariant::Type type
Definition qgsfield.h:58
virtual ~QgsField()
ConfigurationFlag
Configuration flags for fields These flags are meant to be user-configurable and are not describing a...
Definition qgsfield.h:80
#define str(x)
Definition qgis.cpp:37
#define SIP_SKIP
Definition qgis_sip.h:126
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
QList< int > QgsAttributeList
Definition qgsfield.h:26
const QgsField & field
Definition qgsfield.h:476
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsField::ConfigurationFlags) CORE_EXPORT QDataStream &operator<<(QDataStream &out
Writes the field to stream out. QGIS version compatibility is not guaranteed.
const QString & typeName
int precision