qmlRegisterType与Q_ENUM和typedef enum一起使用



我想在我的QML文件中使用来自另一个头的enum。在用Q_ENUM包装它之后,我不能使它与qmlRegisterType:

一起工作
class Test: public QObject
{
     Q_OBJECT
     typedef foo::bar Foobar; // bar is the enum, contained in a namespace foo
     Q_ENUM(Foobar)
     public:
          static void declareQML()
          {
              qmlRegisterType<Foobar>("TestEnums", 1, 0, "Foobar");
          };
}

使用MSVC 2013,我得到以下错误:

C:Qt5.7msvc2013includeQtQmlqqml.h:244: error: C2838: 'staticMetaObject' : illegal qualified name in member declaration
see reference to function template instantiation 'int qmlRegisterType<Test::Foobar>(const char *,int,int,const char *)' being compiled

是因为枚举被包含在命名空间中吗?请注意,我不能修改包含命名空间和enum(由protobuf生成)的文件。

qmlRegisterType()将注册一个在QML中使用的类型,但那将是QObject派生类型,而不是enum。所以你实际上需要注册Test,枚举应该由Q_ENUM宏处理。

枚举必须是QObject派生类的一部分,这样Qt元系统才能处理它。我不认为它会对一些外部枚举工作。

如果枚举在一个常规类中,你可以将其设置为Q_GADGET以使moc处理它,否则你可以在Test的主体中复制原始枚举。

最后,枚举名称和值必须以大写字母开头,以便在QML中工作。

最新更新