I have got an application that is working fine but it wasn't compiled with the warnings turned on. I'm trying to turn it back on and sorting them out but running out of ideas on how to fix this. I have :
QVariant someVar
QVariant::Type variantType = someVar.type();
switch (variantType) {
case QMetaType::QString:
doSomething1();
break;
case QMetaType::Float:
doSomething2();
break;
}
and got this warning/error:
error: case value ‘135’ not in enumerated type ‘QVariant::Type’ [-Werror=switch]
in the QMetaType::Float line. I checked up the QT docs and it listed QMetaType::Float as value 38. What could possibly have caused this?
The closest thing I can find is at https://github.com/qbittorrent/qBittorrent/issues/2510 which has the same error. Anyone encountered this before?
Qt plays somewhat dirty tricks witht these two enumerations (QMetaType::Type
and QVariant::Type
). Quoting 4.8.4 docs on QVariant::type()
:
Returns the storage type of the value stored in the variant. Although this function is declared as returning
QVariant::Type
, the return value should be interpreted asQMetaType::Type
. In particular,QVariant::UserType
is returned here only if the value is equal or greater thanQMetaType::User
.Note that return values in the ranges
QVariant::Char
throughQVariant::RegExp
andQVariant::Font
throughQVariant::Transform
correspond to the values in the rangesQMetaType::QChar
throughQMetaType::QRegExp
andQMetaType::QFont
throughQMetaType::QQuaternion
.Also note that the types
void*
,long
,short
,unsigned long
,unsigned short
,unsigned char
,float
,QObject*
, andQWidget*
are represented inQMetaType::Type
but not inQVariant::Type
, and they can be returned by this function. However, they are considered to be user defined types when tested againstQVariant::Type
.
In other words, the function QVariant::type()
returns values of QMetaType::Type
typed as QVariant::Type
, and those two enumerations share a lot (but not all) of their enumerators. This makes dealing with them in a strict type system difficult—they're basically wibbly-wobbly typey-wypey stuff.
In your case, notice that the enumerator QMetaType::Float
is among those which do not have a direct equivalent in QVariant::Type
.
I would say that the best way to silence the warning would be to change variantType
to QMetaType::Type
, potentially with a cast on initialisation and/or a comment referring to Qt docs if necessary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With