0

我有一个自定义的 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()
4

1 回答 1

0

根据这个答案找到了一个替代解决方案。此解决方案导致只输入大写字母,而不管输入是否区分大小写(fi a 或 A 结果为 A)。

该解决方案涉及将 editingFinished 事件与 textEdited 事件切换,并将其与我的 Class InitialsEditor 的以下新定义相关联:

def updatedText(self):
            self.setText(self.text().upper())
            QApplication.instance().processEvents()
于 2013-07-12T23:37:57.590 回答