I'm using QGIS 3.22 trying to go through a tutorial (3.16 training manual) and I followed the popular Nathan Woodrow's blog setting custom form with python logic where I changed the PyQt4 imports for PyQt5. I set up a vector layer exactly as described by Woodrow and an UI form that works seamlessly.
from PyQt5.QtWidgets import QLineEdit, QDialogButtonBox, QMessageBox
nameField = None
myDialog = None
def formOpen(dialog, layerid, featureid):
global myDialog
myDialog = dialog
global nameField
nameField = dialog.findChild(QLineEdit, "Name")
buttonBox = dialog.findChild(QDialogButtonBox, "buttonBox")
# change buttonBox.accepted.disconnect(myDialog.accept)
buttonBox.accepted.disconnect()
buttonBox.accepted.connect(validate)
# change buttonBox.rejected.connect(myDialog.reject)
buttonBox.rejected.connect(myDialog.resetValues)
def validate():
if not len(nameField.text()) > 0:
QMessageBox.warning(None, "Attention", "Name can't be null", QMessageBox.Ok)
else:
# change myDialog.accept()
myDialog.save()
To get the Python logic working I figure out that QgsAttibuteForm
's accept
and reject
methods were deprecated and replaced by save
and resetValues
so I changed then accordingly.
Furthermore, when tried to place buttonBox.disconnect(myDialog.save)
I got
TypeError: disconnect failed beetween 'accepted' and 'save'
I just disconnect all signals by using disconnect()
.
Finally I got the form running and the validation working but even after showing the invalid entry, the feature is save with a null value after clicking the ok button.
I tried to place a myDialog.resetValues()
or myDialog.close()
at the validation method with the same results.
I'd like the form to revert to the feature values and stay open for the user to correct the entry.