[Qt-interest] QVariant comparisons and custom types?

Dan Milburn danmilburn at clara.co.uk
Mon Sep 13 13:45:46 CEST 2010


Hi,

I am considering implementing the functionality myself and submitting it 
(once I've figure out how git works).  Will let you know how I get on.

Dan

> This is really crap. I can see no-reason why QVariant cannot call custom operators.
> 
> Is it worth raising as a bug/suggestion? I can imagine a lot of people running into this.
> 
> 
>> -----Original Message-----
>> From: qt-interest-bounces at trolltech.com [mailto:qt-interest-
>> bounces at trolltech.com] On Behalf Of Dan Milburn
>> Sent: 13 September 2010 12:00
>> To: qt-interest at trolltech.com
>> Subject: Re: [Qt-interest] QVariant comparisons and custom types?
>>
>> 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.



More information about the Qt-interest-old mailing list