QLineEdit python方式的大写输入

dis*_*dia 2 python pyqt qlineedit

我使用QT Designer绘制了一个UI,但发现我没有参数可以将QLineEdit输入设置为大写.

在做了一些在线搜索之后,我只看到了一些满足我需求的结果,但是所有结果都用Qt编码.例如,这个链接

那么,我有办法以pythonic的方式做到这一点吗?

ekh*_*oro 6

最简单的方法是使用验证器.

这将立即将用户键入或粘贴的任何内容大写到行编辑中:

from PyQt4 import QtCore, QtGui

class Validator(QtGui.QValidator):
    def validate(self, string, pos):
        return QtGui.QValidator.Acceptable, string.upper(), pos
        # for old code still using QString, use this instead
        # string.replace(0, string.count(), string.toUpper())
        # return QtGui.QValidator.Acceptable, pos

class Window(QtGui.QWidget):
    def __init__(self):
        super(Window, self).__init__()
        self.edit = QtGui.QLineEdit(self)
        self.validator = Validator(self)
        self.edit.setValidator(self.validator)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.edit)

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.setGeometry(500, 300, 300, 100)
    window.show()
    sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)


smi*_*tel 3

试试这个,我相信这符合你的目的。我不会称其为Pythonic。更像是 PyQt 覆盖。

#小代码编辑

from PyQt4 import QtGui
import sys
#===============================================================================
# MyEditableTextBox-  
#===============================================================================
class MyEditableTextBox(QtGui.QLineEdit):
#|-----------------------------------------------------------------------------|
# Constructor  
#|-----------------------------------------------------------------------------|

    def __init__(self,*args):
        #*args to set parent
        QtGui.QLineEdit.__init__(self,*args)

#|-----------------------------------------------------------------------------|
# focusOutEvent :- 
#|-----------------------------------------------------------------------------|
    def focusOutEvent(self, *args, **kwargs):
        text = self.text()
        self.setText(text.__str__().upper())
        return QtGui.QLineEdit.focusOutEvent(self, *args, **kwargs)


#|--------------------------End of focusOutEvent--------------------------------|
#|-----------------------------------------------------------------------------| 
# keyPressEvent
#|-----------------------------------------------------------------------------|
    def keyPressEvent(self, event):
        if not self.hasSelectedText():
            pretext = self.text()
            self.setText(pretext.__str__().upper())
        return QtGui.QLineEdit.keyPressEvent(self, event)

#|--------------------End of keyPressEvent-------------------------------------|

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    w = QtGui.QWidget()
    lay = QtGui.QHBoxLayout()
    w.setLayout(lay)
    le1 = MyEditableTextBox()
    lay.addWidget(le1)
    le2 = MyEditableTextBox()
    lay.addWidget(le2)
    w.show()
    sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)

  • @smitkpatel。您可以在重新设置文本之前调用“QLineEdit.keyPressEvent”来消除最后一个字符输入的问题(请注意,您不需要“return”)。另外,重置文本的更简单方法是使用 self.setText(self.text().toUpper()) 。 (2认同)