[PyKDE] custom QValidator

Phil Thompson phil at riverbankcomputing.co.uk
Sat Nov 25 18:02:38 GMT 2006


On Wednesday 22 November 2006 8:33 pm, Adam Tenderholt wrote:
> I'm trying to create a custom validator, but I'm not having any luck.
> The class is reproduced below. When the custom validator is
> initialized, it prints the debug message. However,the validate
> function is never called. Any ideas?
>
> This is PyQt-4.1 with Qt-4.2.1 (Mac/opensource), sip-4.5, and python 2.4.3.
>
> Thanks,
>
> Adam
>
> ------
> class ElementValidator(QtGui.QValidator):
>     def __init__(self, parent = None):
>         QtGui.QValidator.__init__(self, parent)
>         self.regexp = QtCore.QRegExp("[A-Z]")
>         print "validator created"
>
>     def validate(self, inputStr, pos):
>         print "validating"
>         if len(inputStr) > 2:
>             return (QtGui.QValidator.Invalid,pos)
>
>         if not self.regexp.exactMatch(inputStr[0]):
>             return (QtGui.QValidator.Invalid,pos)
>
>         try:
>             table.element.index(inputStr)
>         except ValueError:
>             return (QtGui.QValidator.Intermediate,pos)
>         else:
>             return (QtGui.QValidator.Acceptable,pos)

A complete example would help...

Attaching the validator to a QLineEdit didn't print "validating" but it did 
raise the expected exception about "table", ie. the method is being called.

Phil




More information about the PyQt mailing list