如何验证 QVariant::UserType 类型的 QVariant 是预期类型?

发布于 2024-09-08 07:45:02 字数 417 浏览 3 评论 0原文

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

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

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

bool QVariant::canConvert ( Type t ) const

但我有点怀疑这是否会导致枚举的错误类型?

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

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.

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 ?

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

虫児飞 2024-09-15 07:45:02

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

还有 QVariant::typeName() 返回的名称输入为字符串。

编辑:

这可能取决于您如何设置 QVariant。直接使用 QVariant::QVariant(int type, const void * copy) 不鼓励。

假设我有这样的三种类型:

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

我将它们存储在 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 

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.

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

EDIT :

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

Say I have three types like this :

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

        MyFirstType(const QString &content);

        QString content() const;

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

The third without Q_DECLARE_METATYPE

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();

 [...]

I get :

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 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文