Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

When installing the validator, the number of characters entered is limited. [Python]

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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_())
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading