QFlags and QVariant

1.3k Views Asked by At

What I am trying to do is to simply store a QFlags in a 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 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? 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.

1

There are 1 best solutions below

0
On

Actually it saves. But value is not showed for qDebug(). It can be seen if value is extracted from QVariant:

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

qDebug() << val;

Gives:

QFlags<EnumObject::DemoFlags>(SomeFlag2)