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

Q_ENUMS from one class still cannot be used as parameters of Q_INVOKABLE methods of another class

    XMLWordPrintable

Details

    • Bug
    • Resolution: Out of scope
    • P3: Somewhat important
    • None
    • 4.8.2
    • None
    • windows, linux, mac

    Description

      I know that the issue QTBUG-19741 is marked as fixed in 4.8 but I don't believe this issue has been fixed, at least not in all cases.

      I have 4.8.2 and the following setup:

      namespace one {
      namespace two {
      
      class MyEnum {
      Q_GADGET
      Q_ENUMS(Type)
      public:
      
        enum Type {
          Default = 1,
          NonDefault = 2
        };
      private:
         MyEnum() {};
         ~MyEnum(){};
      };
      
      class EnumUser : pubic QObject {
      Q_OBJECT
      public:
        Q_INVOKABLE void doStuff(one::two::MyEnum::Type enumValue);
      };
      
      QML_DECLARE_TYPE(one::two::EnumUser)
      // so that the enum can be stuffed into QVariant
      Q_DECLARE_METATYPE(one::two::MyEnum::Type)
      
      // somewhere during initialization types are registered using 
      // so that the enum can be used with queued connections
      qRegisterMetaType<one::two::MyEnum::Type>();
      // so that it can be addressed from Qml as "MyEnum."
      qmlRegisterUncreatableType<one::two::MyEnum>("one.two", 1, 0, "MyEnum", "Uncreatable type"); 
      
      qmlRegisterType<one::two::EnumUser>("one.two", 1, 0, "EnumUser); 
      
      
      // then in QML
      import one.two;
      
      EnumUser {
        onStuffHappened: doStuff(EnumUser.NonDefault);
      }
      
      }
      }
      

      In this configuration doStuff() always get invoked with 0 as the parameter, no matter what I pass to it. There are no errors or warnings on the command line when this happens.

      I know that MyEnum is registered correctly since properties I have properties of this type and they work as expected if I assign values to them or read them. But Q_INVOKABLE methods don't work.

      The only work around I found is to add a private method with the same name but with an int parameter in addition to the one with enum parameter: doStuff(int), and cast the int to the enum. Then QML calls this method with an int representing correct enum value.:

      // added to the EnumUser class
      private:
        Q_INVOKABLE void doStuff(int);
      
      void EnumUser::doStuff(int value) {
        doStuff(static_cast<one::two::MyEnum::Type>(value));
      }
      

      Attachments

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

        Activity

          People

            aalpert Alan Alpert
            fuzzypixel Dmitri Trembovetski
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes