Uppercase input in QLineEdit python way

给你一囗甜甜゛ 提交于 2021-02-10 08:09:07

问题


I had drawn up an UI using the QT Designer but found out that there are no parameters for me to set QLineEdit inputs to be uppercase.

After doing some online searching, I have only seen a very few handful of results that cater to my needs, however all are coded in Qt. Example, this link

And so, are there ways for me to do this in the pythonic way?


回答1:


Try this, I believe this serves your purpose. I won't call it much pythonic. More like PyQt override.

#minor code edit

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_())



回答2:


The simplest way would be to use a validator.

This will immediately uppercase anything the user types, or pastes, into the line-edit:

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_())



回答3:


Hey i know i am kind of late, but I hope this might help some one else like me who spent some time searching for this

Mycase: I was trying to convert only the first letter to capital and this is what i ended up with and it worked (just a beginner in python so if you can make this more pythonic please let me know)

In the defining function: line_edit_object.textChanged.connect(lambda:auto_capital(line_edit_object))

the function auto_capital:

def auto_capital(line_edit_object):
    edit=line_edit_object
    text=edit.text()
    edit.text(text.title())

this shall fix every issue. Feel free to make it more pytonic.



来源:https://stackoverflow.com/questions/28962266/uppercase-input-in-qlineedit-python-way

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!