I would like to store the information about whether a checkbox is checked or not in a boolean column in my database (running on PostGreSQL 9.3). I'm using a custom form, and the corresponding checkbox field should appear as checked or unchecked depending on its value in the database - and the value in the database should update if the user changes the state of the checkbox.

I created a variable for the checkbox in my Python script:

global zone_humide
zone_humide = dialog.findChild(QCheckBox, "zone_humide")

I also made sure that the field has the same name as its corresponding column in the database. But when I check or uncheck the box, the change is not recorded in the database - and consequently, I can't call back the current state of the box when I open my form again.

  • 3
    So what's the problem? – Evan Carroll Dec 8 '17 at 2:21

I ended up using a workaround by creating a QLineEdit which I associate to my checkbox with a function (same logic as what is described here). Every time the checkbox is checked, the LineEdit text changes to 't' - otherwise the text is 'f'. It's not very pretty code-wise, but it works and it is not noticeable by the final user. If anyone as a solution that involves less tinkering, I'd be happy to hear about it.


@Mefimefi, just setting the "Edit widget" property as "Check Box" in the layer properties in QGIS works for me.

  • I had tried that, using different ways to represent the state (1/0, t/f...), but I couldn't make it work unfortunately. – Mefimefi Aug 16 '18 at 14:21

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.