Skip to content

Commit

Permalink
Added QgsFields::names method to get a list of all the names of fields
Browse files Browse the repository at this point in the history
  • Loading branch information
mbernasocchi authored and m-kuhn committed Oct 6, 2017
1 parent 5dce8ed commit 1c63522
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 1 deletion.
7 changes: 7 additions & 0 deletions python/core/qgsfields.sip
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ Return number of items
:rtype: int
%End

QStringList names() const;
%Docstring
Returns a list with field names
.. versionadded:: 3.0
:rtype: list of str
%End

bool exists( int i ) const;
%Docstring
Return if a field index is valid
Expand Down
10 changes: 10 additions & 0 deletions src/core/qgsfields.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ int QgsFields::size() const
return d->fields.count();
}

QStringList QgsFields::names() const
{
QStringList lst;
for ( int i = 0; i < d->fields.count(); ++i )
{
lst.append( d->fields[i].field.name() );
}
return lst;
}

bool QgsFields::exists( int i ) const
{
return i >= 0 && i < d->fields.count();
Expand Down
6 changes: 6 additions & 0 deletions src/core/qgsfields.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ class CORE_EXPORT QgsFields
//! Return number of items
int size() const;

/**
* Returns a list with field names
* \since QGIS 3.0
*/
QStringList names() const;

/**
* Return if a field index is valid
* \param i Index of the field which needs to be checked
Expand Down
2 changes: 1 addition & 1 deletion tests/src/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ ADD_PYTHON_TEST(PyQgsFillSymbolLayers test_qgsfillsymbollayers.py)
ADD_PYTHON_TEST(PyQgsProject test_qgsproject.py)
ADD_PYTHON_TEST(PyQgsFeatureIterator test_qgsfeatureiterator.py)
ADD_PYTHON_TEST(PyQgsFeedback test_qgsfeedback.py)
ADD_PYTHON_TEST(PyQgsField test_qgsfield.py)
ADD_PYTHON_TEST(PyQgsFields test_qgsfields.py)
ADD_PYTHON_TEST(PyQgsFieldModel test_qgsfieldmodel.py)
ADD_PYTHON_TEST(PyQgsFilterLineEdit test_qgsfilterlineedit.py)
ADD_PYTHON_TEST(PyQgsFloatingWidget test_qgsfloatingwidget.py)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,25 @@ def test_exceptions(self):
with self.assertRaises(KeyError):
fields.iconForField(111)

def test_names(self):
ml = QgsVectorLayer(
"Point?crs=epsg:4236" +
"&field=id:integer" +
"&field=value:double" +
"&field=crazy:double",
"test_data",
"memory")

assert ml.isValid()
fields = ml.fields()

expected_fields = ['id', 'value', 'crazy']

self.assertEquals(fields.names(), expected_fields)
fields.remove(1)
expected_fields = ['id', 'crazy']
self.assertEquals(fields.names(), expected_fields)


if __name__ == '__main__':
unittest.main()

0 comments on commit 1c63522

Please sign in to comment.