Esempio n. 1
0
 def __init__(self,
              parent,
              typ,
              curvalue,
              fmtstr='%.4g',
              minmax=None,
              allow_enter=False):
     QLineEdit.__init__(self, parent)
     self._typ = typ
     if typ is float:
         val = DoubleValidator(self)
         if minmax:
             # setRange doesn't work correctly in some Qt versions...
             val.setBottom(minmax[0])
             if minmax[1] is not None:
                 val.setTop(minmax[1])
         self.setValidator(val)
         self.setText(fmtstr % curvalue)
     elif typ is int:
         val = QIntValidator(self)
         if minmax:
             val.setRange(minmax[0], minmax[1])
         self.setValidator(val)
         self.setText(str(curvalue))
     else:
         self.setText(str(curvalue))
     self.textChanged.connect(lambda txt: self.valueModified.emit())
     if allow_enter:
         self.returnPressed.connect(
             lambda: self.valueChosen.emit(self._typ(self.text())))
Esempio n. 2
0
def test_double_validator():
    inf = float('inf')
    # valid cases
    validator = DoubleValidator()
    for args in [
        ('0', -inf, inf),
        ('0.0', -inf, inf),
        ('0.0e0', -inf, inf),
        ('-0.0', -inf, inf),
        ('1.23456789123456789e130', -inf, inf),
        ('0', -1, 1),
        ('0', 0, inf),
        ('0', -inf, 0),
    ]:
        validator.setBottom(args[1])
        validator.setTop(args[2])
        assert validator.validate(args[0], 0)[0] == QValidator.Acceptable
    # intermediate cases
    for args in [
        ('4', 10, 50),
        ('-4', -50, -10),
        ('1.0e', -inf, inf),
        ('0', 10, 20),
    ]:
        validator.setBottom(args[1])
        validator.setTop(args[2])
        assert validator.validate(args[0], 0)[0] == QValidator.Intermediate
    # invalid cases
    for args in [
        ('-15', 10, 20),
        ('-1', 10, 20),
        ('+15', -20, -10),
        ('+1', -20, -10),
        ('1,5', 0, 10),
    ]:
        validator.setBottom(args[1])
        validator.setTop(args[2])
        assert validator.validate(args[0], 0)[0] == QValidator.Invalid