Currently I'm converting code from PyQt5 to PyQt6. In Qt5 I had validators in place for integer and double values. Both validators (int and double) in Qt5 did reset the QLineEdit back to the last valid value in there if the user tried to enter something out of range. This by default, without adding special code to do so.
Now in PyQt6 there is just the limitation that characters are blocked and the int field does not allow the decimal point. But no range checking at all.
My test code:
from PyQt6.QtGui import QDoubleValidator, QIntValidator
from PyQt6.QtWidgets import QApplication, QMainWindow, QLineEdit, QVBoxLayout, QWidget
import sys
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.build_window()
def build_window(self):
self.double_e = QLineEdit()
self.double_v = QDoubleValidator(10, 20, 2)
self.double_v.setNotation(QDoubleValidator.Notation.StandardNotation)
self.double_e.setValidator(self.double_v)
self.int_e = QLineEdit()
self.int_v = QIntValidator(10, 20)
self.int_e.setValidator(self.int_v)
self.layout = QVBoxLayout()
self.layout.addWidget(self.double_e)
self.layout.addWidget(self.int_e)
container = QWidget()
container.setLayout(self.layout)
self.setCentralWidget(container)
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
Has the validator behaviour changed between PyQt5 and PyQt6?
How do I get the old functionality back that input fields are checked upon leave for beeing in range and reset back to the last (valid) value if this is not the case?