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?
You could use this:
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:and
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:Here you can find all the types:
http://qt-project.org/doc/qt-5.1/qtcore/qmetatype.html#Type-enum