Unsolved ContentItem of ListView
-
I have ListView with some items, but contentItem of this ListView give me only a subset of these items and it is not in order.
This is an example
import QtQuick 2.6 import QtQuick.Window 2.2 import QtQuick.Controls 1.5 Window { visible: true width: 200 height: 100 property var listModel: ListModel { ListElement { text: "1" } ListElement { text: "2" } ListElement { text: "3" } ListElement { text: "4" } ListElement { text: "5" } ListElement { text: "6" } ListElement { text: "7" } ListElement { text: "8" } ListElement { text: "9" } ListElement { text: "10" } ListElement { text: "11" } ListElement { text: "12" } ListElement { text: "13" } ListElement { text: "14" } ListElement { text: "15" } ListElement { text: "16" } ListElement { text: "17" } ListElement { text: "18" } ListElement { text: "19" } ListElement { text: "20" } ListElement { text: "21" } ListElement { text: "22" } ListElement { text: "23" } ListElement { text: "24" } ListElement { text: "25" } ListElement { text: "26" } ListElement { text: "27" } ListElement { text: "28" } ListElement { text: "29" } ListElement { text: "30" } } ListView{ id:listView width: 100 height: 100 model: listModel delegate: Rectangle { id: delegateItem width: parent.width; height: 100 Text { id: itexItem font.pixelSize: 40 text: model.text } } } Button { id: button x: 100 y: 0 text: qsTr("Button") onPressedChanged: { console.log(listView.contentItem.children.length) for(var i=0; i< listView.contentItem.children.length; i++) { if(listView.contentItem.children[i].children[0]) console.log(listView.contentItem.children[i].children[0].text) } } } }
Please help me
-
try:
console.log(listView.model.count) for(var i=0; i< listView.model.count; i++) { console.log(listView.model.get(i).text) }
-
Thank you but my problem is different, because I have some logic in the delegate.
See this exampleText {
id: itexItem
font.pixelSize: 40
text: model.text === "10"? "Test" : model.text
}So I can't use model, but I should ListView the text of delegate
-
Hi @alemio , your code is working fine for me
ListView{ id:listView width: 100 height: 100 model: listModel delegate: Rectangle { id: delegateItem width: parent.width; height: 100 Text { id: itexItem font.pixelSize: 40 text: model.text === "10"? "Test" : model.text } } }
Sample Output:-
-
@alemio said in ContentItem of ListView:
because I have some logic in the delegate.
The delegate is just for painting it should not contain data logic. You should move that logic in a proxy model
-
This post is deleted! -
Thank you, but so the answer is that I can't use the contentItem to see all items in the ListView, right?
are all items added/deleteded dinamically to the contentItem?
-
@alemio said in ContentItem of ListView:
are all items added/deleteded dinamically to the contentItem?
Yes. ListView destroys items out of viewport.
-
Ok, I'll explain my problem better, maybe it will be easier.
I would like to export a ListView to Excel. In my real project I have a a model and a proxymodel but it has more fields than the ListView and the order is different. If the way is use proxymodel, how can I do that? -
@alemio said in ContentItem of ListView:
In my real project I have a a model and a proxymodel
@alemio said in ContentItem of ListView:
how can I do that?
As mentioned, you should move the logic you currently have in the delegate to the proxy model and use that to save.
Is your model a QML model or a C++(QAbstractItemModel
) one? -
The model is a c++ model QAbstractItemModel. I have a unique model for each table in the database (autogenerated from orm). I have also a proxymodel for these c++ model
-
My proxy model is derived from SortFilterProxyModel
@VRonin Tell me please if I have a colored row or different font, how can I export that in excel?
-
You can use this library to create the excel file with custom formatting.
-
@VRonin I've already taken this library.
I just need to understand how put some logic in proxy model as you told me.
I saw DelegateModel component, but I don't find example how can I extract data in c++ (I think at the end it is the same problem like listview delegate) -
@alemio said in ContentItem of ListView:
I just need to understand how put some logic in proxy model
It's not really clear what exactly you are doing but I suspect you'd want to override the
data()
method of the proxy to convert some value in the base model to a different value in the proxybut I don't find example how can I extract data in c++
Check out
JsonModelSerialiserPrivate::toJsonObject
of this file for an example. this extracts the data and saves it in a JSON object, you basically have to do the same but save it in an excel file