Qt Meta Type System
本文是对 Qt元系统之类型注册 的补充
Meta Type System支持下的异步的信号和槽连接
同步的信号和槽连接用不到类型信息,因为参数可以使用void指针来传递。但是,异步的信号和槽连接由于需要存储参数,所以需要类型信息:
static int *queuedConnectionTypes(const QArgumentType *argumentTypes, int argc)
{
QScopedArrayPointer<int> types(new int [argc + 1]);
for (int i = 0; i < argc; ++i) {
const QArgumentType &type = argumentTypes[i];
if (type.type())
types[i] = type.type();
else if (type.name().endsWith('*'))
types[i] = QMetaType::VoidStar;
else
// QMetaType::type返回类名对应的type id
/*
如果该类型没有注册,返回值为QMetaType::UnknownType
*/
types[i] = QMetaType::type(type.name());
if (!types[i]) {
qWarning("QObject::connect: Cannot queue arguments of type '%s'\n"
"(Make sure '%s' is registered using qRegisterMetaType().)",
type.name().constData(), type.name().constData());
return 0;
}
}
types[argc] = 0;
return types.take();
}
QMetaType::type静态函数返回类型名称对应的type id,所以:
知道了类型名称字符串就可以得到类型的type id,就能得到类的类型信息。
class Q_CORE_EXPORT QMetaType {
...
//从tape name获取type id
static int type(const char *typeName);
static int type(const QByteArray &typeName);
...
//从tape id获取type name
static const char *typeName(int type);