0

I end up with 2 different behaviors of the same code when launching on Spyder (Python 3.9.7) and on the Terminal (Mac OS) (Python 3.10.1)

The issue relates to the validation of input in a field within a GUI.

In both cases (Spyder and Terminal), QIntValidator does block typing string or float as expected. But it does not limit the value according to the expected behavior.

Here are the experts of my code related to the field and the data validator:

        validatorDim = QIntValidator(1, 25, self)

        self.longueurEdit = QLineEdit(self)

        self.longueurEdit.setValidator(validatorDim)

The behavior in Spyder: I cannot type any value above 25 (expected behavior). The issue in Terminal: I can type values up to 99.

Any idea on how to solve this?

kAmJi
  • 77
  • 12
  • 1
    What actually matters here is the actual Qt version: since you've two different environments, you could also have different versions. Do `from PyQt5 import QtCore;print(QtCore.QT_VERSION_STR)` and consider that there have been some changes lately in the way validator behaves with numbers, some of them might seem unintuitive, but the reality is that the validator should not always prevent typing, but partially limit input and, most importantly, *validate* it: the point is that there are some specific cases of editing that must be possible, even if temporarily results in invalid values. – musicamante May 17 '22 at 21:14
  • Tks a lot @musicamante ! Those are actually not the same versions, and in the latest version, indeed it seems the behavior has changed https://stackoverflow.com/questions/70440447/how-to-limit-numbers-accepted-in-a-lineedit. Does not solve my problem but at least I understand :D – kAmJi May 17 '22 at 21:38
  • 1
    For simple cases, you could override the validator and change the result of [`validate()`](https://doc.qt.io/qt-5/qintvalidator.html#validate) to `Invalid` when it's outside the given range. This is possible because the relation between minimum (single digit, positive integer) and maximum (2 digits) values allows a simpler validation method. While a more complex validator *could* be possible, the amount of required implementation for all possible combinations would make the code (and behavior) of the validator and line edit cumbersome. – musicamante May 17 '22 at 22:04
  • Noted, thanks! Should that be an answer, I would have accepted :) – kAmJi May 20 '22 at 05:47

0 Answers0