Planned maintenance has been done but it did not solve the problem. So work will continue on this and a new time for trying updates will be announced asap.

QML C++ enum as property of QAbstractListModel



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

    enum TypeData
        {
            TYPEA,
            TYPEB,
            TYPEC
        };
        Q_ENUM(TypeData)
    

    My model passes structure so I got

    Struct{
    //OTHER STUFF
    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

    @dmoviolin
    hi, sadly, theres more to it.

    I woudl suggest reading through this blockpost:
    https://www.embeddeduse.com/2017/09/15/passing-enum-properties-between-c-and-qml/

    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

    Update:
    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