Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

expose mutiple enum from one class to QML



  • Hi All,

    Is there a way to expose multiple enums from one class to QML, and keep the namespaces.

    I have been at it with qmlRegisterUncreatableMetaObject, but with no success.
    Exposing one is no problem, but when I do both I get overlaps.
    Is there anyway to keep the Enum namespaces in QML?

    
    void Application : QObject
    {
        enum State {
            Home,
            MenuLogin,
            ...
        };
        Q_ENUMS(State)
    
        enum Action {
            Idle,
            MenuLogin,
            ....
        };
        Q_ENUM(Action)
    
        void setEngine(QQmlEngine *engine);
    
    }
    
    void Application::setEngine(QQmlEngine *engine)
    {
        ...
        context->setContextProperty("application", this);
    
        qmlRegisterUncreatableType<Application>(
                        "QmlApplication", 1, 1,
                        "APPLICATION", "Error, enum type");
    }
    

    Then in QML:

    ApplicationWindow {
        switch(applicationState) {
            case APPLICATION.Home:
                break;
            case APPLICATION.MenuLogin:
                break;
                    
        }
    }
    
    Is there anyway I can use APPLICATION.Action.MenuLogin and APPLICATION.State.MenuLogin?
    Thanks in advance for any pointers!
    
    

  • Moderators

    @javs
    IMHO this is rather bad design to have different enum valueswith the same name.
    Better add State/Action to the enum value names.


Log in to reply