This class is similar to QDoubleValidator, but it improves slightly on the editing comfort. The comments mention "comma" but it's actually the decimal point of your locale (comma for me).
class DecimalValidator(QDoubleValidator):
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 comma, and comma is pressed,
# remove the inserted comma and move right of the old comma.
input = input[:pos-1] + input[pos:]
pos = input.find(sep)+1
elif sep in input[:pos] and (len(input.split(sep)[1]) > self.decimals()):
# When we're right of the comma, 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)