简体   繁体   English

QFlags和QVariant

[英]QFlags and QVariant

What I am trying to do is to simply store a QFlags in a QVariant . 我想要做的是简单地将QFlags存储在QVariant

The Flags definition: 旗帜定义:

class EnumObject : public QObject
{
    Q_OBJECT
public:
    enum DemoFlag {
        SomeFlag0 = 0x00,
        SomeFlag1 = 0x01,
        SomeFlag2 = 0x02
    };
    Q_DECLARE_FLAGS(DemoFlags, DemoFlag)
    Q_FLAG(DemoFlags)
};

Now all I do is to construct a qvariant using the QVariant::fromValue function: 现在我所做的就是使用QVariant::fromValue函数构造一个qvariant:

QVariant var = QVariant::fromValue<EnumObject::DemoFlags>(EnumObject::SomeFlag2);
qDebug() << var;

The debug output shows: 调试输出显示:

QVariant(EnumObject::DemoFlags, )

So, for whatever reason QVariant seems to be unable to store the flags? 那么,无论出于何种原因,QVariant似乎都无法存放旗帜? It does recognize the type, but seems unable to store the value. 它确实识别出类型,但似乎无法存储该值。 Did I miss something? 我错过了什么? If I register an enum instead, everything works fine. 如果我注册枚举,一切正常。

Note: I do know that I can store the value by casting it to an interger and back, but this is not possible for me, because the QVariant creation is part of a generic method. 注意:我确实知道我可以通过将值转换为整数和后面来存储该值,但这对我来说是不可能的,因为QVariant创建是泛型方法的一部分。

Actually it saves. 实际上它节省了。 But value is not showed for qDebug(). 但是qDebug()没有显示值。 It can be seen if value is extracted from QVariant: 可以看出是否从QVariant中提取了值:

EnumObject::DemoFlags val = var.value<EnumObject::DemoFlags>();

qDebug() << val;

Gives: 得到:

QFlags<EnumObject::DemoFlags>(SomeFlag2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM