Search code examples
pythonpyqtpyqt5titleqlineedit

Issue in QLineEdit with title()?


In my Programme. use two QLineEdit. First one is normal and second one is titled Line edit. First one/normal QLineEidt works smoothly, But In second textbox(QLineEdit), I cannot insert a text at begging or any where at a time.

for example : I entered a text "Python". Now I add "Hello" to begging of the text ("Hello Python"). If I try to type "Hello", I can insert only one word at a time, (press home key, type word "H",after that cursor jumps to end, once again we move cursor to second position and enter a word "O", Once we enter word "O" cursor jumps to end of the text and so on). I want to type(insert) a text at a stretch.

How to OverCome?

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QFont

class Lineedit_title(QWidget):
    def __init__(self):
        super().__init__()

        self.setGeometry(100,100,500,500)

        self.textbox1 = QLineEdit(self)
        self.textbox1.setGeometry(50,50,200,50)
        self.textbox1.setFont(QFont("Caliber", 15, QFont.Bold))

        self.textbox2 = QLineEdit(self)
        self.textbox2.setGeometry(50,140,200,50)
        self.textbox2.setFont(QFont("Caliber",15,QFont.Bold))
        self.textbox2.textChanged.connect(self.textbox_textchange)

    def textbox_textchange(self,txt):
        self.textbox2.setText(txt.title())

def main():
    app = QApplication(sys.argv)
    win = Lineedit_title()
    win.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

Solution

  • The problem is that when you use setText() the cursor position is set at the end of the text, in this case the cursor position must be saved and restored before and after setText(), respectively:

    def textbox_textchange(self, txt):
        position = self.textbox2.cursorPosition()
        self.textbox2.setText(txt.title())
        self.textbox2.setCursorPosition(position)

    A more elegant solution is to use a custom validator:

    import sys
    from PyQt5.QtWidgets import QApplication, QLineEdit, QWidget
    from PyQt5.QtGui import QFont, QValidator
    
    
    class TitleValidator(QValidator):
        def validate(self, text, pos):
            return QValidator.Acceptable, text.title(), pos
    
    
    class Lineedit_title(QWidget):
        def __init__(self):
            super().__init__()
    
            self.setGeometry(100, 100, 500, 500)
    
            self.textbox1 = QLineEdit(self)
            self.textbox1.setGeometry(50, 50, 200, 50)
            self.textbox1.setFont(QFont("Caliber", 15, QFont.Bold))
    
            self.textbox2 = QLineEdit(self)
            self.textbox2.setGeometry(50, 140, 200, 50)
            self.textbox2.setFont(QFont("Caliber", 15, QFont.Bold))
    
            validator = TitleValidator(self.textbox2)
            self.textbox2.setValidator(validator)
    
    
    def main():
        app = QApplication(sys.argv)
        win = Lineedit_title()
        win.show()
        sys.exit(app.exec_())
    
    
    if __name__ == "__main__":
        main()