QML.jl
QML.jl copied to clipboard
ListModel of 2D arrays
I understand the dynamic list example quite well to create dynamic lists on my own. But is it also possible the use an 2D array und use the QML TabView?
Hi,
Do you mean a TableView? If so, it is usable with a ListModel
, the so-called roles are the columns in that case. To convert the example to Julia, you could just create a composite type named Book
with fields title
and author
and pass an array of those to the ListModel constructor.
Yes, excuse me, I mean TableView.
type Book
title::String
author::String
end
Book1 = Book("A Masterpiece", "Gabriel")
Book2 = Book("Brilliance", "Jens")
Book3 = Book("Outstanding", "Frederik")
libmod = [Book1, Book2, Book3]
@qmlset qmlcontext().libraryModel = ListModel(libmod)
But how must my TableView look like? I tried this without success
TableView {
id: tableView
x: 25
y: 130
width: 750
height: 420
TableViewColumn {
role: title
title: "Title"
}
model: libraryModel
}
My goal is a table like this (generated at runtime with variable columns and rows)
nuclides ╲ years │ 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025
─────────────────┼─────────────────────────────────────────────────────────────────────
Co60 │ 5.46 5.47 5.49 5.49 5.39 4.79 4.27 3.82 3.41 3.05
Cs137 │ 9.29 10.62 11.13 11.31 11.26 11.01 10.78 10.55 10.33 10.12
Ni63 │ 78.9 77.0 75.38 74.48 74.6 75.63 76.46 77.25 77.98 78.64
Sr90 │ 0.35 0.91 2.0 2.72 2.75 2.57 2.49 2.38 2.28 2.19
Pu241 │ 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
Am241 │ 5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0
For the library example I needed role: "title"
, Doohhh.
The TabelView QML component is in fact not designed to display 2D data. I found a workaround at: http://stackoverflow.com/questions/27230818/qml-tableview-with-dynamic-number-of-columns
It is used in the example I just committed.
It should be possible to streamline this approach a bit by allowing to use a 2D array in the ListModel, I'll leave this issue open as a reminder to add that.
I could workaround to show the nuclides in a seprate ListView
left from the TableView
. I will try to reproduce your example. Thank you very much for your help.
This issue makes me realize that basing ListModel
around a Julia Array
is too restrictive. I will relax the requirements on the argument for the ListModel
constructor in the future, so any list-like type (providing indexing and size info and so on) is usable. That would then extend to the 2D array by providing an appropriate getindex/setindex to obtain or set a row.