[PyQt] QValidator raises TypeError

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


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
   

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)

    line_edit = QtGui.QLineEdit()
    line_edit.setValidator(Validator())
    line_edit.show()

    sys.exit(app.exec_())

-- 
+-------------------------------------------------------------+
| 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