There was a problem, when installing the validator in PyQt5, the number of characters is limited to 1.
Why might this be happening?
I give an example code below::
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QLabel, QGridLayout, QLineEdit
from PyQt5 import QtWidgets, QtCore, QtGui
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 700, 700)
self.setWindowTitle('test')
centralwidget = QWidget(self)
layout = QGridLayout(self)
centralwidget.setLayout(layout)
fe = QLineEdit()
we = QLineEdit()
layout.addWidget(fe,0,0,1,2)
layout.addWidget(we,1,0,1,2)
valid = QtCore.QRegExp("[0-9 .,] {100}")
val = QtGui.QRegExpValidator(valid)
fe.setValidator(val)
# fe.setMaxLength(4)
we.setValidator(val)
# we.setMaxLength(4)
self.setCentralWidget(centralwidget)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
>Solution :
try:
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtWidgets import QMainWindow, QApplication, \
QWidget, QLabel, QGridLayout, QLineEdit
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
centralwidget = QWidget(self)
self.setCentralWidget(centralwidget)
# layout = QGridLayout(self)
layout = QGridLayout(centralwidget) # +++
fe = QLineEdit()
we = QLineEdit()
layout.addWidget(fe, 0, 0, 1, 2)
layout.addWidget(we, 1, 0, 1, 2)
# valid = QtCore.QRegExp("[0-9 .,] {100}")
valid = QtCore.QRegExp("[0-9 .,]{15}") # !!! +++
val = QtGui.QRegExpValidator(valid)
fe.setValidator(val)
we.setValidator(val)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
ex.resize(500, 500)
ex.setWindowTitle('QRegExp QRegExpValidator')
ex.show()
sys.exit(app.exec_())