3

I am trying to use QDoubleValidator in an application to validate float numbers in the range 0.00 to 99.99 that are enter in a QLineEdit widget. For that I use a structure like the following:

self.e2 = QLineEdit()
self.e2.setValidator(QDoubleValidator(0.00,99.99,2))
<mode code> 
self.e2.editingFinished.connect(self.__call_float)

I expect that the function __call_float would be called whenever the the input is in this range 0.00, 99.99 and has 2 decimals or less.

The result is different. The forms accepts values with one or two integer digits as: "1.", "22.", "22" but not values with 2 integer digits and 2 decimal values as "22.22", "33.33". There are other values that are accepted as "1.2", "0.22". One particular aspect that is weird is that the behavior does not change when I modify the maximum number of decimals to be 4 as:

self.e2.setValidator(QDoubleValidator(0.00,99.99,4))

I do not thing this is the expected behavior.

I attach a complete application as reference:

import sys
from PyQt5 import QtCore, QtGui
from PyQt5.QtWidgets import QApplication, QWidget, QLineEdit, QFormLayout, QMainWindow
from PyQt5.QtGui import QDoubleValidator

class MainWindow(QMainWindow):

    def __init__(self):
        super(MainWindow, self).__init__()

        self.win= QWidget()

        self.e2 = QLineEdit()
        self.e2.setValidator(QDoubleValidator(0.00,99.99,2))

        flo = QFormLayout()
        flo.addRow("Input float",self.e2)
        self.e2.editingFinished.connect(self.__call_float)

        self.win.setLayout(flo)
        self.win.setWindowTitle("PyQt")
        self.win.show()

        sys.exit(app.exec_())

    def __call_float(self):
        print(self.e2.text())



if __name__ == '__main__':
        app = QApplication(sys.argv)
        mainwindow = MainWindow() 

        mainwindow.show()

        sys.exit(app.exec_())
Juan Osorio
  • 378
  • 3
  • 15

0 Answers0