我有一个自定义的 QLineEdit 编辑器,用于在委托的 QTableWidget 中输入首字母。一旦焦点离开而不使用输入掩码,我想强制大写(fi 不使用 self.setInputMask(">AA"))
注意:
- QLineEdit 文本在调用时会变为大写
- 当焦点丢失时,新的大写文本不会反映在 QLineEdit 中
请参阅下面的自定义类:
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()