Reputation: 141
Is there any way to hide certain item in ListView
?
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
ApplicationWindow {
title: qsTr("Hello World")
width: 640
height: 480
visible: true
ListView {
anchors.fill: parent
model: ListModel {
ListElement { color: "red"; visible: true}
ListElement { color: "green"; visible: false}
ListElement { color: "blue"; visible: true}
}
delegate: Rectangle {
width: parent.width
height: model.visible ? 30 : 0
color: model.color
visible: model.visible
enabled: model.visible
}
}
}
Solution above would be good if only ListView could ignore invisible Item
s' height
.
Setting height
to 0
manually is bad for performance so I need a better solution. Could you help me?
Upvotes: 13
Views: 9622
Reputation: 1296
This is a limitation of the the ListView that is still not resolved as of now. The solution is to either use another model for filtering (or adjust the current model) as suggested by other replies.
Or a better solution is to replace your ListView
with a combination of
ScrollView { Column { Repeater {} } }
So instead of:
ListView {
anchors.fill: parent
model: ...
delegate: ...
}
do:
ScrollView {
anchors.fill: parent
Column {
Repeater {
model: ...
delegate: ...
}
}
}
Upvotes: 1
Reputation: 27
import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Window 2.0
ApplicationWindow {
width: 640
height: 480
visible: true
property var model_items:[
{id: 0, _color: "red" , _visible: true},
{id: 1, _color: "blue" , _visible: false},
{id: 2, _color: "yellow" , _visible: true},
{id: 3, _color: "gray" , _visible: true},
]
ListView {
id: displayListView
anchors.fill: parent
model: myModel
delegate: Rectangle{
id: rec
width: 200
height: 200
color: _color
}
}
function createModel(){
myModel.clear()
for(var i=0;i<model_items.legth; i++)
if(model_items[i]._visible)
myModel.append(model_items[i])
}
ListModel {
id: myModel
}
Component.onCompleted: {
createModel()
}
}
Upvotes: 3
Reputation: 66
I hope this will solve the problem. For a beginner like me, solving this question has helped in understanding qml a bit more.
import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Window 2.0
ApplicationWindow {
width: 640
height: 480
visible: true
ListView {
id: displayListView
anchors.fill: parent
model: displayDelegateModel
}
ListModel {
id: myModel
ListElement { colo: "orange"; visible: true}
ListElement { colo: "red"; visible: false}
ListElement { colo: "white"; visible: true}
ListElement { colo: "black"; visible: false}
ListElement { colo: "green"; visible: true}
ListElement { colo: "yellow"; visible: false}
}
VisualDataModel {
id: displayDelegateModel
delegate: Rectangle {
width: parent.width
height: 30
color: colo
Text {
text: colo
anchors.centerIn: parent
font.bold: true
font.pixelSize: 20
}
}
model: myModel
groups: [
VisualDataGroup {
includeByDefault: false
name: "visible"
}
]
filterOnGroup: "visible"
Component.onCompleted: {
var rowCount = myModel.count;
items.remove(0,rowCount);
for( var i = 0;i < rowCount;i++ ) {
var entry = myModel.get(i);
if(entry.visible == true) {
items.insert(entry, "visible");
}
}
}
}
}
Upvotes: 3
Reputation: 69
You can use QSortFilterProxyModel to filter values:
m_filterModel->setSourceModel(m_model);
Upvotes: 2