[PyQt] QValidator raises TypeError

Phil Thompson phil at riverbankcomputing.com
Tue Oct 20 12:08:20 BST 2009


On Tue, 20 Oct 2009 13:01:01 +0200, Mads Ipsen <mpi at comxnet.dk> wrote:
> I am trying to implement a simple validator for a QLineEdit. To do this 
> you should inherit from QtGui.QValidator and overload the method 
> validate(). The validate method should then return either of the flags:
> 
> QtGui.QValidator.Invalid
> QtGui.QValidator.Intermediate
> QtGui.QValidator.Acceptable
> 
> Below is included a simple example that always returns 
> QtGui.QValidator.Acceptable. Nevertheless, the programs raises the error
> 
>   TypeError: invalid result type from Validator.validate()
> 
> no matter what you type in the line edit.
> 
> Any suggestions?
> 
> Best, Mads
> 
> 
> import sys
> from PyQt4 import QtGui
> 
> class Validator(QtGui.QValidator):
>     def __init__(self, parent=None):
>         QtGui.QValidator.__init__(self, parent)
> 
>     def validate(self, string, pos):
>         return QtGui.QValidator.Acceptable

The Python API is different to the C++ API because the latter updates the
reference to "pos". The Python API needs you to return the (possibly
different) position. Try...

        return (QtGui.QValidator.Acceptable, pos)

Phil


More information about the PyQt mailing list