Uppercase input in QLineEdit python way

Question:

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?

Asked By: dissidia

||

Answers:

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_())
Answered By: smitkpatel

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_())
Answered By: ekhumoro

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.setText(text.title())

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

Answered By: Ja8zyjits

I am also late but after contemplating on this question I think this is some sort of pythonic way of accomplishing it in PyQt5:

class CustomInput(QLineEdit):
    def __init__(self):
        super().__init__()
        self.textChanged.connect(self.text_changed)
    
    def text_changed(self):
        if self.text().isupper():
            return
        self.setText(self.text().upper())
Answered By: Atalay
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.