[SOLVED] Structured constants in QML

Issue

Based on other posts (across sites), I have created a Constants.qml file which contains:

pragma Singleton
import QtQuick 2.6

QtObject {
    readonly property alias MyConst: myConst_

    QtObject
    {
        id: myConst_
        readonly property int Test: 1
        readonly property int Apple: 2
        readonly property int Dog: 4
    }
}

And elsewhere I am trying to reference these values like this:

var a = Constants.MyConst.Test

Which generates error:

TypeError: Cannot read property 'Test' of undefined    

Can someone explain how to make this work?

Solution

Try it with lower case:

pragma Singleton
import QtQuick 2.6

QtObject {
    readonly property alias myConst: myConst_

    QtObject
    {
        id: myConst_
        readonly property int test: 1
        readonly property int apple: 2
        readonly property int dog: 4
    }
}

Answered By – JarMan

Answer Checked By – Jay B. (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *