I have a problem with QGIS 2.4 when introducing layers with a custom UI. More precisely, widgets such as CheckBoxes are not recognised as such (appear as Text Edit in Attributes) and the corresponding Checked value appears as NULL (although checked by default in the UI file).

When I am manually changing the specific field:

instudy_id = layer.fieldNameIndex("instudy")
layer.setEditorWidgetV2(instudy_id, 'CheckBox') 

Everything is normal. The corresponding part of the UI file is:

UI file

Sorry if my explanation is poor, I am quite new to QGIS. Hope the screenshots help.

Layer -> Properties Attribute Table of Layer

To conclude:

  • some options of my Widgets (defined in my .ui file) are overwritten by QGIS (e.g. "Checked" or "Unchecked" state of a QCheckBox)

  • others, which are introduced in the corresponding .py file (like QgsEditorWidgetWrapper.fromWidget( in study ).setEnabled(False) ) are not respected by QGIS.

What is the proper way to introduce a custom / customise an .ui file ? Is there anywhere detailed information/examples on the use of newer methods like QgsVectorLayer.setEditorWidgetV2, setEditorWidgetV2Config, QgsEditorWidgetWrapper ?


What is shown in the layer properties / Fields tab in the column Edit widget is not defined by the .ui file (like you seem to assume as you are saying "recognized"). Instead, it is a configuration that needs to match your .ui-file. You can open it and change the edit widget to Check Box and configure it to your needs.

To set these values with python you can use the following code:

layer.setEditorWidgetV2Config(instudy_id, {u'CheckedState': 1, u'UncheckedState': 0} )

To actually set the value of the widget you can subsequently use the following code to set the value of the widget.

QgsEditorWidgetWrapper.fromWidget( instudy ).setValue( 1 )

The editable state of a widget needs also to be controlled by QGIS, as it needs to enable/disable widgets based on the layer editable state. Therefore there is a special method to tell QGIS which widgets should never be swichted to editable

layer.setFieldEditable( instudy_id, False )

For more information have a look at the API documentation in particular QgsVectorLayer::setEditorWidgetV2

More information about the widget wrapper can also be found here

  • Good to know that the "Edit Widget" column doesn't get any info from the .ui file. And yes, I know that I can change it manually. The thing is that it seems pointless to me. First of all, I can't afford to do this for every single layer I design. Then, what's the purpose of importing a custom UI if it is not taken into account properly (here the "in study" Checkbox is not checked as it was configured to be but I have other similar problems as well). – Stav Sep 10 '14 at 8:34
  • The point is, that you can configure things in QGIS that you are not able to configure in QtDesigner. E.g. the representation of True/False in the data cannot be configured in QtDesigner. Common representations are 1/0, T/F, t/f... You need to tell QGIS which one to use. – Matthias Kuhn Sep 10 '14 at 8:54
  • I see your point. But I am still a bit confused. Is this part then (in the .ui file):<property name="checked"> <bool>true</bool> </property> useless? – Stav Sep 10 '14 at 9:27
  • Pretty much. This is used for the default state (when used as a widget independent of any field) and will be overwritten by QGIS when used in combination with a field. – Matthias Kuhn Sep 10 '14 at 9:37
  • Furthermore, how come when I modify a QLineEdit feature like height_field.setText("0") it is not overwritten by QGIS, however instudy.setChecked(True) which is a QCheckBox is? Same goes for QComboBoxes. Why are these features different than QLineEdits? And should I provide the default state (e.g. Checked=True) in the code (and please how) ? – Stav Sep 10 '14 at 12:54

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.