我在qml中使用了enum
class SettingManager : public QObject
{
Q_OBJECT
public:
enum BookKinds{
BookKind1=0,
BookKind2=1,
};
Q_ENUMS(BookKinds)
Q_PROPERTY(BookKinds bookKind READ bookKind WRITE setBookKind NOTIFY bookKindChanged)
explicit SettingManager(QObject *parent = nullptr);
void setBookKind(BookKinds dkob);
BookKinds bookKind();
signals:
void bookKindChanged();
};
在main.cpp
我注册了SettingManager
qmlRegisterType<SettingManager>("Test",1,0,"SettingManager");
我在qml文件中使用它
onCurrentIndexChanged:
{
if(tbarBookKindForDisplay.currentIndex==0)
{
settingManager.bookKind=BookManager.BookKind1;
}
else if(tbarBookKindForDisplay.currentIndex==1){
settingManager.bookKind=BookManager.BookKind2;
}
}
当CurrentIndex
TabBar
更改时发生以下错误:
错误:无法将[undefined]指定给int
答案 0 :(得分:4)
您将类型注册为SettingManager
,但将其用作BookManager
。正确的代码是:
settingManager.bookKind = SettingManager.BookKind1;
您还应该使用Q_ENUM
instead of Q_ENUMS
。