【发布时间】:2013-07-10 15:51:57
【问题描述】:
我有一个定制的 QLineEdit 编辑器,用于在委托的 QTableWidget 中输入首字母。一旦焦点离开不使用输入掩码(f.i.不使用 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()
【问题讨论】:
-
您应该调用
QLineEdit::focusOutEvent的默认实现,而不是手动发出editingFinished。可能是默认实现做了一些重要的事情。