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

Arno Rehn arno at arnorehn.de
Wed Feb 10 22:46:50 UTC 2010


On Wednesday 10 February 2010 22:59:55 Arno Rehn wrote:
> On Wednesday 10 February 2010 21:38:12 IBBoard wrote:
> > Hi,
> > 
> > I'm back with another Qt#/Qyoto question - this time about the
> > Model/View code. I've tried reading the documentation, but while I can
> > see what looks like a promising section of the API I've not been able to
> > work out how to actually use it!
> > 
> > Basically, I'm used to WinForms (which lets you set a model of arbitrary
> > data objects) and GTK# (which also lets you add arbitrary models in a
> > data store that widgets use) in .Net/C# and I've used SWT in Java (which
> > doesn't have full models, but does let you call a "setData" function
> > with a string and an object, which gets a similar effect in most
> > situations).
> > 
> > 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.

-- 
Arno Rehn
arno at arnorehn.de



More information about the Kde-bindings mailing list