Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QMetaType::Float not in QVariant::Type

Tags:

c++

qt

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?

like image 910
Boon Avatar asked Jul 08 '15 10:07

Boon


1 Answers

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 as QMetaType::Type. In particular, QVariant::UserType is returned here only if the value is equal or greater than QMetaType::User.

Note that return values in the ranges QVariant::Char through QVariant::RegExp and QVariant::Font through QVariant::Transform correspond to the values in the ranges QMetaType::QChar through QMetaType::QRegExp and QMetaType::QFont through QMetaType::QQuaternion.

Also note that the types void*, long, short, unsigned long, unsigned short, unsigned char, float, QObject*, and QWidget* are represented in QMetaType::Type but not in QVariant::Type, and they can be returned by this function. However, they are considered to be user defined types when tested against QVariant::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.

like image 100
Angew is no longer proud of SO Avatar answered Sep 21 '22 12:09

Angew is no longer proud of SO