0

I would like to perform input validation on a customized QTableWidget that has its setItemDelegate to a subclass of QStyledItemDelegate. The input validation works and my error message pops up correctly, but the focus moves on to the next cell selection (ie: if I pressed TAB it will perform my input validation, print a msg if bad input, and then move the focus to the adjacent cell). I would like the focus to remain on the first cell until the input is correct.

Perhaps if I could edit the TAB traversal so that I manually control the traversal in the table (ie: check if input is valid then traverse with TAB) I could achieve input validation; however, I am unaware of a method to modify the table's (QTableWidget) default TAB traversal (described in the detailed description of the superclass QAbstractItemView).

Below is the relevant code:

class TheEditor(QLineEdit):
    # a signal to tell the delegate when we have finished editing
    editingFinished = Signal()

    def __init__(self, parent=None):
            # Initialize the editor object
            super(TheEditor, self).__init__(parent)
            self.setAutoFillBackground(True)
            self.setValidator(QIntValidator(0,999999999, self))

    def focusOutEvent(self, event):
            # Once focus is lost, tell the delegate we're done editing
            self.editingFinished.emit()

class EditDelegate(QStyledItemDelegate):
    def __init__(self, parent=None):
            super(EditDelegate, self).__init__(parent)

    def createEditor(self, parent, option, index):
            # Creates and returns the custom editor object we will use to edit the cell
            result = index.column()
            if result==0:
                    editor = TheEditor(parent)
                    editor.editingFinished.connect(self.checkInput)
                    return editor
            else:
                    return QStyledItemDelegate.createEditor(self, parent, option, index)

    def errorMessage(self, error_message):
            newQWidget = QWidget()
            QtGui.QMessageBox.critical(newQWidget, "Invalid Entry", error_message, QtGui.QMessageBox.Retry)

    def checkInput(self):
            # ... some code here that does validation
            if result == expected: # good input!
                    self.commitData.emit(editor)
                    self.closeEditor.emit(editor, QAbstractItemDelegate.EditNextItem)
                    return True
            else: # bad input!
                    self.errorMessage("Invalid!")
                    self.closeEditor.emit(editor, QAbstractItemDelegate.NoHint)
                    return True

Does anyone have any suggestions to achieve input validation? I found a similar question here but I wasn't able to implement it so that it worked.

David Yee
  • 3,515
  • 25
  • 45

1 Answers1

0

Maybe checkInput can return False in case of bad input. Then use this information to define the current index of your model using setCurrentIndex(index).

Frodon
  • 3,684
  • 1
  • 16
  • 33