0
votes

I am very new to QML and fairly seasoned with C++. I have been trying to go through some QML examples to try and learn it.

I was playing around with the TumblerColumn control (from examples) and basically trying to set the model to set the year. it goes something like:

TumblerColumn {
    id: yearColumn
    width: characterMetrics.width * 4 + tumbler.delegateTextMargins

    model: ListModel {
        Component.onCompleted: {
            for (var i = 2000; i < 2100; ++i) {
                append({value: i.toString()});
            }
        }
    }
    onCurrentIndexChanged: tumblerDayColumn.updateModel()
}

Now, I made a change like:

TumblerColumn {
    id: yearColumn
    width: characterMetrics.width * 4 + tumbler.delegateTextMargins

    property int startYear: 2000
    property int endYear: 3000

    model: ListModel {
        Component.onCompleted: {
            for (var i = startYear; i < endYear; ++i) {
                append({value: i.toString()});
            }
        }
    }
    onCurrentIndexChanged: tumblerDayColumn.updateModel()
}

This returns an error:

ReferenceError: startYear is not defined

How can I define these readonly constant properties for such a QML element.

2

2 Answers

3
votes

startYear and endYear aren't in that scope. Try this

TumblerColumn {
    id: yearColumn
    width: characterMetrics.width * 4 + tumbler.delegateTextMargins

    property int startYear: 2000
    property int endYear: 3000

    model: ListModel {
        Component.onCompleted: {
            for (var i = yearColumn.startYear; i < yearColumn.endYear; ++i) {
                append({value: i.toString()});
            }
        }
    }
    onCurrentIndexChanged: tumblerDayColumn.updateModel()
}
0
votes

Try make new QtObject in QML and add getters and setters

Item {
    QtObject {
        id : readOnlyProperties
        property int startYear : 2000
        property int endYear : 3000
    }

    function getStartYear() {return readOnlyProperties.startYear ;}
    function settartYear (_startYear ) { readOnlyProperties.startYear = _startYear; }

    // the same getter and setter for endYear
}