[PyKDE] Subclassing QValidator

John J. Lee jjl at pobox.com
Tue Feb 5 20:00:33 GMT 2002


#!/usr/bin/env python

"""With PyQt 2.4, I get infinite recursion on typing a character."""

import sys
from qt import *

class FloatValidator(QDoubleValidator):
    def fixup(self, input):
        QDoubleValidator.fixup(self, input)
        if input.length() == 0:
            input.insert(0, "None")

    def validate(self, input, posn):
        state, posn = QDoubleValidator.validate(self, input, posn)
        if state != QValidator.Invalid:
            return state, posn
        if input == "None":
            return QValidator.Acceptable, posn
        elif "None".find(str(input)) == 0:
            return QValidator.Intermediate, posn
##         else:
##             return QValidator.Invalid, posn

class FloatLineEdit(QLineEdit):
    """Floating-point LineEdit, accepts empty input as 'None'."""
    def __init__(self, *args):
        QLineEdit.__init__(self, *args)
#        self.setValidator(QDoubleValidator(self))
        self.setValidator(FloatValidator(self))

    def sizeHint(self):
        sz = QLineEdit.sizeHint(self)
        return QSize(sz.width()/2, sz.height())


a = QApplication(sys.argv)
mw = FloatLineEdit(None)
#mw = QLineEdit(None)
mw.show()
a.connect(a, SIGNAL("lastWindowClosed()"), a, SLOT("quit()"))
a.exec_loop()


# Also, QMessageBox.about doesn't seem look for icon method in parent
# (sorry, no example code), contrary to the Qt docs.


# John





More information about the PyQt mailing list