[Development] QVariant comparison in Qt6

Milian Wolff mail at milianw.de
Wed Sep 23 22:24:24 CEST 2020


On Mittwoch, 23. September 2020 21:12:52 CEST Thiago Macieira wrote:
> On Wednesday, 23 September 2020 06:59:16 PDT Milian Wolff wrote:
> > > 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.
> > 
> > This wouldn't be an issue if you sort by the integral values instead.
> 
> And how do you sort these variants:
>  1
>  "hello"
>  "!"
> 
> if you convert to integer, the string becomes 0, so it sorts first. If you
> convert the integer to string, "1" sorts before "hello". Note that
> QAbstractItemModelPrivate::isVariantLessThan returns false for BOTH orders:
> 
>  isVariantLessThan(1, "hello") == false
>  isVariantLessThan("hello", 1) == false

Right, sorry - I missed the part where you say "a few of the above are 
strings, the others are numbers". Mixed variant types within a single column 
is I believe a super rare case in models.

So from my side: as long as there's an equivalent to comparing two QVariants 
with matching types, e.g. via a public 
QAbstractItemModelPrivate::isVariantLessThan equivalent which can then be used 
in proxy models etc., then I'm fine.

Please just make sure there are no string conversions or similar required for 
that common case. That was all I wanted to point our (after misunderstanding 
your example above).

> > > > 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.
> > 
> > Agreed.
> 
> And fix the case above.
> 
> Which in my mind involves converting to string if the two types aren't equal
> or numeric.

For the mixed case, that indeed sounds like a good approach.

-- 
Milian Wolff
mail at milianw.de
http://milianw.de
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.qt-project.org/pipermail/development/attachments/20200923/eef88b70/attachment.sig>


More information about the Development mailing list