Qt: typeid alternative

c++, qt, qtcore, typeinfo

Solution

You could use this:

`const char * QVariant::typeName () const`

Returns the name of the type stored in the variant. The returned strings describe the C++ datatype used to store the data: for example, "QFont", "QString", or "QVariantList". An Invalid variant returns 0.

This will work for POD and registered built-in Qt types. You would need to use the following method for register your custom type though.

`int qRegisterMetaType(const char * typeName)`

One another thing you could try, although it is somewhat superfluous to `QVariant` is to work with Object's QMetaObject in the following way:

`const char * QMetaObject::className() const`

Returns the class name.

and

`const QMetaObject * QObject::metaObject() const [virtual]`

Returns a pointer to the meta-object of this object.

A meta-object contains information about a class that inherits QObject, e.g. class name, superclass name, properties, signals and slots. Every QObject subclass that contains the Q_OBJECT macro will have a meta-object.

The meta-object information is required by the signal/slot connection mechanism and the property system. The inherits() function also makes use of the meta-object.

If you have no pointer to an actual object instance but still want to access the meta-object of a class, you can use staticMetaObject.

Needless to say, this will only work for QObjects, so not for QString, etc. You would need to create QObject subclasses.

There is also some `QMetaType` that you can use for creation, but this is a bit different, so I am just mentioning here to be complete:

`int QMetaType::type(const char * typeName) [static]`

Returns a handle to the type called typeName, or QMetaType::UnknownType if there is no such type.

Here you can find all the types:

http://qt-project.org/doc/qt-5.1/qtcore/qmetatype.html#Type-enum

Problem

I'm wondering if Qt provides an alternative to typeid to recognize variable types and get their name in a human-readable format. My specific problem is the following: ``` struct gArgument{ QString type; void* arg; }; void gargConverter(gArgument* oArg, T data){ oArg->type = typeid(data).name(); oArg->arg = static_cast<void*> (&data); } ``` the idea would be that of generalizing a variable to use as a input to functions. As a side node tyeinfo seems not to be working correctly on my system (I'm using MinGW on Windows 7), if I try: ``` int i; std::cout << typeid(i).name() << std::endl; QString s; std::cout << typeid(s).name() << std::endl; double d; std::cout << typeid(d).name() << std::endl; float f; std::cout << typeid(f).name() << std::endl; ``` I get ``` i 7QString d f ``` Any suggestion?

Original source