My class has enum property, i wish to access this property using QObject*. When calling QVariant QObject::property ( const char * name ) const return value is empty QVariant of enum type.
Consider the following code:
/* Interface class */
class IFoo
{
Q_GADGET
public:
Q_ENUMS(ColorType)
typedef enum
{
COLOR_RED = 0,
COLOR_BLUE
} ColorType;
virtual QString Name(void) const = 0;
};
Q_DECLARE_METATYPE(IFoo::ColorType)
class Foo
: public IFoo
{
Q_OBJECT
public:
Foo(void)
{
qint32 typeId = qRegisterMetaType<IFoo::ColorType>("ColorType");
qRegisterMetaTypeStreamOperators<int>(IFoo::ColorType);
}
virtual QString Name(void) const { return _name; }
void SetColor(ColorType color) { _color = color; }
ColorType Color(void) const { return _color; }
QString ColorString(void) const { return _color == IFoo::COLOR_RED ? "Red" : "Blue"; }
Q_PROPERTY(IFoo::ColorType Color READ Color WRITE SetColor)
Q_PROPERTY(QString ColorString READ ColorString)
private:
ColorType _color;
QString _name;
};
int main (int argc, char **argv) {
QCoreApplication app(argc, argv);
Foo f;
f.SetColor(IFoo::COLOR_RED);
qDebug() << f.property("Color"); // Returns QVariant(IFoo::ColorType, )
qDebug() << f.property("ColorString"); // Returns QString(Red)
}
Why does property return empty QVariant value? String wrapper property works as it should.
It looks like that moc tool is unable to generate strings for respective values. IMO problem is
typedef. Try simpleenuminside of class:Or
typedefwithenumkeyword:I'm pretty sure that missing
enumkeyword confuses the moc tool.