9

I've been developing a Plugin on the QGIS platform using PyQGIS. So far plugin does the following: You can select any feature of the already loaded vector layer in QGIS. After selection, the plugin displays attribute names, and associated attribute values, explicitly for the selected feature. You can change those values, and save them.

Now I would like to add the possibility to define the data type for every attribute that the loaded layer contains. For example, I would like that first attribute named, let's say, "road_id" can only be a 4-digit long integer, so if you enter a string value or even a 5-digit integer, it gets rejected.

Is it possible to do that? If it is, which are the classes/methods I should be looking into?

Taras
35.8k5 gold badges77 silver badges151 bronze badges
asked Jun 13, 2012 at 11:00
1
  • So do you just need to get the fields for the layer and the width Commented Jun 13, 2012 at 12:00

3 Answers 3

13

For PyQGIS 3 :

The function pendingFields() has been replaced by fields(), see the QGIS API Documentation | Backwards Incompatible Changes for more details.

for field in layer.fields():
 print( "Name:%s, TypeName:%s, Type:%s, Length:%s" % ( field.name(), field.typeName(), field.type(), field.length()))

References:

Taras
35.8k5 gold badges77 silver badges151 bronze badges
answered Jun 12, 2019 at 3:12
0
10

For PyQGIS 2 :

If you need to get the fields of a layer you can use QgsVectorLayer::pendingFields() in Python like so:

fields = layer.pendingFields()

which will give you something like this:

{0: <qgis.core.QgsField object at 0x46d9d40>, 
1: <qgis.core.QgsField object at 0x46d9b00>, 
2: <qgis.core.QgsField object at 0x46d9cb0>, 
3: <qgis.core.QgsField object at 0x46d9ef0>, 
4: <qgis.core.QgsField object at 0x46d9dd0>}

Which you can then go though get the name, data type, and length:

for field in fields.itervalues():
 print "Name:%s Type:%s, Length:%s" % ( field.name(), field.typeName(), field.length())
Name:Seg_Par Type:String, Length:10
Name:Lot_num Type:String, Length:5
Name:Plan_num Type:String, Length:10
Name:Tenure Type:String, Length:2
Name:Area_ha Type:Real, Length:14
Taras
35.8k5 gold badges77 silver badges151 bronze badges
answered Jun 13, 2012 at 12:26
0
1

It's not necessary to write your own plugin for this purpose, you can use your own customised edit user interface. See also these blog posts:

https://woostuff.wordpress.com/2011/09/05/qgis-tips-custom-feature-forms-with-python-logic/

and

http://linfiniti.com/2009/11/creating-dynamic-forms-a-new-feature-headed-for-qgis-1-4/

answered Jun 13, 2012 at 11:49
1
  • 1
    Oh, I'm afraid I do have to write plugin because this is the project I'm doing for the college :) Commented Jun 13, 2012 at 11:54

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.