Question

I have a customized QLineEdit editor for inputting initials in a delegated QTableWidget. I would like to force upper case once the focus has been left without using an input mask (f.i. without using self.setInputMask(">AA"))

Notes:
- the QLineEdit text does get changed to uppercase when called
- the new uppercase text does not get reflected in the QLineEdit when focus is lost

See the custom class below:

class InitialsEditor(QLineEdit):
    # The custom editor for editing the Initials

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

    def __init__(self, parent=None):
            # Initialize the editor object
            super(InitialsEditor, self).__init__(parent)
            self.setAutoFillBackground(True)
            rx = QRegExp("[A-Z]{1,2}") # validate A-Z with 2 characters
            rx.setCaseSensitivity(Qt.CaseInsensitive)
            self.setValidator(QRegExpValidator(rx, self)) # limit the input to A-Z
            #self.setMaxLength(2) # limit the max char length
            #self.setInputMask(">AA")

    def focusOutEvent(self, event):
            # Once focus is lost, tell the delegate we're done editing
            self.setText(self.text().upper()) # make the text uppercase
            print(self.text()) # returns the correct self.text() in uppercase...
            self.editingFinished.emit()
Était-ce utile?

La solution

Found out an alternative solution based on this answer. This solution results in only caps being entered regardless of case sensitive input (f.i. a or A results in A).

The solution involves switching out the editingFinished event with textEdited event and associating it with the following new definition for my Class InitialsEditor:

def updatedText(self):
            self.setText(self.text().upper())
            QApplication.instance().processEvents()
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top