在QML中使用枚举分配或更改int属性

ess*_*eev 5 enums qt qml qtquick2

考虑这个简单的枚举类:

#include <QObject>
class BookTypes : public QObject
{
    Q_GADGET
    Q_ENUMS(AllBooksType)    

public:

    enum AllBooksType{
        eMagazine,
        eReference,
        eTextBook,
        eThesis
    };

signals:

public slots:

};
Run Code Online (Sandbox Code Playgroud)

输入注册 main()

qmlRegisterUncreatableType<BookTypes>("trial", 1, 0, "BookTypes", 
"Don't create qml instance for BookTypes");
Run Code Online (Sandbox Code Playgroud)

这是QML示例:

Rectangle {
        id: rect
        x: 100; y: 100
        width: 100
        height: 70
        color: "PowderBlue"
        border.color: "RoyalBlue"
        border.width: 1
        radius: 3

        MouseArea{
            x: 0; y: 0
            height: parent.height
            width: parent.width
            property int bt: BookTypes.eTextBook //perfect. now bt is 2
            onClicked: {
                console.debug("old book type:- ")
                console.debug(bt) //prints 2
                console.debug("selected book type:- ")
                bt = BookTypes.eReference //gives error - why ?
                console.debug(BookTypes.eReference) //prints 'undefined'
                console.debug(bt)
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

这意味着枚举被正确暴露,因为它已bt成功初始化

property int bt: BookTypes.eTextBook
Run Code Online (Sandbox Code Playgroud)

我不明白的是:当我尝试替换bt处理程序的值时,为什么它不可访问:

bt = BookTypes.eReference //gives error - why ?
Run Code Online (Sandbox Code Playgroud)

我如何传递这样一个enum方法的参数Q_INVOKABLE,例如:

console.debug(BookTypes.eReference) //prints 'undefined'
SomeObj.someCPPMethod(BookTypes.eReference) // sends 'undefined' and defaults to 0
Run Code Online (Sandbox Code Playgroud)

cma*_*t85 5

取自文档:

注意:枚举值的名称必须以大写字母开头才能从QML访问.

这提出了一个问题:为什么它在属性绑定中起作用?我不知道,可能是一个Qt bug.