File tree Expand file tree Collapse file tree 5 files changed +43
-1
lines changed Expand file tree Collapse file tree 5 files changed +43
-1
lines changed Original file line number Diff line number Diff line change @@ -115,6 +115,13 @@ Return number of items
115
115
:rtype: int
116
116
%End
117
117
118
+ QStringList names() const;
119
+ %Docstring
120
+ Returns a list with field names
121
+ .. versionadded:: 3.0
122
+ :rtype: list of str
123
+ %End
124
+
118
125
bool exists( int i ) const;
119
126
%Docstring
120
127
Return if a field index is valid
Original file line number Diff line number Diff line change @@ -122,6 +122,16 @@ int QgsFields::size() const
122
122
return d->fields .count ();
123
123
}
124
124
125
+ QStringList QgsFields::names () const
126
+ {
127
+ QStringList lst;
128
+ for ( int i = 0 ; i < d->fields .count (); ++i )
129
+ {
130
+ lst.append ( d->fields [i].field .name () );
131
+ }
132
+ return lst;
133
+ }
134
+
125
135
bool QgsFields::exists ( int i ) const
126
136
{
127
137
return i >= 0 && i < d->fields .count ();
Original file line number Diff line number Diff line change @@ -138,6 +138,12 @@ class CORE_EXPORT QgsFields
138
138
// ! Return number of items
139
139
int size () const ;
140
140
141
+ /* *
142
+ * Returns a list with field names
143
+ * \since QGIS 3.0
144
+ */
145
+ QStringList names () const ;
146
+
141
147
/* *
142
148
* Return if a field index is valid
143
149
* \param i Index of the field which needs to be checked
Original file line number Diff line number Diff line change @@ -60,7 +60,7 @@ ADD_PYTHON_TEST(PyQgsFillSymbolLayers test_qgsfillsymbollayers.py)
60
60
ADD_PYTHON_TEST (PyQgsProject test_qgsproject.py )
61
61
ADD_PYTHON_TEST (PyQgsFeatureIterator test_qgsfeatureiterator.py )
62
62
ADD_PYTHON_TEST (PyQgsFeedback test_qgsfeedback.py )
63
- ADD_PYTHON_TEST (PyQgsField test_qgsfield .py )
63
+ ADD_PYTHON_TEST (PyQgsFields test_qgsfields .py )
64
64
ADD_PYTHON_TEST (PyQgsFieldModel test_qgsfieldmodel.py )
65
65
ADD_PYTHON_TEST (PyQgsFilterLineEdit test_qgsfilterlineedit.py )
66
66
ADD_PYTHON_TEST (PyQgsFloatingWidget test_qgsfloatingwidget.py )
Original file line number Diff line number Diff line change @@ -91,6 +91,25 @@ def test_exceptions(self):
91
91
with self .assertRaises (KeyError ):
92
92
fields .iconForField (111 )
93
93
94
+ def test_names (self ):
95
+ ml = QgsVectorLayer (
96
+ "Point?crs=epsg:4236" +
97
+ "&field=id:integer" +
98
+ "&field=value:double" +
99
+ "&field=crazy:double" ,
100
+ "test_data" ,
101
+ "memory" )
102
+
103
+ assert ml .isValid ()
104
+ fields = ml .fields ()
105
+
106
+ expected_fields = ['id' , 'value' , 'crazy' ]
107
+
108
+ self .assertEquals (fields .names (), expected_fields )
109
+ fields .remove (1 )
110
+ expected_fields = ['id' , 'crazy' ]
111
+ self .assertEquals (fields .names (), expected_fields )
112
+
94
113
95
114
if __name__ == '__main__' :
96
115
unittest .main ()
You can’t perform that action at this time.
0 commit comments