[PyQt] QVariant TypeError on python class, but not on Qt class

Drew Vogel dvogel at intercarve.net
Wed Apr 4 14:17:03 BST 2007


Phil Thompson wrote:
> On Wednesday 04 April 2007 3:01 am, Drew Vogel wrote:
>   
>> Consider this listing:
>>
>>    1.
>>       from PyQt4 import QtCore
>>    2.
>>       from PyQt4 import QtGui
>>    3.
>>
>>    4.
>>
>>    5.
>>       # This works
>>    6.
>>       i = QtGui.QIcon()
>>    7.
>>       qi = QtCore.QVariant(i)
>>    8.
>>
>>    9.
>>
>>   10.
>>       # This throws TypeError: argument 1 of QVariant() has invalid type
>>   11.
>>       class C:
>>   12.
>>               def __init__(self, v):
>>   13.
>>                       self.value = v
>>   14.
>>
>>   15.
>>       x = C(100)
>>   16.
>>       qx = QtCore.QVariant(x)
>>
>>
>> Why does line 16 throw TypeError, but line 7 does not? I suspect that it
>> has something to do with "x" being a python class and "i" being a Qt
>> class, but I don't see anything in the QVariant documentation mentioning
>> a difference. Is this supposed to be supported?
>>     
>
> QVariant can only handle types it knows about (nothing to do with whether they 
> are Qt classes). It knows nothing about C.
>
> Phil
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
>   
Is there any way to tell QVariant about my C class? In C++ I did this 
with the Q_DECLARE_METATYPE macro and qRegisterMetaType(). The PyQt4 
QVariant documentation[1] links to the QMetaType documentation[2], but 
the link is dead. I couldn't find the equivilent through trial and error 
so I suspected that PyQt didn't need to register meta types because it 
used Python's introspection features.

[1] http://www.riverbankcomputing.com/Docs/PyQt4/html/qvariant.html
[2] http://www.riverbankcomputing.com/Docs/PyQt4/html/qmetatype.html




More information about the PyQt mailing list