June 22, 2011

Luca Luca
Ant Farmer
496 posts

Check QVariant type

 

Hi all,
I initialize a QVariant this way:

  1. float my_float;
  2. my_float = 12.2;
  3. QVariant my_variant;
  4. ...
  5. ...
  6. my_variant = QVariant(my_float);
  7. ...
  8. ...

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:

  1. if(my_variant.type() == QMetaType::Float)
  2. {
  3.  
  4. }

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

Is this the right way to check the QVariant type?

3 replies

June 22, 2011

loladiro loladiro
Lab Rat
594 posts

After reading the sources:

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

June 22, 2011

Andre Andre
Robot Herder
6422 posts

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

June 22, 2011

Luca Luca
Ant Farmer
496 posts
loladiro wrote:
After reading the sources:
  1. 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.

Thanks.

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

That’s a good news…

 
  ‹‹ Associate      All Application Resizing ››

You must log in to post a reply. Not a member yet? Register here!