Important: Please read the Qt Code of Conduct -

QML C++ enum as property of QAbstractListModel

  • Hi I want to pass Enum as a property of object here what I got:

    enum TypeData

    My model passes structure so I got

    TypeData MyType;
    I also added it in rolenames, return QVariant.
    Now I want to do something like in delegate
    if(rolenamefortype === mymodel.TYPEA)
        //do stuff
    It is of course registered in main as model worked before this change

    It did not work.
    Printing out mymodel.TYPEA gives undefined. Printing rolenamefortype gives random numbers.

  • Moderators

    hi, sadly, theres more to it.

    I woudl suggest reading through this blockpost:

    It's more up to date than my other bookmarked links.

    most likly, from what I've read in your OP, you haven't registered the enum via qmlRegisterUncreatableType and or forgotten to import the registered type in the qml file where you want to use it.

  • I have enum inside class that is registeredtype. Do I need to declare enum outside and register it again?

  • Moderators

    @dmoviolin how did you register it? There are multiple ways

    and AFAIK Enums require qmlRegisterUncreatableType
    Registering c types with the qml type system

    registers a named C++ type that is not instantiable but should be identifiable as a type to the QML type system. This is useful if a type's enums or attached properties should be accessible from QML but the type itself should not be instantiable.

  • I registered it as RegiterType cause it is model derived from QAbstractListModel, enum is inside it. I would like to keep enum inside it rather than new header

    I can do if(rolenamefortype === 0) etc, cause it's int based like C style enum. However I would like to do if(rolenamefortype === model.TYPEA) cause of clarity

Log in to reply