Skip to content

Commit

Permalink
Update dynamiclist example to use Julia model
Browse files Browse the repository at this point in the history
  • Loading branch information
barche committed Nov 21, 2016
1 parent 1430d0a commit 5f3e645
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 35 deletions.
37 changes: 35 additions & 2 deletions example/dynamiclist.jl
Original file line number Diff line number Diff line change
@@ -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
33 changes: 0 additions & 33 deletions example/qml/dynamiclist.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5f3e645

Please sign in to comment.