How to use own enum with Q_INVOKABLE and namespace



  • Hi,
    i will use this function from qml.

    Q_INVOKABLE bool startBackUp(SomeEnum i);
    

    The Enum is definied in *. h file:

    namespace MyNamespace
    {
        Q_NAMESPACE
      
        enum class SomeEnum {
            Foo,
            Bar
        };
        Q_ENUM_NS(SomeEnum)
    }
    
    I did the registration (```
    qmlRegisterUncreatableMetaObject(MyNamespace::staticMetaObject, ..."MyNamespace", 
    ```)
    

    Im write the ```

    "import myImort 1.0"
    

    Is use the function in qml:

    Model.startBackUp( MyNamespace.Foo) // works not!
    console.log("MyEnums: " + MyNamespace.Foo + " " + MyNamespace.bar  ) // works: MyEnums: 0 1
    

    I get the Message: ```
    Error: Unknown method parameter type: MyNamespace.Foo

    it works fine if i using an Integer datatype: ```

    Q_INVOKABLE bool startBackUp(int i); // works
    Model.startBackUp( MyNamespace.Foo) // works
    

  • Lifetime Qt Champion

    Hi,

    Do you also call Q_DECLARE_METATYPE and qRegisterMetaType ?



  • Yes i do.

    The console.log works fine. And also with Integer.

    Only here I get the errror message:

    Q_INVOKABLE bool startBackUp(SomeEnum i);
    ```  //Error: Unknown method parameter type: MyNamespace.Foo

Log in to reply
 

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