如何验证QVariant :: UserType类型的QVariant是预期类型? [英] How to verify QVariant of type QVariant::UserType is expected type?

查看:126
本文介绍了如何验证QVariant :: UserType类型的QVariant是预期类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写测试代码,该代码将自动遍历所有小部件的Q_PROPERTY,并且某些属性正在使用通过qRegisterMetaType注册的类型.如果我想将它们读/写到QVariant中,则在将它们存储到变量中时需要使用QVariant :: UserType.到目前为止一切顺利.

I'm writing testing code that will automatically iterate thru all Q_PROPERTY's of widgets and some properties are using types that are registered via qRegisterMetaType. If i want to read/write these into QVariant i need to use QVariant::UserType when storing them into variant. So far so good.

但是当我想测试这些属性的读写时,我还需要知道它们的类型.对于已经是标准qt类型的东西,我可以通过QVariant :: type()来实现,但是由于我有很多用户类型,这将如何实现?

But when i want to test reads and writes of these properties, i need to also know their type. For stuff that are already standard qt types, i can do this via QVariant::type() but as i have alot of usertypes, how would this be accomplished ?

从QVariant的api中,我发现了这一点:

From the api of QVariant, i spotted this:

bool QVariant::canConvert ( Type t ) const

但是我有点怀疑在枚举的情况下是否会导致错误的类型?

But i'm slightly doubtful if this will lead to wrong types in case of enums?

那么,验证QVariant中存储哪种类型的用户类型的万无一失的方法是什么?

So, what would be the foolproof way to verify what type of usertype is stored in QVariant ?

推荐答案

对于用户定义的类型,有 QVariant :: userType().它的工作方式类似于QVariant :: type(),但返回用户定义类型的类型ID整数,而QVariant :: type()始终返回QVariant :: UserType.

For user defined types there is QVariant::userType(). It works like QVariant::type() but returns the type id integer of the user defined type whereas QVariant::type() always return QVariant::UserType.

还有 QVariant :: typeName()返回类型的名称作为字符串.

There is also QVariant::typeName() which returns the name of the type as a string.

这可能取决于您如何设置QVariant.直接使用 QVariant :: QVariant(int类型,const void *复制).

It probably depends on how you set the QVariant. Directly using QVariant::QVariant(int type, const void * copy) is discouraged.

说我有三种类型:

class MyFirstType
{ 
    public:
        MyFirstType();
        MyFirstType(const MyFirstType &other);
        ~MyFirstType();

        MyFirstType(const QString &content);

        QString content() const;

    private:
        QString m_content;
};
Q_DECLARE_METATYPE(MyFirstType);

第三个没有Q_DECLARE_METATYPE

The third without Q_DECLARE_METATYPE

我将它们存储在QVariant中:

I store them in QVariant :

 QString content = "Test";

 MyFirstType first(content);

 MySecondType second(content);

 MyThirdType third(content);

 QVariant firstVariant;
 firstVariant.setValue(first);

 QVariant secondVariant = QVariant::fromValue(second);

 int myType = qRegisterMetaType<MyThirdType>("MyThirdType");

 QVariant thirdVariant(myType, &third); // Here the type isn't checked against the data passed

 qDebug() << "typeName for first :" << firstVariant.typeName();
 qDebug() << "UserType :" << firstVariant.userType();
 qDebug() << "Type : " << firstVariant.type();

 [...]

我明白了:

typeName for first : MyFirstType 
UserType : 256 
Type :  QVariant::UserType 

typeName for second : MySecondType 
UserType : 257 
Type :  QVariant::UserType 

typeName for third : MyThirdType 
UserType : 258 
Type :  QVariant::UserType 

这篇关于如何验证QVariant :: UserType类型的QVariant是预期类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆