1

I am using PostGIS table in QGIS with custom QGISFieldWidgetFactory. However, I wanted to rename the field names in QGIS Edit Form from say first_name to First Name using Python. Currently, I was able to format all labels/field names using the following method.

class WidgetWrapper(QgsEditorWidgetWrapper):
     ....
    def format_form(self):
        title = format_name(
            self.entity().short_name
        )

        title = QApplication.translate(
            'WidgetWrapper',
            '{} Editor'.format(title)
        )

        # Set title and format labels for QGIS form
        if self.parent.parent() is not None:
            self.parent.parent().setWindowTitle(title)
            label = self.parent.parent().findChildren(QLabel)[-1:]
            if len(label) == 1:
                text = label[0].text()
                formatted_text = format_name(text)
                label[0].setText(formatted_text)

      ....
   def initWidget(self, widget):
       ....
       self.format_form()

This is not a clean way and it doesn't work in QGIS 2.8. Also on QGIS 2.14, it can't work for the last field name. More over, it is not good performance wise as format_form() is called for every field in the layer which is not desirable.

Is there a fix for this?

  • Isn't that the same as using aliases? – Matthias Kuhn Jul 17 '16 at 8:07
  • Yes sir. Is there a way to do that using python? – wondim Jul 17 '16 at 12:58
2

The proper way to dealing with this is to use an alias.

You can use addAttributeAlias for this (as usual, check the documentation for this)

This way it will not only be used in python-preprocessed forms but also in the attribute table and other places.

  • That is very elegant! Thank you! Is there a way to also change the Window Title of the form with changing the layer name? – wondim Jul 17 '16 at 16:40
  • I meant, is there a way to also change the Window Title of the form without changing the layer name? – wondim Jul 17 '16 at 16:47
  • 1
    What's wrong with changing the layer name? The layer name is already something human readable. (Analogy: field name => alias, table name => layer name) – Matthias Kuhn Jul 17 '16 at 23:17
  • Agreed, Mattias! I will leave that to the Users for version 2.8. – wondim Jul 18 '16 at 6:52

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.