[Development] QVariant comparison in Qt6

Thiago Macieira thiago.macieira at intel.com
Fri Sep 18 17:06:20 CEST 2020


On Friday, 18 September 2020 01:59:50 PDT Albert Astals Cid via Development 
wrote:
> We have a few generic item models and proxy models that implement sorting,
> they did so by using operator< of QVariant.
> 
> I want a way to be able to do the same in Qt6 in a way that ideally doesn't
> involve having to copy the QAbstractItemModelPrivate::isVariantLessThan
> code.

Perform an unconditional conversion to string and sort that.

In a model, where you're likely to *display* the element, this is probably the 
right thing to do, as you don't want to explain to your users why your list 
shows items like

1
10
11
2
3
21

A few of the above are strings and the others are numbers. It should always 
lexicographically sort or use numeric/natural sorting.
 
> Maybe making QAbstractItemModelPrivate::isVariantLessThan public and clearly
> defining what it was would be good enough (i mean if it is good enough for
> QAbstractItemModel it could be enough for most of the other
> implementations).

That's a good idea.

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel DPG Cloud Engineering





More information about the Development mailing list