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

Q_PROPERTY with using-declared enum cannot be compared in QML

    XMLWordPrintable

Details

    Description

      When I use using-declared enum as a property type in another class then the property isn't compared correctly with the enum value in QML.

      enum class

      class GradesClass
      {
          Q_GADGET
      public:
          enum GradesEnum { A = 0, B, C };
          Q_ENUM(GradesEnum)
          ...
      };
      
      using Grade = GradesClass::GradesEnum;
      

      other class

      class Student : public QObject
      {
          Q_OBJECT
      public:
          ...
          Q_PROPERTY(Grade grade READ grade NOTIFY gradeChanged)
          Grade grade() { return m_grade; }
          ...
          Grade m_grade = Grade::B;
      };
      

      main.cpp

      qRegisterMetaType<Grade>("Grade");
      engine.rootContext()->setContextProperty("student", new Student());
      qmlRegisterUncreatableType<GradesClass>("App.Grades", 1, 0, "Grades", "...");
      

      main.qml

      import App.Grades 1.0
      
      Window {
          visible: true
          Component.onCompleted: {
              console.log("student.grade:", student.grade)
              console.log("Enum value:", Grades.B)
              console.log("Equals enum val:", (student.grade === Grades.B))
              console.log("Equals int:", (student.grade === 1))
          }
      }
      

      output

      qml: student.grade: B
      qml: Enum value: 1
      qml: Equals enum val: false
      qml: Equals int: false
      

      If NOT using using-declaration for enum Q_PROPERTY it works as expected:

      Q_PROPERTY(GradesClass::GradesEnum grade READ grade NOTIFY gradeChanged)
      

      output

      qml: student.grade: 1
      qml: Enum value: 1
      qml: Equals enum val: true
      qml: Equals int: true
      

      Attachments

        Activity

          People

            max.goldstein Maximilian Goldstein
            decon Daniel M
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: