0

I make a QWidget object in which there are some lineEdits and I intend to add some constraints to them, so I implement QDoubleValidator objects.Following is the related part in my code.

self.inductance = QLineEdit()
self.inductance.setValidator(QDoubleValidator(0.99,99.99,1))

enter image description here

I can write '123', but cant '123.45'

full code of the program https://pastebin.com/5y4fnddc

Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • Does this answer your question? [I use QDoubleValidator in my pyqt5 program but it doesn't seem to work](https://stackoverflow.com/questions/54741145/i-use-qdoublevalidator-in-my-pyqt5-program-but-it-doesnt-seem-to-work) – musicamante Jan 15 '22 at 20:14
  • Note: a validator doesn't *always* prevent typing potentially invalid values, as its scope is to provide an interface that prevents *some* typing while allowing *intermediate* values to allow editing and query the validator about it. If you want to ensure that the written value is within the range you must *first* call [`validate()`](https://doc.qt.io/qt-5/qdoublevalidator.html#validate) and eventually fix the returned value if the result is not `Acceptable` (thus, it's not within the selected range). Also consider using [QDoubleSpinBox](https://doc.qt.io/qt-5/qdoublespinbox.html) – musicamante Jan 15 '22 at 20:18
  • Also, please remember that questions should be self-contained and not rely on external websites that could make data unavailable to some people or that could be deleted sooner or later. For instance, your pastebin will expire in 6 days, after that your code will not be available anymore, and your question will become partially invalid. – musicamante Jan 15 '22 at 20:20
  • Please remember that Stack Overflow is not your favourite Python forum, but rather a question and answer site for all programming related questions. Thus, please always include the tag of the language you are programming in, that way other users familiar with that language can more easily find your question. – Adriaan Jul 20 '22 at 08:04

1 Answers1

0

you need to change "QDoubleValidator(0.99, 99.99, 1)" into "QDoubleValidator(0.99, 99.99, 2)" like the following script:

from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtGui import QDoubleValidator

self.inductance = QLineEdit()
self.inductance.setValidator(QDoubleValidator(0.99, 99.99, 2))
  • 1
    Welcome to Stack Overflow! Why should I or anyone else for that matter "try this"? Please read [answer] and [edit] your question to contain an explanation as to why this code would actually solve the problem at hand. Always remember that you're not only solving the problem, but are also educating the OP and any future readers of this post. – Adriaan Jul 20 '22 at 08:04
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 21 '22 at 05:33