[PyQt] List-like objects and QVariant

Phil Thompson phil at riverbankcomputing.com
Thu Jan 21 12:34:27 GMT 2010

On Wed, 20 Jan 2010 12:48:44 +0100, "Sybren A. Stüvel" <sybren at stuvel.eu>
> Hi all,
> This is my first post to this list. I'm a software engineer at Chess in 
> Haarlem, The Netherlands, and I'm using PyQt to develop a testing
> application. 
> We created a TestSuite class that contains a filename and an ordered 
> dictionary of TestCase objects The TestSuite class implements the
> __getitem__ 
> method to get a test case by either its ID or its index number. It also 
> implements the __len__ method to return the number of test cases stored.
> We're unable to store such a TestSuite object in a QVariant object.
> QVariant 
> seems to think that __len__ + __getitem__ equals "list", so the TestSuite

> object is converted into a list of TestCases. This means that nearly all 
> information stored in TestSuite is lost when storing it in a QVariant.

The current behaviour is to allow any sequence to be converted to a
QVariantList. When converting back it can only be to a Python list because
the fact that it was originally something else has been forgotten.

This behaviour is wrong - I should have changed it when I changed the
behaviour for Qt sub-classes in PyQt v4.5 (see the PyQt docs about
potential incompatibilities). I will fix it for PyQt v4.7.1.

As a workaround you could wrap your class as an attribute of another
(non-sequence) class when converting to QVariant and unwrap it when
converting back.


More information about the PyQt mailing list