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)