Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Registering an Enumeration through a Plugin
Forum Updated to NodeBB v4.3 + New Features

Registering an Enumeration through a Plugin

Scheduled Pinned Locked Moved QML and Qt Quick
9 Posts 4 Posters 5.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gary_
    wrote on last edited by
    #1

    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)
    @

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mbrasser
      wrote on last edited by
      #2

      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")?

      1 Reply Last reply
      0
      • G Offline
        G Offline
        Gary_
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mbrasser
          wrote on last edited by
          #4

          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?

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Schneidi
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • T Offline
              T Offline
              thorbjorn
              wrote on last edited by
              #6

              @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)

              1 Reply Last reply
              0
              • S Offline
                S Offline
                Schneidi
                wrote on last edited by
                #7

                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 ?

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  Gary_
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    thorbjorn
                    wrote on last edited by
                    #9

                    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"?

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved