我有一个自定义的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()
答案 0 :(得分:0)
找到基于this answer的替代解决方案。无论区分大小写输入(f.i. a或A结果为A),此解决方案都只会输入大写字母。
解决方案涉及使用textEdited事件切换editFinished事件,并将其与我的Class InitialsEditor的以下新定义相关联:
def updatedText(self):
self.setText(self.text().upper())
QApplication.instance().processEvents()