Allow entry in QLineEdit only within range of QDoubleValidator

前端 未结 7 718
傲寒
傲寒 2020-12-16 05:52

I have a set of QLineEdits that are supposed to accept double values within a certain range, (e.g., -15 to 15).

I have something along these lines when

7条回答
  •  时光取名叫无心
    2020-12-16 06:33

    I spent almost a day trying to make QDoubleValidator work with reasonable user feedback when checking for acceptable range of QLineEdit input. My attempts to use Qt prescribed validator::fixup() turned out to be a waste of time. Earlier answers in this thread are much more useful but still have shortcomings. In the end I opted for a different and simpler approach.

    1. Equip QLineEdit with QDoubleValidator which performs no range checking.
    2. In a handler for QLineEdit editingFinished signal do range checking and if necessary reset of QLineEdit text.

    This approach disallows typing of illegal characters, takes care of localization and corrects values outside of desired range.

    Works well for me.

提交回复
热议问题