Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Check QVariant type
Qt 6.11 is out! See what's new in the release blog

Check QVariant type

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 15.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    luca
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • L Offline
      L Offline
      loladiro
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • L Offline
          L Offline
          luca
          wrote on last edited by
          #4

          [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...

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved