在qml中使用c ++枚举

时间:2018-03-16 08:57:07

标签: qt qml qtquick2 qtabbar

我在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

1 个答案:

答案 0 :(得分:4)

您将类型注册为SettingManager,但将其用作BookManager。正确的代码是:

settingManager.bookKind = SettingManager.BookKind1;

您还应该使用Q_ENUM instead of Q_ENUMS