3

I would like to hide a field column while editing. The following code works perfectly in pyqgis2.

def fieldVisibility (layer,fname):
  f = layer.editFormConfig()
  for i, column in enumerate(layer.fields()):
    if column.name()==fname:
      f.setWidgetType(i, "Hidden")
        break
    else:
      continue

setWidgetType has been superseded and I don’t know the new expression in pyqgis3.

  • 1
    @ennine, have you seen this article How can I hide a column in qgis3 using pyqgis. By the way setWidgetType() now only take a string as the first parameter. Access by index has been removed. (source)[qgis.org/api/api_break.html] – Taras Aug 23 '18 at 12:25
  • 1
    @Many thanks Taras Dubrava for the link. I update the code as mentioned above and it works perfectly. – ennine Aug 23 '18 at 13:17
  • The new code should be posted as an answer and not edited into the question :) – Joseph Aug 23 '18 at 13:20
  • @ennine, you are welcome. Do not forget to put your valid code as an answer to your question. – Taras Aug 23 '18 at 13:30
3

Following the link mentioned by @Taras, the revise code is as follows:

def fieldVisibility (layer,fname):
  setup = QgsEditorWidgetSetup('Hidden', {})
  for i, column in enumerate(layer.fields()):
    if column.name()==fname:
      layer.setEditorWidgetSetup(idx, setup)
        break
    else:
      continue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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