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

Enum value treated as string when accessed within for...in loop

    XMLWordPrintable

Details

    Description

      Running the attached example prints:

      qml: 0 string
      qml: enum value cannot be evaluated
      qml: 1 string
      qml: enum value cannot be evaluated
      

      It should print:

      qml: 0 number
      qml: EnumA
      qml: 1 number
      qml: EnumB
      

      QML from the example:

      import QtQuick 2.15
      import QtQuick.Controls 2.15
      import example 1.0
      
      ApplicationWindow {
          width: 640
          height: 480
          visible: true
      
          Component.onCompleted: {
              // qml: enum value cannot be evaluated
              for (var enumValue in [
                       MyNamespace.EnumA,
                       MyNamespace.EnumB,
                   ]) {
                  print(enumValue, typeof enumValue)
                  switch (enumValue) {
                  case MyNamespace.EnumA:
                      console.log("EnumA")
                      break
                  case MyNamespace.EnumB:
                      console.log("EnumB")
                      break
                  default:
                      console.warn("enum value cannot be evaluated")
                      break
                  }
              }
      
              // qml: enum value cannot be evaluated
      //        var array = [
      //            MyNamespace.EnumA,
      //            MyNamespace.EnumB,
      //        ]
      //        for (var enumValue in array) {
      //            print(enumValue, typeof enumValue)
      //            switch (enumValue) {
      //            case MyNamespace.EnumA:
      //                console.log("EnumA")
      //                break
      //            case MyNamespace.EnumB:
      //                console.log("EnumB")
      //                break
      //            default:
      //                console.warn("enum value cannot be evaluated")
      //                break
      //            }
      //        }
      
              // qml: enum value cannot be evaluated
      //        var array = new Array()
      //        array.push(MyNamespace.EnumA)
      //        array.push(MyNamespace.EnumB)
      //        for (var enumValue in array) {
      //            print(enumValue, typeof enumValue)
      //            switch (enumValue) {
      //            case MyNamespace.EnumA:
      //                console.log("EnumA")
      //                break
      //            case MyNamespace.EnumB:
      //                console.log("EnumB")
      //                break
      //            default:
      //                console.warn("enum value cannot be evaluated")
      //                break
      //            }
      //        }
      
              // Works
      //        var enumValue = MyNamespace.EnumA
      //        print(enumValue, typeof enumValue)
      //        switch (enumValue) {
      //        case MyNamespace.EnumA:
      //            console.log("EnumA")
      //            break
      //        case MyNamespace.EnumB:
      //            console.log("EnumB")
      //            break
      //        default:
      //            console.warn("enum value cannot be evaluated")
      //            break
      //        }
          }
      }
      

      Doing

      print(array[0], typeof array[0])
      

      outside of the loop prints the expected value, and using a regular, index-based for loop works.

      Attachments

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

        Activity

          People

            qt.team.quick.subscriptions Qt Quick and Widgets Team
            mitch_curtis Mitch Curtis
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes