[Python] Validate Dialog with multiple and various fields

Discussão em 'Python' iniciado por Stack, Maio 29, 2025.

  1. Stack

    Stack Membro Participativo

    Using PyQt5 Dialog created by QtCreator which contains multiple data fields, is it possible to validate with various criteria and fix errant fields without closing the Dialog? ie. The only time the Dialog closes is if it is explicitly closed with "Close" or if all the fields are validated when "Save" is pressed. Ideally the focus would then be on that errant field I have been unable to find any examples of this online and would be grateful if anyone knows of any.

    Update: I have attempted to make this work by setting the class variable error_field to the errant field. When the Dialog is saved and there is a validation error the focus is set to this field. However processing continues and there is no opportunity to change the field. I have tried to use various loops to no avail. If anyone could point out the error of my ways I would be gratefull. The below quasi pseudo code shows (I hope) the idea.

    class BUConfig(QtWidgets.QDialog): error_field = ''

    def __init__(self, ini):
    super(BUConfig, self).__init__()
    self.error_field = ''

    etc.

    self.populate(ini)

    def populate(self, ini):
    errNo, ini = cfg.getINIvalues('gui', config_file)
    self.ui.br_arch_dir_le.setText(ini.aDir)
    .
    etc.

    def closeEvent(self, event):
    if self.error_field:
    event.ignore()
    self.error_field.setFocus()
    else: event.accept()
    self.error_field = ''

    def validate_file_path(self, ini, .........):
    if error, return errNo

    def saveConfig(self, ini): #From SAVE button
    errNo = self.validate_file_path(ini, self.ui.br_arch_dir_le.text())
    if errNo != 0:
    self.error_field = self.ui.br_arch_dir_le
    self.close() # calls closeEvent()

    errNo = self.validate_file_path(...........next field


    self.close()
    self.closeWindow()

    return ini



    if name == "main": import ........

    application = BUConfig(ini)
    application.show()
    sys.exit(app.exec_())

    Continue reading...

Compartilhe esta Página