首页 > 解决方案 > 将 qml 注册的枚举作为参数发送到 c++ 方法

问题描述

我有这样的enum

class ScreenOrientation : public QObject
{
    Q_OBJECT

public:
    enum Orientation {
        Unspecified = -1,
        Landscape = 0,
        Portrait = 1
    };
    Q_ENUM(Orientation)
};

注册于

qmlRegisterUncreatableType<ScreenOrientation>("Foo", 1, 0, "ScreenOrientation", QStringLiteral("Not creatable as it is an enum type"));

还有我的 QML 代码:

backend.setOrientation(ScreenOrientation.Landscape)

当我的 C++ 方法有int作为参数时它正在工作

...
void setOrientation(int orientation);
...

但是,如果我使用ScreenOrientation::Orientation作为方法参数,我会得到“错误:未知方法参数类型:ScreenOrientation::Orientation ”。我可以以某种方式ScreenOrientation::Orientation用作方法参数吗?它看起来比只是漂亮得多int

标签: c++qtqml

解决方案


推荐阅读