我的問題是指:的QVariant USERTYPE()ID
How to verify QVariant of type QVariant::UserType is expected type?
具體來說,如果
struct MyType {
....
};
Q_DECLARE_METATYPE(MyType);
QVariant v(QVariant::fromValue(MyType());
有沒有辦法找出v.userType()
將在編譯時返回?
我的問題是指:的QVariant USERTYPE()ID
How to verify QVariant of type QVariant::UserType is expected type?
具體來說,如果
struct MyType {
....
};
Q_DECLARE_METATYPE(MyType);
QVariant v(QVariant::fromValue(MyType());
有沒有辦法找出v.userType()
將在編譯時返回?
編譯時無法找到它,因爲直到運行時才確定它。你可以用qMetaTypeId<MyType>()
得到它。
從我試過的,你所聲明的userType()
索引將從256開始,然後增加1。
因此,如果您使用Q_DECLARE_METATYPE(someType);
,此類型將返回256
在userType()
調用。如果您接着執行Q_DECLARE_METATYPE(someOtherType);
,它將返回257
等等。
另外,如果你需要檢查它在運行時,你可以得到的值一次,然後用它來comparations:
int MyTypeID = QVariant::fromValue(MyType()).userType();
if(someObject.userType == MyTypeID)
{
//do stuff
}
你也可能想看看qRegisterMetaType()功能。