Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QVariant signed/unsigned comparisons

The QVariant type of the Qt Framework offers comparison operators <, <=, >, >=, but they work unexpected on signed/unsigned integer arguments mismatch:

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

Does anybody know if this is a bug, or is this intended? Do this operators always return false on singed/unsigned mismatch?

Btw, I'm using Qt 5.6

like image 695
jonjonas68 Avatar asked Oct 19 '22 11:10

jonjonas68


1 Answers

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

If you want different behavior, convert values before comparison explicitly with toInt() or similar methods: QVariant(-1).toInt() < QVariant(0u).toInt() == true

like image 168
Andrei R. Avatar answered Nov 18 '22 03:11

Andrei R.