[PyQt] QValidator raises TypeError

Mads Ipsen mpi at comxnet.dk
Tue Oct 20 12:18:06 BST 2009


Phil Thompson wrote:
> 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
>
>   
Yep, that does it.

Thanks!!!

-- 
+-------------------------------------------------------------+
| Mads Ipsen, Scientific developer                            |
+-------------------------------+-----------------------------+
| QuantumWise A/S               | phone:         +45-29716388 |
| Nørre Søgade 27A              | www:    www.quantumwise.com |
| DK-1370 Copenhagen K, Denmark | email:  mpi at quantumwise.com |
+-------------------------------+-----------------------------+




More information about the PyQt mailing list