Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-35721

Exposing C++ enums to QML no longer working

    XMLWordPrintable

Details

    • eb3087e4b0b770200512925730c328a8bda7f3d7 (qtdeclarative)

    Description

      With the following class

      class ApplicationManager : public QObject
      {
          Q_OBJECT
          Q_ENUMS(Roles)
      
      public:
          ApplicationManager(QObject* parent = 0) {}
      
          enum Roles {
              RoleAppId = Qt::UserRole,
              RoleName,
              RoleComment,
              RoleIcon,
              RoleStage,
              RoleState,
              RoleFocused,
          };
      };
      

      Exposed to QML by:

      static QObject* applicationManagerSingleton(QQmlEngine* engine, QJSEngine* scriptEngine) {
          Q_UNUSED(engine);
          Q_UNUSED(scriptEngine);
      
          return new ApplicationManager;
      }
      
      class FooPlugin : public QQmlExtensionPlugin {
          Q_OBJECT
          Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface/1.0")
      
          virtual void registerTypes(const char* uri)
          {
              Q_ASSERT(QLatin1String(uri) == QLatin1String("Foo"));
      
              qmlRegisterSingletonType<ApplicationManager>(
                          uri, 0, 1, "ApplicationManager", applicationManagerSingleton);
          }
      };
      

      And used from QML this way:

      import Foo 0.1
      
      Item {
          Component.onCompleted: {
              console.log("ApplicationManager = " + ApplicationManager);
              console.log("ApplicationManager.RoleComment = " + ApplicationManager.RoleComment);
          }
      }
      

      You should get printed out on the terminal something like the following:

      ApplicationManager = ApplicationManager(0x147f6f0)
      ApplicationManager.RoleComment = 258
      

      But instead you get:

      ApplicationManager = ApplicationManager(0x147f6f0)
      ApplicationManager.RoleComment = undefined
      

      Meaning that the enumeration didn't make it to the QML side.
      That used works with Qt 5.0.2 and git tag v5.2.0 but no longer works with the current stable branch (5.2.1), as of Dec 18 2013.

      One hint is that it does work if you import the modue into a named namespace, like this:

      import Foo 0.1 as Bar
      
      Item {
          Component.onCompleted: {
              console.log("Bar.ApplicationManager = " + Bar.ApplicationManager);
              console.log("Bar.ApplicationManager.RoleComment = " + Bar.ApplicationManager.RoleComment);
          }
      }
      

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            shausman Simon Hausmann
            dandrader Daniel d'Andrada
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes