4

I have a nested Item where I want to declare an enum. How can I access the enum from the parent Item? I dont want to declare the enum in the outer Item, because it belongs to the nested/inner Item. I tried several things and the Docs dont show nested examples either.

// main.qml
import QtQuick 2.15
import QtQuick.Window 2.15

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    MyItem {
        id: myItem
        enumValue: // ???
    }
}
// MyItem.qml
import QtQuick 2.15

Item {

    Component.onCompleted: console.log(nestedItem.MyEnum.First) // Cannot read property 'First' of undefined
    Component.onCompleted: console.log(MyItem.MyEnum.First) // Cannot read property 'First' of undefined
    Component.onCompleted: console.log(MyEnum.First) // MyEnum is not defined
    Component.onCompleted: console.log(MyItem.First) // undefined

    Item {
        id: nestedItem

        property int enumValue

        enum MyEnum {
            First,
            Second,
            Third
        }
    }
}

2
  • Would you ask how to use a C++ variable outside the { ... } scope it is declared in? This looks very similar. How about you describe the ultimate goal you are trying to achieve? On another note, do not overuse tags; are you working qt6 (as you should be) or are you still stuck on qt5? Commented Nov 13 at 10:19
  • What is my goal? I want to declare the enum to use it in my nested item, to set properties depending on the choosen enum-value. I am not oderusing tags, I tested it on qt5 and qt6, and I am not stuck on qt5, we use bigger applications written in qt5 which we cannot switch to qt6 from one day to another. Commented Nov 13 at 10:23

1 Answer 1

2

The documentation clearly states:

Only named QML types can hold enumerations usable in QML. Each enumeration must have a surrounding named type. QML Namespaces are also QML types and can hold enums.

Therefor, you cannot declare an enum in your nested type. What you can do, however, is move it to a separate file to create a new type you can use:

// NestedItem.qml
import QtQuick

Item {
  enum MyEnum {
    First,
    Second,
    Third
  }
}

You can then use and access your type elsewhere in your application:

Item {
  NestedItem {
      id: nestedItem
  }
  Component.onCompleted: console.log(MyEnum.First)
}

note that you also do not need to instantiate the type to use the enum. the following is perfectly okay:

Item {
  Component.onCompleted: console.log(NestedItem.MyEnum.First, NestedItem.Second) // Both acceptable
}

Creating a new QML type like this makes a lot of sense, if you plan to also instantiate the type. It helps creating connection between the type in use and the enum values. A good example for this is Text, which uses enum values for alignment:

Text {
  horizontalAlignment: Text.AlignHCenter
}

If you do not need any of the graphical features of Item, you may use QtObject as the base type that holds your enums. However, at that point, you should also consider the second part of the quote up top:

QML Namespaces are also QML types and can hold enums.

Creating your enum in C++ and making it available to QML will prevent any misuse of your type, if you have no need for instantiation but just want to have an enum available for use.

Sign up to request clarification or add additional context in comments.

2 Comments

Does NestedItem need to be Item, or could it be just QtObject? I'd imagine QtObject should work, but too lazy to test. Anyway, if QtObject does work, it would be better to use that instead of Item.
Yes, QtObject should work also. I chose Item since it seems like OP will also create an instance of the element. In which case it makes sense (similar to the enums of the Text type). I'll add a short section to my answer in a moment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.