[Qt-interest] QVariant comparisons and custom types?
Dan Milburn
danmilburn at clara.co.uk
Mon Sep 13 13:00:19 CEST 2010
Daniel Price wrote:
> Hi List,
>
>
>
> I’m trying to use custom types (enums and structs) with QVariant. To cut
> a long story short, I’m storing custom data in the userData field of
> combox box rows. I have an interface that wraps the combox that must set
> the list to the index corresponding to the row with specific data. This
> interface is generic and can’t know about the specific custom data being
> passed in – only that it’s a variant that matches another variant
> already in the list.
>
>
>
> For reasons that escape me, QVariant’s equality operator doesn’t call
> the equality operator of custom types. Rather it compares the addresses
> of the data stored. This makes no sense given that all of Qt other
> container classes behave as expected. In what situations would the
> addresses be the same?!
>
>
>
> So I need to find a way around this. I’ve thought about templates or
> using other variant classes (boost any) but I still need to store them
> inside Qt’s interface as a QVariant.
>
>
>
> The interface needs to be able to compare the contents of the variants
> without having to be hard-coded with all the type cases.
>
>
>
> Any help appreciated!
Hi,
Yes, this is an issue I've run into before as well. It seems like it
should be possible to extend QMetaType to be able to handle equality in
a similar way as it deals with the QDataStream operators, but there's no
solution available at the moment that I know of.
The only thing I can think of would be to create your own functions for
testing QVariant equality which knows about your custom types and
manually casts the variant and compares equality.
Dan
More information about the Qt-interest-old
mailing list