QML call to ContextObject function with Enum Parameter



  • Hi everyone !

    I have problem with this simple situation.
    I use setContextObject to implement M-V-VM pattern.
    And in QML, I want to call to VM function with enum parameter
    but I get

    Error: Unknown method parameter type: AdsProvider::EnumAdsProvider
    

    This is my ViewModel Class

    class MainWindowViewModel : public DynamicObject
    {
        Q_OBJECT
    public:
        MainWindowViewModel(QObject *parent = 0);
    
        Q_INVOKABLE void showLoginDialog(AdsProvider::EnumAdsProvider engine);
    };
    

    This is the Enum declaration

    class AdsProvider : public QObject
        {
            Q_OBJECT
        public:
            //static const QString GOOGLE;
            //static const QString BING;
            static QString Username;
    
            enum EnumAdsProvider
            {
                GOOGLE,
                BING,
                FACEBOOK
            };
            Q_ENUMS(EnumAdsProvider)
    
            static void registerType()
            {
                qmlRegisterType<AdsProvider>("SystemConstants", 1, 0, "AdsProvider");
            }
        };
    

    I'm already register the custom Type & Enum like this

        AdsProvider::registerType();
        qmlRegisterType<DynamicObject>("core.dynamicobject", 1, 0, "DynamicObject");
        qmlRegisterType<MainWindowViewModel>("Presentation.ViewModel", 1, 0, "MainWindowViewModel");
    

    Here is the QML code call to VM function

    import Presentation.ViewModel 1.0
    import SystemConstants 1.0
    
    ApplicationWindow {
        id:root
        ...
        ToolButton{
            iconSource: "qrc:/login.png"
            onClicked:
            {
                showLoginDialog(AdsProvider.FACEBOOK);
            }
        }
        ...
    }
    

    If I change the the parameter type in VM's function to int, it worked !!!
    But I want to know exactly Enum type instead of int for any Enum parameters.

    Please Help !!! Thanks a lots !!!



  • @Dong
    I usually put Q_ENUMS macro right afte Q_OBJECT. Maybe that helps?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.