【问题标题】:Change Case to Capital on focusOutEvent在 focusOutEvent 上将案例更改为大写
【发布时间】: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。可能是默认实现做了一些重要的事情。

标签: qt pyqt pyside qlineedit


【解决方案1】:

找到基于this answer 的替代解决方案。此解决方案导致只输入大写字母,而不管输入是否区分大小写(f.i. a 或 A 结果为 A)。

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

def updatedText(self):
            self.setText(self.text().upper())
            QApplication.instance().processEvents()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-24
    • 2011-08-11
    • 1970-01-01
    • 1970-01-01
    • 2014-04-20
    • 2010-09-25
    • 1970-01-01
    • 2011-10-06
    相关资源
    最近更新 更多