我有一个自定义的QLineEdit编辑器,用于在委托的QTableWidget中输入缩写。我想在不使用输入掩码的情况下在离开焦点后强制使用大写字母(例如,不使用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()
根据此答案找到了替代解决方案。该解决方案导致仅输入大写字母,而不考虑区分大小写的输入(fi a或A导致A)。
该解决方案涉及用textEdited事件关闭editFinished事件,并将其与我的Class InitialsEditor的以下新定义相关联:
def updatedText(self):
self.setText(self.text().upper())
QApplication.instance().processEvents()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句