Skip to content

Commit de41c74

Browse files
committed
Add isNumeric and QML bindings to QgsField
1 parent 1391aad commit de41c74

File tree

5 files changed

+54
-1
lines changed

5 files changed

+54
-1
lines changed

python/core/qgsfield.sip

+8
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ class QgsField
7373
*/
7474
QString comment() const;
7575

76+
/**
77+
* Returns if this field is numeric. Any integer or floating point type
78+
* will return true for this.
79+
*
80+
* @note added in QGIS 2.18
81+
*/
82+
bool isNumeric() const;
83+
7684
/**
7785
* Set the field name.
7886
* @param name Name of the field

src/core/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,7 @@ SET(QGIS_CORE_MOC_HDRS
460460
qgsdataprovider.h
461461
qgsdbfilterproxymodel.h
462462
qgseditformconfig.h
463+
qgsfield.h
463464
qgsgeometryvalidator.h
464465
qgsgml.h
465466
qgsgmlschema.h
@@ -648,7 +649,6 @@ SET(QGIS_CORE_HDRS
648649
qgsfeatureiterator.h
649650
qgsfeaturerequest.h
650651
qgsfeaturestore.h
651-
qgsfield.h
652652
qgsfield_p.h
653653
qgsfontutils.h
654654
qgsgeometrycache.h

src/core/qgsfield.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ QString QgsField::comment() const
111111
return d->comment;
112112
}
113113

114+
bool QgsField::isNumeric() const
115+
{
116+
return d->type == QVariant::Double || d->type == QVariant::Int || d->type == QVariant::UInt || d->type == QVariant::LongLong || d->type == QVariant::ULongLong;
117+
}
118+
114119
/***************************************************************************
115120
* This class is considered CRITICAL and any change MUST be accompanied with
116121
* full unit tests in testqgsfield.cpp.

src/core/qgsfield.h

+16
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ class QgsFieldsPrivate;
4343

4444
class CORE_EXPORT QgsField
4545
{
46+
Q_GADGET
47+
48+
Q_PROPERTY( bool isNumeric READ isNumeric )
49+
Q_PROPERTY( int length READ length )
50+
Q_PROPERTY( int precision READ precision )
51+
Q_PROPERTY( QString comment READ comment )
52+
Q_PROPERTY( QString name READ name )
53+
4654
public:
4755
/** Constructor. Constructs a new QgsField object.
4856
* @param name Field name
@@ -107,6 +115,14 @@ class CORE_EXPORT QgsField
107115
*/
108116
QString comment() const;
109117

118+
/**
119+
* Returns if this field is numeric. Any integer or floating point type
120+
* will return true for this.
121+
*
122+
* @note added in QGIS 2.18
123+
*/
124+
bool isNumeric() const;
125+
110126
/**
111127
* Set the field name.
112128
* @param name Name of the field

tests/src/core/testqgsfield.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class TestQgsField: public QObject
3434
void copy();// test cpy destruction (double delete)
3535
void assignment();
3636
void gettersSetters(); //test getters and setters
37+
void isNumeric(); //test isNumeric
3738
void equality(); //test equality operators
3839
void asVariant(); //test conversion to and from a QVariant
3940
void displayString();
@@ -117,6 +118,29 @@ void TestQgsField::gettersSetters()
117118
QCOMPARE( field.comment(), QString( "comment" ) );
118119
}
119120

121+
void TestQgsField::isNumeric()
122+
{
123+
QgsField field;
124+
field.setType( QVariant::Int );
125+
QVERIFY( field.isNumeric() );
126+
field.setType( QVariant::UInt );
127+
QVERIFY( field.isNumeric() );
128+
field.setType( QVariant::Double );
129+
QVERIFY( field.isNumeric() );
130+
field.setType( QVariant::LongLong );
131+
QVERIFY( field.isNumeric() );
132+
field.setType( QVariant::ULongLong );
133+
QVERIFY( field.isNumeric() );
134+
field.setType( QVariant::String );
135+
QVERIFY( !field.isNumeric() );
136+
field.setType( QVariant::DateTime );
137+
QVERIFY( !field.isNumeric() );
138+
field.setType( QVariant::Bool );
139+
QVERIFY( !field.isNumeric() );
140+
field.setType( QVariant::Invalid );
141+
QVERIFY( !field.isNumeric() );
142+
}
143+
120144
void TestQgsField::equality()
121145
{
122146
QgsField field1;

0 commit comments

Comments
 (0)