Discussion:
[Interest] Q_ENUM doesn't work for private enums
Tom Isaacson
2018-10-27 01:07:32 UTC
Permalink
I'm using Qt 5.6 in Visual Studio 2017. I've got a class that looks like:
class tClass : public QObject
{
Q_OBJECT

private:
enum class eMode
{
Mode1,
Mode2,
Mode3
};

When I add:
Q_ENUM(eMode)
after the enum declaration I get an error:
moc_tclass.cpp(): error C2248: 'tClass::eMode': cannot access private enum declared in class 'tClass'
tclass.h(348): note: see declaration of 'tClass:: eMode'
tclass.h(): note: see declaration of 'tClass

Is this a known limitation of Q_ENUM? Is it fixed in later versions of Qt?

Thanks.


Tom Isaacson
Henry Skoglund
2018-10-27 02:19:40 UTC
Permalink
Post by Tom Isaacson
class tClass : public QObject
{
Q_OBJECT
enum class eMode
{
Mode1,
Mode2,
Mode3
};
Q_ENUM(eMode)
moc_tclass.cpp(): error C2248: 'tClass::eMode': cannot access private enum declared in class 'tClass'
tclass.h(348): note: see declaration of 'tClass:: eMode'
tclass.h(): note: see declaration of 'tClass
Is this a known limitation of Q_ENUM? Is it fixed in later versions of Qt?
Hi, the Q_ENUM macro relies on static data structures declared in the
moc_tclass.cpp file (outside of the scope of the tclass) and these
declarations fail for private stuff. So changing that "private:" to
"public:" is the pretty much your only choice for Q_ENUM to work :-(

Rgrds Henry
Tom Isaacson
2018-10-27 02:26:26 UTC
Permalink
I thought that must be it, just surprised it's not mentioned in the documentation:
https://doc.qt.io/qt-5/qobject.html#Q_ENUM

Tom Isaacson


-----Original Message-----
From: Interest <interest-bounces+tom.isaacson=***@qt-project.org> On Behalf Of Henry Skoglund
Sent: Saturday, 27 October 2018 15:20
To: ***@qt-project.org
Subject: Re: [Interest] Q_ENUM doesn't work for private enums
Post by Tom Isaacson
class tClass : public QObject
{
Q_OBJECT
enum class eMode
{
Mode1,
Mode2,
Mode3
};
Q_ENUM(eMode)
moc_tclass.cpp(): error C2248: 'tClass::eMode': cannot access private enum declared in class 'tClass'
tclass.h(348): note: see declaration of 'tClass:: eMode'
tclass.h(): note: see declaration of 'tClass
Is this a known limitation of Q_ENUM? Is it fixed in later versions of Qt?
Hi, the Q_ENUM macro relies on static data structures declared in the moc_tclass.cpp file (outside of the scope of the tclass) and these declarations fail for private stuff. So changing that "private:" to "public:" is the pretty much your only choice for Q_ENUM to work :-(

Rgrds Henry

Loading...