QVariant / unsigned comparison

The QVariant Qt Framework type offers comparison operators <, <=, >, >= , but they work unexpectedly when unsigned integer arguments do not match:

 QVariant(-1) < QVariant(0u) yields false QVariant(0u) > QVariant(-1) yields false 

Does anyone know if this is a bug or is it intended? Do these statements always return false when socket / unsigned mismatch?

Btw, I am using Qt 5.6

+5
source share
1 answer

QVariant(-1) < QVariant(0u) will call the built-in comparators int and unsigned int . Basically, (int(-1) < uint(0)) == false (and it explains why here).

If you need other behavior, directly convert the values ​​before comparing using toInt () or similar methods: QVariant(-1).toInt() < QVariant(0u).toInt() == true

+3
source

Source: https://habr.com/ru/post/1247181/


All Articles