[PyQt] QAbstractSpinBox.fixup() error

David Wolfe dwolfe at fifthsally.com
Tue Feb 16 12:10:51 GMT 2010

Hi, all.  Been a bit confused recently by the behavior of
QAbstractSpinBox.fixup().  I have a class that derives from QSpinBox
with the goal of ensuring that the user only enters integers whose
last two digits form a number between 0 and 31, inclusive.  For
example, 19900 is valid, as is 19931; but 19932 through 19999 are

Because I don't know when the user enters '550' (which would be
invalid by itself) whether he might be on the way to entering '5501'
(valid), my validate() function returns QValidator.Intermediate for
any integral input.

I thought I could do 'final' validation by overriding fixup(), but I
keep seeing the following error in the console:

   TypeError: invalid result type from IdSpinBox.fixup()

This error only goes away if I stop trying to return a value from
fixup(), but... if that's the correct signature, how should I fix
the input?

Relevant code appended below.  I'm using PyQt-x11-gpl-4.7 with
Python-2.6.4 and Qt-4.6.2.


['free' function]
def normalize_id(id):
     """Return the ID with all but the last two digits stripped"""
     if not isinstance(id, str):
         id = str(id)
     return int(re.sub(".*([0-9]{2})", "\\1", id));

     ... [IdSpinBox functions] ...

     def fixup(self, input):
         norm_id = normalize_id(input)
         if 0 <= norm_id <= 31:  # Okay
             return input
         return QString(self.fallback_value)

     def validate(self, input, pos):
         if not input:
             return (QValidator.Intermediate, pos)
             return (QValidator.Intermediate, pos)
         except ValueError:
             return (QValidator.Invalid, pos)

More information about the PyQt mailing list