Check QVariant type



  • Hi all,
    I initialize a QVariant this way:
    @
    float my_float;
    my_float = 12.2;
    QVariant my_variant;
    ...
    ...
    my_variant = QVariant(my_float);
    ...
    ...
    @

    now I need to be sure it's a float and not a double or an int.
    QVariant::Type doesn't have "float" but I actually check the type this way:
    @
    if(my_variant.type() == QMetaType::Float)
    {

    }
    @
    this seems to works but I get a warning while compiling:
    @
    warning: comparison between 'enum QVariant::Type' and 'enum QMetaType::Type'
    @

    Is this the right way to check the QVariant type?



  • After reading the sources:
    @
    QVariant(float f) { d.is_null = false; d.type = QMetaType::Float; d.data.f = f; }
    @
    Yes it is! Although you could use int QVariant::userType() if you feel more comfortable comparing to an int.



  • Note that in Qt 5, the distinction between QVariant::Type and QMetaType::Type will most likely go away.



  • [quote author="loladiro" date="1308738072"]After reading the sources:
    @
    QVariant(float f) { d.is_null = false; d.type = QMetaType::Float; d.data.f = f; }
    @
    Yes it is! Although you could use int QVariant::userType() if you feel more comfortable comparing to an int.[/quote]

    Thanks.

    [quote author="Andre" date="1308739392"]Note that in Qt 5, the distinction between QVariant::Type and QMetaType::Type will most likely go away.[/quote]

    That's a good news...


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.