diff --git a/example/dynamiclist.jl b/example/dynamiclist.jl index fd18739..e63aaf7 100644 --- a/example/dynamiclist.jl +++ b/example/dynamiclist.jl @@ -1,13 +1,46 @@ using Base.Test using QML -qml_file = joinpath(dirname(@__FILE__), "qml", "dynamiclist.qml") +# Julia Fruit model item. Each field is automatically a role, by default +type Fruit + name::String + cost::Float64 + attributes::ListModel +end + +# Attributes must have a description and are nested model items +type Attribute + description::String +end +# Construct using attributes from an array of QVariantMap, as in the append call in QML +function Fruit(name, cost, attributes::Array) + return Fruit(name, cost, ListModel([Attribute(a["description"]) for a in attributes])) +end + +# Use a view, since no ApplicationWindow is provided in the QML qview = init_qquickview() -# Load QML after setting context properties, to avoid errors +# Our initial data +fruitlist = [ + Fruit("Apple", 2.45, ListModel([Attribute("Core"), Attribute("Deciduous")])), + Fruit("Banana", 1.95, ListModel([Attribute("Tropical"), Attribute("Seedless")])), + Fruit("Cumquat", 3.25, ListModel([Attribute("Citrus")])), + Fruit("Durian", 9.95, ListModel([Attribute("Tropical"), Attribute("Smelly")]))] + +# Set a context property with our listmodel +@qmlset qmlcontext().fruitModel = ListModel(fruitlist) + +# Load QML after setting context properties, to avoid errors on initialization +qml_file = joinpath(dirname(@__FILE__), "qml", "dynamiclist.qml") set_source(qview, qml_file) QML.show(qview) # Run the application exec() + +# Show that the Julia fruitlist was modified +println("Your fruits:") +for f in fruitlist + println(" $(f.name), \$$(f.cost)") +end diff --git a/example/qml/dynamiclist.qml b/example/qml/dynamiclist.qml index 19897c7..8e54bc7 100644 --- a/example/qml/dynamiclist.qml +++ b/example/qml/dynamiclist.qml @@ -48,39 +48,6 @@ Rectangle { width: 500; height: 400 color: "#343434" - // The model: - ListModel { - id: fruitModel - - ListElement { - name: "Apple"; cost: 2.45 - attributes: [ - ListElement { description: "Core" }, - ListElement { description: "Deciduous" } - ] - } - ListElement { - name: "Banana"; cost: 1.95 - attributes: [ - ListElement { description: "Tropical" }, - ListElement { description: "Seedless" } - ] - } - ListElement { - name: "Cumquat"; cost: 3.25 - attributes: [ - ListElement { description: "Citrus" } - ] - } - ListElement { - name: "Durian"; cost: 9.95 - attributes: [ - ListElement { description: "Tropical" }, - ListElement { description: "Smelly" } - ] - } - } - // The delegate for each fruit in the model: Component { id: listDelegate