[PyQt] QAbstractSpinBox.fixup() error
Phil Thompson
phil at riverbankcomputing.com
Tue Feb 16 12:37:53 GMT 2010
On Tue, 16 Feb 2010 07:10:51 -0500, David Wolfe <dwolfe at fifthsally.com>
wrote:
> 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
> invalid.
>
> 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.
>
> Thanks,
> Dave
>
> ['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)
> try:
> int(input)
> return (QValidator.Intermediate, pos)
> except ValueError:
> return (QValidator.Invalid, pos)
The input is a mutable QString. Your normalize_id() function needs to
modify the QString it is passed.
Phil
More information about the PyQt
mailing list