[Development] QVariant comparison in Qt6

Lars Knoll lars.knoll at qt.io
Fri Sep 18 13:25:45 CEST 2020

> On 18 Sep 2020, at 11:23, Albert Astals Cid via Development <development at qt-project.org> wrote:
> El divendres, 18 de setembre de 2020, a les 11:00:23 CEST, André Somers va 
> escriure:
>> On 18-09-2020 09:12, Albert Astals Cid via Development wrote:
>>> El divendres, 18 de setembre de 2020, a les 2:54:53 CEST, Thiago Macieira
>>> va> 
>>> escriure:
>>>> On Thursday, 17 September 2020 16:15:47 PDT Bernhard Lindner wrote:
>>>>> Hi!
>>>>> There was a discussion about the decision to deprecate (remove?)
>>>>> QVariant
>>>>> comparison (<,>) in Qt6 completely.
>>>>> Has anything changed for Qt6 (especially regarding comparison of equal
>>>>> and/or convertible types)? Is the complete deprecation still the latest
>>>>> decision?
>>>> Yes, it's changed; no, it's not deprecation.
>>>> What's been removed is the conversion. Aside from the numeric types,
>>>> comparing two variants of different types will always result in false. If
>>>> you want to compare across types, convert one to the other's type or to a
>>>> common third type. You know what data you put in there and you should
>>>> know
>>>> what conversions can be lossy or not.
>>> But i don't what data someone else put it in a QVariant.
>>> The fact that QAbstractItemModelPrivate::isVariantLessThan exists should
>>> be
>>> proof enough that you can't expect my class to know what someone else put
>>> inside a QVariant, i mean that's basically the defining feature of
>>> QVariant "it can hold random things for you".
>>>  Is the official Qt position that we should all "copy&paste"
>>> QAbstractItemModelPrivate::isVariantLessThan into our item model classes
>>> that need sorting?
>> How often do you really have items of different QVariant types in a
>> single column and and role in a model?
> Probably not often, but that means adding new API to classes forcing users of 
> those classes to specify either a single type for each role/column (so the 
> comparison can be done internally in the class) or a comparer function for 
> each role/column and offloading the comparison back to the user, makes things a 
> bit annoying.
>> From what I understand, in Qt 6 you can still compare QVariants if they
>> contain the same type - be that a user type or not - for both equality
>> and lessThan provided the type in QVariant supports such comparisons. Right?
> There's no lessThan in Qt6 for QVariant, so no, you can't do that [unless you 
> unbox the data from the QVariant, but then you're not comparing QVariants 
> anymore :)].

I’m not really in favour of adding an operator<() to QVariant again, as it would need to provide some decent ordering, and that would probably cause conflicts with operator=() that does somewhat loose comparisons for numeric types.

But we could provide a compare() method similar to QMetaType. I don’t think we can provide total ordering here as some types stored in a QVariant do not provide comparison operators, but we could basically provide the same as QMetaType:

std::optional<int> QVariant::compare(const QVariant &other);

Would that be good enough?


> Cheers,
>  Albert
>> André
>> _______________________________________________
>> Development mailing list
>> Development at qt-project.org
>> https://lists.qt-project.org/listinfo/development
> -- 
> Albert Astals Cid | albert.astals.cid at kdab.com | Senior Software Engineer
> Klarälvdalens Datakonsult AB, a KDAB Group company
> Tel: Sweden (HQ) +46-563-540090, USA +1-866-777-KDAB(5322)
> KDAB - The Qt, C++ and OpenGL Experts
> _______________________________________________
> Development mailing list
> Development at qt-project.org
> https://lists.qt-project.org/listinfo/development

More information about the Development mailing list