[Kde-bindings] Custom data types in a QAbstractItemModel?

IBBoard ibboard at gmail.com
Thu Feb 11 18:54:03 UTC 2010

On 10/02/10 22:46, Arno Rehn wrote:
> On Wednesday 10 February 2010 22:59:55 Arno Rehn wrote:
>> On Wednesday 10 February 2010 21:38:12 IBBoard wrote:
>>> ...
>>> Looking at QAbstractItemModel, it seems to take QVariants as its data.
>>> Looking at QVariant there is a "UserType" as well as all of the
>>> Q-wrappers around standard data types. Unfortunately I've not been able
>>> how to use the "UserType" to wrap a user type yet. I've tried
>>> "QVariant.FromValue(object)", but that complains about not being able to
>>> infer generic parameters and if I try
>>> "QVariant.FromValue<QVariant>(object)" then it throws class cast
>>> exceptions.
>> Try QVariant.FromValue<type of your class>(object) instead.
>> example:
>> var variant = QVariant.FromValue<IntPtr>(new IntPtr());
>> works fine for me.
>> The question now is whether the value is correctly stored in the QVariant
>> for non-Qt-classes. The above IntPtr example doesn't seem to preserve the
>> value. That might be because IntPtr is a value-type though.
> Non-Qt types for QVariant were really broken. I fixed this in trunk and branch 
> 4.4 now. I also added a convenience overload QVariant.FromValue<T>(T value) so 
> the generic type can be inferred from the parameter.
> .NET framework types are now fully integrated into Qt's metatype system, so  a 
> System.IntPtr will seem like a real C++ class to QVariant.

Thanks - I'll check that out and hopefully that'll solve my problems. I
guess using the type of the object that you're passing in does make more
sense :) It would probably have helped if the class cast exceptions had
information about what the invalid cast was from and to! At least I'm
finding problems with the .Net bindings, even if I do keep asking
questions :)

More information about the Kde-bindings mailing list