Search code examples
c++qtunsignedsigned

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


Solution

  • 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