I want that my QLineEdit to insert a space after every two characters, I want his because in that QLineEdit I will insert on hexadecimal character. I tried the code from below from this post: PyQt auto-space qlineedit characters and it's work very well but when I want to delete a characters it work only when occurs the last space, and then I can't delete anything.
from PyQt5.QtWidgets import QLineEdit, QApplication
class LineEdit(QLineEdit):
def __init__(self, *args, **kwargs):
QLineEdit.__init__(self, *args, **kwargs)
self.textChanged.connect(self.onTextChanged)
self.setValidator(QRegExpValidator(QRegExp("(\\d+)")))
def onTextChanged(self, text):
if len(text) % 6 == 5:
self.setText(self.text()+" ")
if __name__ == '__main__':
app = QApplication(sys.argv)
le = LineEdit()
le.show()
sys.exit(app.exec_())
If you need to use this class multiple times I think the best way is to inherit a custom class from QLineEdit (like you already did) but add setInputMask() to it.
Try this:
from PyQt5.QtWidgets import QLineEdit, QApplication
import sys
class HexLineEdit(QLineEdit):
def __init__(self, *args, **kwargs):
QLineEdit.__init__(self, *args, **kwargs)
self.setInputMask("HH HH HH HH")
if __name__ == '__main__':
app = QApplication(sys.argv)
le = HexLineEdit()
le.show()
sys.exit(app.exec_())