Registering an Enumeration through a Plugin



  • XmlListModel has the ability to access it's enumeration values for Status in QML (Qt Quick).

    @
    if (status == XmlListModel.Ready) {
    // do something
    }
    @

    I would like my class to expose it's enumerated values as well. The following QML does not work:

    @
    if (status == MyModel.Ready) { // ERROR (ReferenceError: Can't find variable: MyModel)
    // do something
    }

    if (status == XmlListModel.Ready) { // SUCCESS (My enum values match XmlListModel's enums)
    // do something
    }

    if (status == 1) { // SUCCESS (Just making sure my plugin works and it isn't a red herring)
    // do something
    }
    @

    I get the following error: ReferenceError: Can't find variable: MyModel.

    MyModel.h

    @
    class MyModel : public QAbstractListModel
    {
    Q_Object
    Q_PROPERTY(Status status READ getStatus NOTIFY statusChanged);

    public:
    Q_ENUMS(Status)
    enum Status { Null, Ready, Loading, Error };
    Status getStatus() const;

    private:
    Status _status;

    signals:
    void statusChanged(MyModel::Status);

    ...

    }
    @

    MyPlugin.cpp

    @
    void Plugin::registerTypes(const char *uri)
    {
    qmlRegisterType<MyModel>(uri, 1, 0, "MyModel");
    }

    Q_EXPORT_PLUGIN2(MyPlugin, MyPugin)
    @



  • This sounds like a bug. Are you using an import statement for your plugin (e.g. "import MyPlugin 1.0" or "import MyPlugin 1.0 as MyPlugin")?



  • I am not using import statement. I am using qmldir and it resides in the same directory as the main qml file.



  • Okay. This seems to work fine in the non-plugin case, and I suspect it would work correctly for plugins with an explicit import statement as well. Could you please add a bug report for this using the "tracker":http://bugreports.qt.nokia.com?



  • I have a problem similar to this topic except the use of the plugin.

    I build an qml interface to share signals and information between my c++ and qml layer.

    It works fine but I can't provide my enums to qml. I already saw an example to this topic but I miss some informations.

    Could you show me an Example of how to providing an enum to qml ?

    Thanks



  • @Schneidi

    Did you try the code posted by Gary_? That is the correct way to expose an enum to QML as far as I know, which has worked for me both with and without using a plugin.

    (though his Q_OBJECT macro is wrongly written as Q_Object)



  • Hi Thorbjørn

    sorry had some other issues to solve.
    I already tried it this way but I don't know how to implement the READ method
    in this case the

    Status getStatus() const;

    Do you have a complete example for this ?



  • Sorry for not updating till now. I was able to resolve this issue.

    First, I had to change the registration function to:

    @
    void Plugin::registerTypes(const char *uri)
    {
    qmlRegisterType<MyModel>("MyPlugin", 1, 0, "MyModel");
    }

    Q_EXPORT_PLUGIN2(MyPlugin, MyPugin)
    @

    Then included:

    @
    import MyPlugin 1.0
    @

    Without any additional changes, the Enumeration was visible to QML.



  • Hmm, you shouldn't do that change to qmlRegisterType though, and it's strange that it would fix your issue. Did you have a look at the value of "uri"?


Log in to reply
 

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