Access Data (Selected) in a ListView QML/Qt - qt

I'm accessing a XML page (online) via a XmlListModel and displaying the data using a ListView delegate (Row).
Once the Listview display the data (Xml file always only contain one "record"), i want a separate label to display one of the node's data (selected row)? Any guidance on this matter is appreciated.
My Listview:
ListView {
id: viweID
model: modelID
delegate: Row {
id:rowID
spacing: 10
Text {
id: fnameId
text: FName
}
Text {
id:lnameID
text: LName
}
}
Thank you

The ListView can be accessed either in terms of its items or by visiting its underlying model (see the documentation for further details).
It looks to me that you want to visit the former, for it already contains all the data that have to be shown on your separated panel.
To do that you can rely on the currentItem property and the signals onCurrentItemChanged/onCurrentIndexChanged.
It's a matter of exposing the desired properties from the delegate object to be able to read them and set them on the other panel.
See here for further details on how to expose those properties.

Related

how to add dynamically created object into a Container

I am trying to create ListModels dynamically, and insert these ListModels into a Container.
I get a variable number of ListModels, each with variable content, from a backend system. Based on the user's interaction with the GUI, one of those ListModels needs to be loaded into a known ListView. The client wants to avoid Qt/C++, so I am looking to solve this via QML.
How do I get a dynamically created object (in my case, a ListModel) into a Container? This code does not work:
property string strID;
property string strName;
Container {
id: functionListContainer;
}
// create ListModel
var newObject = Qt.createQmlObject("import QtQml.Models 2.14; ListModel { }", mainWindow);
...
// dynamically append elements into the ListModel
newObject.append({ deviceID: strID, deviceName: strName })
...
// add ListModel to Container
functionListContainer.addItem(newObject);
createQmlObject and append seem to work as intended. I get this error when running addItem code above:
"Could not convert argument 0 at"
...
"Passing incompatible arguments to C++ functions from JavaScript is dangerous and deprecated."
"This will throw a JavaScript TypeError in future releases of Qt!"
Any idea regarding how to get this to work? I know that addItem is expecting an Item, not an Object, but I do not know how to get this to work. I have tried replacing var with property Item newItem and property QtObject newObject [combined with addItem(newObject.item)], and all give (seemingly identical) errors. Is it a simple issue of casting the object into an Item? If so, what is the syntax that needs to be used?
Lastly, assuming I do have a Container with N ListModels, is it possible to refer to the container directly in the ListView? i.e.:
property int idx;
Container {
id: functionListContainer;
}
// add ListModels to container...
// use ListModel inside container
ListView {
...
model: functionListContainer.itemAt(idx);
...
}
I know that ListView is expecting a ListModel or something equivalent. But I am not sure how to connect the ListView with a Container containing ListModels, or if it is even possible.
If I were to summarize my problem, I am trying to have a ListView display different ListModels based on context, and I am trying to do this within a pure-QML framework as much as possible. Both of my questions above related to this. It would be helpful even only to clarify that this is not an option and that it is necessary to use another method like clearing and populating the ListModel (suggestions welcome).
You are trying to add a QObject (since ListModel is inherited from QAbstractListModel, which is in turn inherited from QObject) as a visual item using the addItem function. However, in QML a QObject (or QtObject) is regarded as an "storage element".
What you want to do it add the contentData of the Container:
Container {
id: functionListContainer
}
// create ListModel
var newObject = Qt.createQmlObject("import QtQml.Models 2.14; ListModel { }", mainWindow);
...
// dynamically append elements into the ListModel
newObject.append({ deviceID: strID, deviceName: strName })
...
// add ListModel to Container
functionListContainer.contentData.push(newObject)
The contentData property is the place where all children reside in the Container; QObject and QQuickItem (note that QQuickItem is inherited from QObject).
About referencing the lists, this also becomes easy when using the contentData property:
// create ListModel
var newObject = Qt.createQmlObject("import QtQml.Models 2.14; ListModel { objectName: \"the_list\" }", mainWindow);
...
// add ListModel to Container
functionListContainer.contentData.push(newObject)
console.log(functionListContainer.contentData[0])
will yield:
qml: QQmlListModel(0x55bcb7119720, "the_list")
Note that this is almost the same as using a Javascript array, the documentation of QQmlListProperty (which is what contentData is) states it is transparent.

How to detect if any of QML editable items in the root Item were edited?

I have an item of some type that consists of several QML editing controls:
Column {
id: inputItem
SpinBox {
}
TextInput {
}
ComboBox {
}
Button {
id: enableMeButton // this needs to be enabled if
text: "Apply" // anything was changed above
enabled: false
}
}
This item [Column as example] can be potentially inserted to some list view as "polymorphic" editing item so that we don't know beforehand what edit fields to handle (say we would want to move Apply outside). Or we want to use a common type with Apply to develop different editing delegates (which is the story here).
How can we detect that any of the data handled in this form was changed? Is there a generic way to do so or there is some trick to accomplish it?

Page loaded signal

We have a gui with multiple pages. On each page there are custom text components with a data coupling. When a page is shown/loaded the text components update their data binding using the Component.oncompleted and onVisibleChanged signals. To finalize this a call must be made to the communication module after each child has done its updates.
When pages are changed this can be done in the "onVisibleChanged" of the page. The problem is finding a point to do this when the page is created. The Component.onCompleted is performed before the one of the children.
Do you know any signal to use for this?
As a workaround I have an empty NumberAnimation with a Component.onCompleted call. This apperently runs after the children but feels like a hack.
Here goes the example code
DataText
DataText
{
property string address: ""
text: name + Model.getSingleItems().get(address).value //property binding here
onVisibleChanged: { Model.getSingleItems().setSubscribeStatus(address, visible);} //Stages the address for (un)subscribe
Component.onCompleted:{ Model.getSingleItems().setSubscribeStatus(address, visible);} //Stages the address for (un)subscribe
}
An example page
Rectangle
{
DataText
{
address: "datakey"
}
//This code must be executed after all setSubscribeStatus calls to
//finish the subscriptions.
Component.onCompleted:{ Model.getSingleItems().finialize();}
onVisibleChanged: { Model.getSingleItems().finialize();}
}
I'm looking for a way to subscribe to a batch of data adresses in the communication module. We can't keep all the data up to date due to limitations in our bandwith with the actual device.

How can I populate a TableView dynamically?

If I want to build out a TableView dynamically by loading data via AJAX call rather than by specifying list elements manually (like in the code below), how can that be done? I haven't found any examples anywhere yet.
ListModel {
id: dataModel
ListElement { title:"Image title"; credit:"some author"; source:"http:/..." }
ListElement { title:"Another title"; credit:"some author"; source:"http:/..." }
}
You need to fetch the data and then append it to your model.
The docs on ListModel are pretty straight forward and clear on how to add data to a model dynamically.
As to making the web request, here's an example on how to fetch data in QML using XMLHttpRequest.

Delegate reading from QML nested list not updating

So I have created a nested list in QML:
ListModel {
id: players
ListElement {
name: "Player 1"
counters: [
ListElement {
name: "Life"
count: 20
edit: false
} ,
ListElement {
name: "Poison"
count: 0
edit: false
}
]
}
//etc...
}
And, I have two views that read data from this: a ListView that is given the first-level ListModel, and a GridView that reads from the counters second-level model. I have given the GridView the counters list through the attached property in the delegate, like so:
GridView {
model: counters
}
Initially, it reads the data just fine. But my problem is that when the second-level model data is updated from outside the delegate, the GridView does not update with the new data. How do I get it to update?
When looking around, I have seen other people just create two separate models, but the problem is that I need the information in the nested list to be associated with the ListElement that contains it, so that really is not an option.
It turns out that you have to catch the signal and change the property manually. It's a little brute-force (because it will run given any change in the model, not just changes from outside the delegate), but it works.
In order to do this, you have to add a Connections element to the view delegate to catch the signal.
GridView {
id: view
model: counters
delegate: Item {
property int count: count
Connections {
target: view
onDataChanged: Item.count = count
}
}
}
It is important that the Connections element is a child of the delegate, because the view creates/removes delegates at its whim, and as such it is difficult to access the delegates created by the view.

Resources