[PyQt] List-like objects and QVariant
Sybren A. Stüvel
sybren at stuvel.eu
Wed Jan 20 11:48:44 GMT 2010
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.
I've included a minimal example that illustrates the problem:
---------------------------------------------------------------------------
import unittest
from PyQt4 import QtCore
class ClassUnderTest(object):
'''This is a class that looks like a list, but isn't.'''
def __init__(self, mylist=None):
self.mylist = mylist or []
def __getitem__(self, index):
return self.mylist[index]
def __len__(self):
return len(self.mylist)
def __eq__(self, other):
return self.__class__ == other.__class__ and \
self.mylist == other.mylist
class QVariantTest(unittest.TestCase):
def test_store_restore(self):
# Create a ClassUnderTest instance.
original_object = ClassUnderTest([1, 2, 3, 4])
# Save in a QVariant and retrieve it again.
variant = QtCore.QVariant(original_object)
pyobj = variant.toPyObject()
# Verify that they are the same.
self.assertEqual(pyobj, original_object)
---------------------------------------------------------------------------
I hope you can help me to fix this issue.
Regards,
--
Sybren A. Stüvel
sybren at stuvel.eu
http://stuvel.eu/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part.
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20100120/cbe7e565/attachment.pgp>
More information about the PyQt
mailing list