[Interest] Qt support for strongly-typed enums

Tom Isaacson Tom.Isaacson at navico.com
Sat Nov 21 07:09:06 CET 2015


>Strongly-typed enums need to be used as a completely separate type. You need to declare them as metatypes, load them into a QVariant using >QVariant::fromValue and retrieve using qvariant_cast<EnumType>().

When you say completely separate do you mean the enum can't be part of a class? The code I'm updating looks like:
class Foo : public QObject
{
	Q_OBJECT

public:
	enum eFoo
	{
		Foo1,
		Foo2
	};
};

I've updated it to look like:
class Foo : public QObject
{
	Q_OBJECT

public:
	enum class eFoo
	{
		Foo1,
		Foo2
	};
};
Q_DECLARE_METATYPE(Foo::eFoo)

That compiles fine, but this code:
	eFoo foo = Foo::eFoo::Foo1;
	QVariantMap map;
	map.insert("foo", QVariant::fromValue(foo));
fails to compile with the error "Type is not registered, please use the Q_DECLARE_METATYPE macro to make it known to Qt's meta-object system".

Is this just not possible?

Thanks.






More information about the Interest mailing list