This class is similar to QDoubleValidator, but it improves slightly on the editing comfort.
Are there any corner- or special cases of inputs I didn't consider, which would lead to bad behaviour?
Is it wrong to assume the last inserted character is left of the cursor (for the case that I'm sure that a character was inserted, because there are two separators, or too many digits at the end)?
class DecimalValidator(QDoubleValidator): '''This is intended to be used with a QLineEdit. It has some features of an inputMask, but without the disadvantages. For inputMask, the right alignment and the inserting of digits on the left hand side of the separator isn't really nice.''' def __init__(self, *args): QDoubleValidator.__init__(self, *args) self.setNotation(QDoubleValidator.StandardNotation) def validate(self, input, pos): sep = self.locale().decimalPoint() if pos and (input[pos-1]==sep) and (sep in input[pos:]): # When we're left of the separator, and separator is entered again, # remove the inserted separator and move right of the old separator. input = input[:pos-1] + input[pos:] pos = input.find(sep)+1 elif sep in input[:pos] and (len(input.split(sep)) > self.decimals()): # When we're right of the separator, and all decimal places are used already, # go into overwrite mode (by removing the old digit) input = input[:pos] + input[pos+1:] return QDoubleValidator.validate(self, input, pos)