I am creating a project with QGIS 2.18 to be exported to QField. The Survey form is as follow (3 columns):

  • Customer name - checked on site? - Comment
  • Water meter number - checked on site? - Comment
  • Type of water meter - checked on site? - Comment
  • Tariff code - checked on site? - Comment
  • Diameter - checked on site? - Comment
  • etc. etc.

As one can sees, there will be several fields with the same properties ("checked on site" as form value relation, "comment" as a text).

Is it possible to duplicate some fields structure keeping the properties (widget, length, type, etc.) ?

1 Answer 1


I am afraid it's not possible. But you are lucky :D ! Today it's the first birthday of my son.

I made a script that do what you want. Hope this will help you or anyone else.

Have a look to this :

enter image description here

The script is under pull request state, but you can download it on my fork

or get it from this code :

from qgis.core import *
from qgis.gui import *
from qgis.utils import *
from PyQt4.QtCore import QVariant

##Duplicate Fied=name
##field_to_duplicate=field input_layer

layer = processing.getObject(input_layer)

pr = layer.dataProvider()

caps = pr.capabilities()

def refreshLayer():
    layer.setCacheImage( None )

fields = layer.pendingFields()

QVariantDict = {
    4 : QVariant.Int,
    1 : QVariant.Double,
    10 : QVariant.String,
    14 : QVariant.Date,

listFieldName = str(fieldname_list).split(';')

newFieldIndex = len(fields)

for field in fields:
    if field.name() == field_to_duplicate:
        FieldIdx = fields.indexFromName(field.name()) 
        field_type = QVariantDict[field.type()]
        field_precision = field.precision()
        field_length = field.length()
        field_widget = layer.editorWidgetV2(FieldIdx)
        field_widgetConfig = layer.editorWidgetV2Config(FieldIdx)
        progress.setText("{},{},{},{}".format(str(field.name()),str(FieldIdx), str(field_widget), str(field_widgetConfig)))

for f in listFieldName:
    if f in fields:
    else :
        if caps & QgsVectorDataProvider.AddAttributes:
            modelProperties= QgsField(f, field_type, str(field_length), int(field_precision), QVariant.String )             
            res = pr.addAttributes([modelProperties])
            layer.setEditorWidgetV2(newFieldIndex , field_widget)
            layer.setEditorWidgetV2Config(newFieldIndex , field_widgetConfig)
            newField_widget = layer.editorWidgetV2(newFieldIndex)
            newField_widgetConfig = layer.editorWidgetV2Config(newFieldIndex)
            progress.setText("{},{},{},{}".format(str(f),str(newFieldIndex), str(newField_widget), str(newField_widgetConfig)))
            newFieldIndex += 1


any improvement will be appreciate.

  • 1
    Happy birthday to your son :)
    – Joseph
    Commented Oct 30, 2017 at 15:37
  • Thanks Hugo and happy birthday to your son ! This script could be a great improvement for newer versions of QGIS Commented Oct 31, 2017 at 2:11
  • you are welcome. the script should be open through the processing interface (get script from online source) when Qgis team will merge the fork. The script is not compatible to the QGIS3 for now. As I now the Widget class is refactored so, we will need to adapt this script as soon as possible. Please, don't forget to mark the answer as resolved Commented Oct 31, 2017 at 7:59

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.