[PyQt] QValidator.validate() returns TypeError: invalid result type

Eric Frederich eric.frederich at gmail.com
Thu Aug 5 02:22:47 BST 2010


Reading the documentation here...

http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qvalidator.html

... I see that the validate() function is listed twice.
One returning (State, QString, int) and another returning just (State, int)
I would think this means I have the choice to return either 2-tuple or a
3-tuple.

Returning a 2-tuple seems to work fine but I get an error trying to return
(State, QString, and int)

Is this a bug, or did I miss something or interpret the documentation wrong?

Code is below.

As a side note...
I have tried sending this twice before but it never got through to where I
could read it in the archives.
Perhaps I need to be subscribed to the list to post to it??? If so it
doesn't say that on the mailing list web page.

Thanks,
~Eric



#!/usr/bin/env python

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import  *

class EricsValidator(QValidator):

    def validate(self, text, position):
        print 'validate (%s %s)' % (repr(text), repr(position))
        return (QValidator.Intermediate, text, position)

class Form(QDialog):

    def __init__(self, parent=None):

        super(Form, self).__init__(parent)

        self.lineedit = QLineEdit()

        self.validators = []
        layout = QGridLayout()
        layout.addWidget(self.lineedit, 0, 0, 1, 2)
        for i, (name, validator) in enumerate([
            ("Q&IntValidator"   , QIntValidator(self)   ),
            ("Q&DoubleValidator", QDoubleValidator(self)),
            ("&Eric's Validator", EricsValidator(self)  ),
        ]):
            label  = QLabel(name)
            button = QRadioButton()
            label.setBuddy(button)
            self.validators.append((validator, label, button))
            layout.addWidget(label , i + 1, 0)
            layout.addWidget(button, i + 1, 1)
            label.setBuddy(button)

            self.connect(button, SIGNAL("clicked()"), self.update)

        self.validators[-1][2].setChecked(True)

        self.setLayout(layout)

        self.setWindowTitle("Validation Tester")

        self.update()

    def update(self):

        for validator, label, button in self.validators:
            if button.isChecked():
                print 'setting validator to', label.text()
                self.lineedit.setValidator(validator)
                break


if __name__ == '__main__':
    app = QApplication(sys.argv)
    form = Form()
    form.show()
    app.exec_()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20100804/03d19612/attachment.html>


More information about the PyQt mailing list