QGIS API Documentation 3.28.14-Firenze (exported)
Loading...
Searching...
No Matches
qgsfield.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsfield.cpp - 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 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
17#include "qgsfields.h"
18#include "qgsfield_p.h"
19#include "qgis.h"
20#include "qgsapplication.h"
21#include "qgssettings.h"
23#include "qgsvariantutils.h"
24
25#include <QDataStream>
26#include <QIcon>
27#include <QLocale>
28#include <QJsonDocument>
29
30/***************************************************************************
31 * This class is considered CRITICAL and any change MUST be accompanied with
32 * full unit tests in testqgsfield.cpp.
33 * See details in QEP #17
34 ****************************************************************************/
35
36#if 0
37QgsField::QgsField( QString nam, QString typ, int len, int prec, bool num,
38 QString comment )
39 : mName( nam ), mType( typ ), mLength( len ), mPrecision( prec ), mNumeric( num )
40 , mComment( comment )
41{
42 // This function used to lower case the field name since some stores
43 // use upper case (e.g., shapefiles), but that caused problems with
44 // attribute actions getting confused between uppercase and
45 // lowercase versions of the attribute names, so just leave the
46 // names how they are now.
47}
48#endif
49QgsField::QgsField( const QString &name, QVariant::Type type,
50 const QString &typeName, int len, int prec, const QString &comment, QVariant::Type subType )
51{
52 d = new QgsFieldPrivate( name, type, subType, typeName, len, prec, comment );
53}
54
55QgsField::QgsField( const QgsField &other ) //NOLINT
56 : d( other.d )
57{
58
59}
60
61QgsField::~QgsField() = default;
62
63/***************************************************************************
64 * This class is considered CRITICAL and any change MUST be accompanied with
65 * full unit tests in testqgsfield.cpp.
66 * See details in QEP #17
67 ****************************************************************************/
68
69QgsField &QgsField::operator =( const QgsField &other ) //NOLINT
70{
71 d = other.d;
72 return *this;
73}
74
75bool QgsField::operator==( const QgsField &other ) const
76{
77 return *( other.d ) == *d;
78}
79
80bool QgsField::operator!=( const QgsField &other ) const
81{
82 return !( *this == other );
83}
84
85QString QgsField::name() const
86{
87 return d->name;
88}
89
90QString QgsField::displayName() const
91{
92 if ( !d->alias.isEmpty() )
93 return d->alias;
94 else
95 return d->name;
96}
97
99{
100 if ( alias().isEmpty() )
101 {
102 return name();
103 }
104 return QStringLiteral( "%1 (%2)" ).arg( name() ).arg( alias() );
105}
106
107QString QgsField::displayType( const bool showConstraints ) const
108{
109 QString typeStr = typeName();
110
111 if ( length() > 0 && precision() > 0 )
112 typeStr += QStringLiteral( "(%1, %2)" ).arg( length() ).arg( precision() );
113 else if ( length() > 0 )
114 typeStr += QStringLiteral( "(%1)" ).arg( length() );
115
116 if ( showConstraints )
117 {
119 ? QStringLiteral( " NOT NULL" )
120 : QStringLiteral( " NULL" );
121
123 ? QStringLiteral( " UNIQUE" )
124 : QString();
125 }
126
127 return typeStr;
128}
129
131{
132 if ( d->type == QVariant::UserType )
133 {
134 if ( d->typeName.compare( QLatin1String( "geometry" ), Qt::CaseInsensitive ) == 0 )
135 {
136 return QObject::tr( "Geometry" );
137 }
138 }
139 return QgsVariantUtils::typeToDisplayString( d->type, d->subType );
140}
141
142QVariant::Type QgsField::type() const
143{
144 return d->type;
145}
146
147QVariant::Type QgsField::subType() const
148{
149 return d->subType;
150}
151
152QString QgsField::typeName() const
153{
154 return d->typeName;
155}
156
158{
159 return d->length;
160}
161
163{
164 return d->precision;
165}
166
167QString QgsField::comment() const
168{
169 return d->comment;
170}
171
173{
174 return d->type == QVariant::Double || d->type == QVariant::Int || d->type == QVariant::UInt || d->type == QVariant::LongLong || d->type == QVariant::ULongLong;
175}
176
178{
179 return d->type == QVariant::Date || d->type == QVariant::Time || d->type == QVariant::DateTime;
180}
181
182/***************************************************************************
183 * This class is considered CRITICAL and any change MUST be accompanied with
184 * full unit tests in testqgsfield.cpp.
185 * See details in QEP #17
186 ****************************************************************************/
187
188void QgsField::setName( const QString &name )
189{
190 d->name = name;
191}
192
193void QgsField::setType( QVariant::Type type )
194{
195 d->type = type;
196}
197
198void QgsField::setSubType( QVariant::Type subType )
199{
200 d->subType = subType;
201}
202
203void QgsField::setTypeName( const QString &typeName )
204{
205 d->typeName = typeName;
206}
207
208void QgsField::setLength( int len )
209{
210 d->length = len;
211}
213{
214 d->precision = precision;
215}
216
217void QgsField::setComment( const QString &comment )
218{
219 d->comment = comment;
220}
221
223{
224 return d->defaultValueDefinition;
225}
226
227void QgsField::setDefaultValueDefinition( const QgsDefaultValue &defaultValueDefinition )
228{
229 d->defaultValueDefinition = defaultValueDefinition;
230}
231
233{
234 d->constraints = constraints;
235}
236
238{
239 return d->constraints;
240}
241
242QString QgsField::alias() const
243{
244 return d->alias;
245}
246
247void QgsField::setAlias( const QString &alias )
248{
249 d->alias = alias;
250}
251
252QgsField::ConfigurationFlags QgsField::configurationFlags() const
253{
254 return d->flags;
255}
256
257void QgsField::setConfigurationFlags( QgsField::ConfigurationFlags flags )
258{
259 d->flags = flags;
260}
261
262/***************************************************************************
263 * This class is considered CRITICAL and any change MUST be accompanied with
264 * full unit tests in testqgsfield.cpp.
265 * See details in QEP #17
266 ****************************************************************************/
267
268QString QgsField::displayString( const QVariant &v ) const
269{
270 if ( QgsVariantUtils::isNull( v ) )
271 {
273 }
274
275 if ( v.userType() == QMetaType::type( "QgsReferencedGeometry" ) )
276 {
277 QgsReferencedGeometry geom = qvariant_cast<QgsReferencedGeometry>( v );
278 if ( geom.isNull() )
280 else
281 {
282 QString wkt = geom.asWkt();
283 if ( wkt.length() >= 1050 )
284 {
285 wkt = wkt.left( 999 ) + QChar( 0x2026 );
286 }
287 QString formattedText = QStringLiteral( "%1 [%2]" ).arg( wkt, geom.crs().userFriendlyIdentifier() );
288 return formattedText;
289 }
290 }
291
292 // Special treatment for numeric types if group separator is set or decimalPoint is not a dot
293 if ( d->type == QVariant::Double )
294 {
295 // if value doesn't contain a double (a default value expression for instance),
296 // apply no transformation
297 bool ok;
298 v.toDouble( &ok );
299 if ( !ok )
300 return v.toString();
301
302 // Locales with decimal point != '.' or that require group separator: use QLocale
303 if ( QLocale().decimalPoint() != '.' ||
304 !( QLocale().numberOptions() & QLocale::NumberOption::OmitGroupSeparator ) )
305 {
306 if ( d->precision > 0 )
307 {
308 if ( -1 < v.toDouble() && v.toDouble() < 1 )
309 {
310 return QLocale().toString( v.toDouble(), 'g', d->precision );
311 }
312 else
313 {
314 return QLocale().toString( v.toDouble(), 'f', d->precision );
315 }
316 }
317 else
318 {
319 // Precision is not set, let's guess it from the
320 // standard conversion to string
321 const QString s( v.toString() );
322 const int dotPosition( s.indexOf( '.' ) );
323 int precision;
324 if ( dotPosition < 0 && s.indexOf( 'e' ) < 0 )
325 {
326 precision = 0;
327 return QLocale().toString( v.toDouble(), 'f', precision );
328 }
329 else
330 {
331 if ( dotPosition < 0 ) precision = 0;
332 else precision = s.length() - dotPosition - 1;
333
334 if ( -1 < v.toDouble() && v.toDouble() < 1 )
335 {
336 return QLocale().toString( v.toDouble(), 'g', precision );
337 }
338 else
339 {
340 return QLocale().toString( v.toDouble(), 'f', precision );
341 }
342 }
343 }
344 }
345 // Default for doubles with precision
346 else if ( d->precision > 0 )
347 {
348 if ( -1 < v.toDouble() && v.toDouble() < 1 )
349 {
350 return QString::number( v.toDouble(), 'g', d->precision );
351 }
352 else
353 {
354 return QString::number( v.toDouble(), 'f', d->precision );
355 }
356 }
357 else
358 {
359 const double vDouble = v.toDouble();
360 // mimic Qt 5 handling of when to switch to exponential forms
361 if ( std::fabs( vDouble ) < 1e-04 )
362 return QString::number( vDouble, 'g', QLocale::FloatingPointShortest );
363 else
364 return QString::number( vDouble, 'f', QLocale::FloatingPointShortest );
365 }
366 }
367 // Other numeric types than doubles
368 else if ( isNumeric() &&
369 !( QLocale().numberOptions() & QLocale::NumberOption::OmitGroupSeparator ) )
370 {
371 bool ok;
372 const qlonglong converted( v.toLongLong( &ok ) );
373 if ( ok )
374 return QLocale().toString( converted );
375 }
376 else if ( d->typeName.compare( QLatin1String( "json" ), Qt::CaseInsensitive ) == 0 || d->typeName == QLatin1String( "jsonb" ) )
377 {
378 const QJsonDocument doc = QJsonDocument::fromVariant( v );
379 return QString::fromUtf8( doc.toJson().data() );
380 }
381 else if ( d->type == QVariant::ByteArray )
382 {
383 return QObject::tr( "BLOB" );
384 }
385 else if ( d->type == QVariant::StringList || d->type == QVariant::List )
386 {
387 QString result;
388 const QVariantList list = v.toList();
389 for ( const QVariant &var : list )
390 {
391 if ( !result.isEmpty() )
392 result.append( QStringLiteral( ", " ) );
393 result.append( var.toString() );
394 }
395 return result;
396 }
397
398 // Fallback if special rules do not apply
399 return v.toString();
400}
401
403{
404 switch ( flag )
405 {
407 return QObject::tr( "None" );
409 return QObject::tr( "Not searchable" );
411 return QObject::tr( "Do not expose via WMS" );
413 return QObject::tr( "Do not expose via WFS" );
414 }
415 return QString();
416}
417
418/***************************************************************************
419 * This class is considered CRITICAL and any change MUST be accompanied with
420 * full unit tests in testqgsfield.cpp.
421 * See details in QEP #17
422 ****************************************************************************/
423
424bool QgsField::convertCompatible( QVariant &v, QString *errorMessage ) const
425{
426 const QVariant original = v;
427 if ( errorMessage )
428 errorMessage->clear();
429
430 if ( QgsVariantUtils::isNull( v ) )
431 {
432 v.convert( d->type );
433 return true;
434 }
435
436 if ( d->type == QVariant::Int && v.toInt() != v.toLongLong() )
437 {
438 v = QVariant( d->type );
439 if ( errorMessage )
440 *errorMessage = QObject::tr( "Value \"%1\" is too large for integer field" ).arg( original.toLongLong() );
441 return false;
442 }
443
444 // Give it a chance to convert to double since for not '.' locales
445 // we accept both comma and dot as decimal point
446 if ( d->type == QVariant::Double && v.type() == QVariant::String )
447 {
448 QVariant tmp( v );
449 if ( !tmp.convert( d->type ) )
450 {
451 // This might be a string with thousand separator: use locale to convert
452 bool ok = false;
453 double d = qgsPermissiveToDouble( v.toString(), ok );
454 if ( ok )
455 {
456 v = QVariant( d );
457 return true;
458 }
459 // For not 'dot' locales, we also want to accept '.'
460 if ( QLocale().decimalPoint() != '.' )
461 {
462 d = QLocale( QLocale::C ).toDouble( v.toString(), &ok );
463 if ( ok )
464 {
465 v = QVariant( d );
466 return true;
467 }
468 }
469 }
470 }
471
472 // For string representation of an int we also might have thousand separator
473 if ( d->type == QVariant::Int && v.type() == QVariant::String )
474 {
475 QVariant tmp( v );
476 if ( !tmp.convert( d->type ) )
477 {
478 // This might be a string with thousand separator: use locale to convert
479 bool ok;
480 const int i = qgsPermissiveToInt( v.toString(), ok );
481 if ( ok )
482 {
483 v = QVariant( i );
484 return true;
485 }
486 }
487 }
488
489 // For string representation of a long we also might have thousand separator
490 if ( d->type == QVariant::LongLong && v.type() == QVariant::String )
491 {
492 QVariant tmp( v );
493 if ( !tmp.convert( d->type ) )
494 {
495 // This might be a string with thousand separator: use locale to convert
496 bool ok;
497 const qlonglong l = qgsPermissiveToLongLong( v.toString(), ok );
498 if ( ok )
499 {
500 v = QVariant( l );
501 return true;
502 }
503 }
504 }
505
506 //String representations of doubles in QVariant will return false to convert( QVariant::Int )
507 //work around this by first converting to double, and then checking whether the double is convertible to int
508 if ( d->type == QVariant::Int && v.canConvert( QVariant::Double ) )
509 {
510 bool ok = false;
511 const double dbl = v.toDouble( &ok );
512 if ( !ok )
513 {
514 //couldn't convert to number
515 v = QVariant( d->type );
516
517 if ( errorMessage )
518 *errorMessage = QObject::tr( "Value \"%1\" is not a number" ).arg( original.toString() );
519
520 return false;
521 }
522
523 const double round = std::round( dbl );
524 if ( round > std::numeric_limits<int>::max() || round < -std::numeric_limits<int>::max() )
525 {
526 //double too large to fit in int
527 v = QVariant( d->type );
528
529 if ( errorMessage )
530 *errorMessage = QObject::tr( "Value \"%1\" is too large for integer field" ).arg( original.toDouble() );
531
532 return false;
533 }
534 v = QVariant( static_cast< int >( std::round( dbl ) ) );
535 return true;
536 }
537
538 //String representations of doubles in QVariant will return false to convert( QVariant::LongLong )
539 //work around this by first converting to double, and then checking whether the double is convertible to longlong
540 if ( d->type == QVariant::LongLong && v.canConvert( QVariant::Double ) )
541 {
542 //firstly test the conversion to longlong because conversion to double will rounded the value
543 QVariant tmp( v );
544 if ( !tmp.convert( d->type ) )
545 {
546 bool ok = false;
547 const double dbl = v.toDouble( &ok );
548 if ( !ok )
549 {
550 //couldn't convert to number
551 v = QVariant( d->type );
552
553 if ( errorMessage )
554 *errorMessage = QObject::tr( "Value \"%1\" is not a number" ).arg( original.toString() );
555
556 return false;
557 }
558
559 const double round = std::round( dbl );
560 if ( round > static_cast<double>( std::numeric_limits<long long>::max() ) || round < static_cast<double>( -std::numeric_limits<long long>::max() ) )
561 {
562 //double too large to fit in longlong
563 v = QVariant( d->type );
564
565 if ( errorMessage )
566 *errorMessage = QObject::tr( "Value \"%1\" is too large for long long field" ).arg( original.toDouble() );
567
568 return false;
569 }
570 v = QVariant( static_cast< long long >( std::round( dbl ) ) );
571 return true;
572 }
573 }
574
575 if ( d->typeName.compare( QLatin1String( "json" ), Qt::CaseInsensitive ) == 0 || d->typeName.compare( QLatin1String( "jsonb" ), Qt::CaseInsensitive ) == 0 )
576 {
577 if ( d->type == QVariant::String )
578 {
579 const QJsonDocument doc = QJsonDocument::fromVariant( v );
580 if ( !doc.isNull() )
581 {
582 v = QString::fromUtf8( doc.toJson( QJsonDocument::Compact ).constData() );
583 return true;
584 }
585 v = QVariant( d->type );
586 return false;
587 }
588 else if ( d->type == QVariant::Map )
589 {
590 if ( v.type() == QVariant::StringList || v.type() == QVariant::List || v.type() == QVariant::Map )
591 {
592 return true;
593 }
594 v = QVariant( d->type );
595 return false;
596 }
597 }
598
599 if ( ( d->type == QVariant::StringList || ( d->type == QVariant::List && d->subType == QVariant::String ) )
600 && ( v.type() == QVariant::String ) )
601 {
602 v = QStringList( { v.toString() } );
603 return true;
604 }
605
606 if ( ( d->type == QVariant::StringList || d->type == QVariant::List ) && !( v.type() == QVariant::StringList || v.type() == QVariant::List ) )
607 {
608 v = QVariant( d->type );
609
610 if ( errorMessage )
611 *errorMessage = QObject::tr( "Could not convert value \"%1\" to target list type" ).arg( original.toString() );
612
613 return false;
614 }
615
616 if ( d->type == QVariant::UserType && d->typeName.compare( QLatin1String( "geometry" ), Qt::CaseInsensitive ) == 0 )
617 {
618 if ( v.userType() == QMetaType::type( "QgsReferencedGeometry" ) || v.userType() == QMetaType::type( "QgsGeometry" ) )
619 {
620 return true;
621 }
622 else if ( v.type() == QVariant::String )
623 {
624 const QgsGeometry geom = QgsGeometry::fromWkt( v.toString() );
625 if ( !geom.isNull() )
626 {
627 v = QVariant::fromValue( geom );
628 return true;
629 }
630 }
631 return false;
632 }
633 else if ( !v.convert( d->type ) )
634 {
635 v = QVariant( d->type );
636
637 if ( errorMessage )
638 *errorMessage = QObject::tr( "Could not convert value \"%1\" to target type \"%2\"" )
639 .arg( original.toString() )
640 .arg( d->typeName );
641
642 return false;
643 }
644
645 if ( d->type == QVariant::Double && d->precision > 0 )
646 {
647 const double s = std::pow( 10, d->precision );
648 const double d = v.toDouble() * s;
649 v = QVariant( ( d < 0 ? std::ceil( d - 0.5 ) : std::floor( d + 0.5 ) ) / s );
650 return true;
651 }
652
653 if ( d->type == QVariant::String && d->length > 0 && v.toString().length() > d->length )
654 {
655 const int length = v.toString().length();
656 v = v.toString().left( d->length );
657
658 if ( errorMessage )
659 *errorMessage = QObject::tr( "String of length %1 exceeds maximum field length (%2)" ).arg( length ).arg( d->length );
660
661 return false;
662 }
663
664 return true;
665}
666
668{
669 d->editorWidgetSetup = v;
670}
671
673{
674 return d->editorWidgetSetup;
675}
676
677void QgsField::setReadOnly( bool readOnly )
678{
679 d->isReadOnly = readOnly;
680}
681
683{
684 return d->isReadOnly;
685}
686
687
688/***************************************************************************
689 * This class is considered CRITICAL and any change MUST be accompanied with
690 * full unit tests in testqgsfield.cpp.
691 * See details in QEP #17
692 ****************************************************************************/
693
694QDataStream &operator<<( QDataStream &out, const QgsField &field )
695{
696 out << field.name();
697 out << static_cast< quint32 >( field.type() );
698 out << field.typeName();
699 out << field.length();
700 out << field.precision();
701 out << field.comment();
702 out << field.alias();
705 out << field.constraints().constraints();
706 out << static_cast< quint32 >( field.constraints().constraintOrigin( QgsFieldConstraints::ConstraintNotNull ) );
707 out << static_cast< quint32 >( field.constraints().constraintOrigin( QgsFieldConstraints::ConstraintUnique ) );
714 out << static_cast< quint32 >( field.subType() );
715 return out;
716}
717
718QDataStream &operator>>( QDataStream &in, QgsField &field )
719{
720 quint32 type;
721 quint32 subType;
722 quint32 length;
723 quint32 precision;
724 quint32 constraints;
725 quint32 originNotNull;
726 quint32 originUnique;
727 quint32 originExpression;
728 quint32 strengthNotNull;
729 quint32 strengthUnique;
730 quint32 strengthExpression;
731
732 bool applyOnUpdate;
733
734 QString name;
735 QString typeName;
736 QString comment;
737 QString alias;
738 QString defaultValueExpression;
739 QString constraintExpression;
740 QString constraintDescription;
741
742 in >> name >> type >> typeName >> length >> precision >> comment >> alias
743 >> defaultValueExpression >> applyOnUpdate >> constraints >> originNotNull >> originUnique >> originExpression >> strengthNotNull >> strengthUnique >> strengthExpression >>
744 constraintExpression >> constraintDescription >> subType;
745 field.setName( name );
746 field.setType( static_cast< QVariant::Type >( type ) );
748 field.setLength( static_cast< int >( length ) );
749 field.setPrecision( static_cast< int >( precision ) );
750 field.setComment( comment );
751 field.setAlias( alias );
752 field.setDefaultValueDefinition( QgsDefaultValue( defaultValueExpression, applyOnUpdate ) );
753 QgsFieldConstraints fieldConstraints;
754 if ( constraints & QgsFieldConstraints::ConstraintNotNull )
755 {
756 fieldConstraints.setConstraint( QgsFieldConstraints::ConstraintNotNull, static_cast< QgsFieldConstraints::ConstraintOrigin>( originNotNull ) );
758 }
759 else
761 if ( constraints & QgsFieldConstraints::ConstraintUnique )
762 {
763 fieldConstraints.setConstraint( QgsFieldConstraints::ConstraintUnique, static_cast< QgsFieldConstraints::ConstraintOrigin>( originUnique ) );
765 }
766 else
769 {
770 fieldConstraints.setConstraint( QgsFieldConstraints::ConstraintExpression, static_cast< QgsFieldConstraints::ConstraintOrigin>( originExpression ) );
772 }
773 else
775 fieldConstraints.setConstraintExpression( constraintExpression, constraintDescription );
776 field.setConstraints( fieldConstraints );
777 field.setSubType( static_cast< QVariant::Type >( subType ) );
778 return in;
779}
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS.
QString userFriendlyIdentifier(IdentifierType type=MediumString) const
Returns a user friendly identifier for the CRS.
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.
ConstraintStrength
Strength of constraints.
void setConstraintStrength(Constraint constraint, ConstraintStrength strength)
Sets the strength of a constraint.
void setConstraintExpression(const QString &expression, const QString &description=QString())
Set the constraint expression for the field.
ConstraintOrigin
Origin of constraints.
ConstraintStrength constraintStrength(Constraint constraint) const
Returns the strength of a field constraint, or ConstraintStrengthNotSet if the constraint is not pres...
ConstraintOrigin constraintOrigin(Constraint constraint) const
Returns the origin of a field constraint, or ConstraintOriginNotSet if the constraint is not present ...
QString constraintExpression() const
Returns the constraint expression for the field, if set.
@ ConstraintNotNull
Field may not be null.
@ ConstraintUnique
Field must have a unique value.
@ ConstraintExpression
Field has an expression constraint set. See constraintExpression().
void removeConstraint(Constraint constraint)
Removes a constraint from the field.
QString constraintDescription() const
Returns the descriptive name for the constraint expression.
void setConstraint(Constraint constraint, ConstraintOrigin origin=ConstraintOriginLayer)
Sets a constraint on the field.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:51
bool isDateOrTime
Definition qgsfield.h:55
void setEditorWidgetSetup(const QgsEditorWidgetSetup &v)
Set the editor widget setup for the field.
Definition qgsfield.cpp:667
QString typeName() const
Gets the field type.
Definition qgsfield.cpp:152
void setConstraints(const QgsFieldConstraints &constraints)
Sets constraints which are present for the field.
Definition qgsfield.cpp:232
void setAlias(const QString &alias)
Sets the alias for the field (the friendly displayed name of the field ).
Definition qgsfield.cpp:247
QString name
Definition qgsfield.h:60
bool operator!=(const QgsField &other) const
Definition qgsfield.cpp:80
bool operator==(const QgsField &other) const
Definition qgsfield.cpp:75
int precision
Definition qgsfield.h:57
static QString readableConfigurationFlag(QgsField::ConfigurationFlag flag)
Returns the readable and translated value of the configuration flag.
Definition qgsfield.cpp:402
int length
Definition qgsfield.h:56
QgsField & operator=(const QgsField &other)
Assignment operator.
Definition qgsfield.cpp:69
QString displayString(const QVariant &v) const
Formats string for display.
Definition qgsfield.cpp:268
void setPrecision(int precision)
Set the field precision.
Definition qgsfield.cpp:212
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:98
bool convertCompatible(QVariant &v, QString *errorMessage=nullptr) const
Converts the provided variant to a compatible format.
Definition qgsfield.cpp:424
void setName(const QString &name)
Set the field name.
Definition qgsfield.cpp:188
void setComment(const QString &comment)
Set the field comment.
Definition qgsfield.cpp:217
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:107
void setLength(int len)
Set the field length.
Definition qgsfield.cpp:208
void setConfigurationFlags(QgsField::ConfigurationFlags configurationFlags)
Sets the Flags for the field (searchable, …)
Definition qgsfield.cpp:257
QString displayName() const
Returns the name to use when displaying this field.
Definition qgsfield.cpp:90
void setDefaultValueDefinition(const QgsDefaultValue &defaultValueDefinition)
Sets an expression to use when calculating the default value for the field.
Definition qgsfield.cpp:227
ConfigurationFlags configurationFlags
Definition qgsfield.h:64
QString friendlyTypeString() const
Returns a user friendly, translated representation of the field type.
Definition qgsfield.cpp:130
void setReadOnly(bool readOnly)
Make field read-only if readOnly is set to true.
Definition qgsfield.cpp:677
QVariant::Type type
Definition qgsfield.h:58
QVariant::Type subType() const
If the field is a collection, gets its element's type.
Definition qgsfield.cpp:147
QString alias
Definition qgsfield.h:61
QgsField(const QString &name=QString(), QVariant::Type type=QVariant::Invalid, const QString &typeName=QString(), int len=0, int prec=0, const QString &comment=QString(), QVariant::Type subType=QVariant::Invalid)
Constructor.
Definition qgsfield.cpp:49
QgsDefaultValue defaultValueDefinition
Definition qgsfield.h:62
bool isNumeric
Definition qgsfield.h:54
void setSubType(QVariant::Type subType)
If the field is a collection, set its element's type.
Definition qgsfield.cpp:198
void setType(QVariant::Type type)
Set variant type.
Definition qgsfield.cpp:193
QString comment
Definition qgsfield.h:59
virtual ~QgsField()
QgsFieldConstraints constraints
Definition qgsfield.h:63
ConfigurationFlag
Configuration flags for fields These flags are meant to be user-configurable and are not describing a...
Definition qgsfield.h:80
@ HideFromWfs
Field is not available if layer is served as WFS from QGIS server.
@ NotSearchable
Defines if the field is searchable (used in the locator search for instance)
@ None
No flag is defined.
@ HideFromWms
Field is not available if layer is served as WMS from QGIS server.
QgsEditorWidgetSetup editorWidgetSetup() const
Gets the editor widget setup for the field.
Definition qgsfield.cpp:672
bool isReadOnly
Definition qgsfield.h:65
void setTypeName(const QString &typeName)
Set the field type.
Definition qgsfield.cpp:203
A geometry is the spatial representation of a feature.
static QgsGeometry fromWkt(const QString &wkt)
Creates a new geometry from a WKT string.
QString asWkt(int precision=17) const
Exports the geometry to WKT.
QgsCoordinateReferenceSystem crs() const
Returns the associated coordinate reference system, or an invalid CRS if no reference system is set.
A QgsGeometry with associated coordinate reference system.
static QString typeToDisplayString(QVariant::Type type, QVariant::Type subType=QVariant::Type::Invalid)
Returns a user-friendly translated string representing a QVariant type.
static bool isNull(const QVariant &variant)
Returns true if the specified variant should be considered a NULL value.
qlonglong qgsPermissiveToLongLong(QString string, bool &ok)
Converts a string to an qlonglong in a permissive way, e.g., allowing for incorrect numbers of digits...
Definition qgis.cpp:85
double qgsPermissiveToDouble(QString string, bool &ok)
Converts a string to a double in a permissive way, e.g., allowing for incorrect numbers of digits bet...
Definition qgis.cpp:71
int qgsPermissiveToInt(QString string, bool &ok)
Converts a string to an integer in a permissive way, e.g., allowing for incorrect numbers of digits b...
Definition qgis.cpp:78
QDataStream & operator>>(QDataStream &in, QgsField &field)
Reads a field from stream in into field. QGIS version compatibility is not guaranteed.
Definition qgsfield.cpp:718
QDataStream & operator<<(QDataStream &out, const QgsField &field)
Definition qgsfield.cpp:694
const QgsField & field
Definition qgsfield.h:476
const QString & typeName
int precision