我正在将Qt 5.7与结合使用,同时遵循Qt 4.8的本教程(每个条目中没有可轻拂的内容)。该方式存在的工作如下:QtQuick 2.0
ListView
Close
在详细视图中单击按钮才能将进入状态重置为其默认的紧凑视图。这会导致混乱,在某些情况下,如果用户单击所有项目,则所有项目都将以其完整视图显示。用户Close
每次打开详细视图时都单击按钮并不是很方便。
我已经更改了当用户单击视图时要关闭的条目。我还试图防止这种混乱,并实现更多(杂项)流动行为:
目前我通过循环我ListView
的contentItem.children[loop_index]
和设置状态""
("Details"
=显示详细视图| ""
=显示紧凑视图)。由于ListView
工作方式(按需加载/卸载委托),这是非常不可靠的,当我尝试访问其他委托的状态时,经常会得到未定义的引用。MouseArea
我正在执行以下操作的以下内容是每个委托的一部分:
// state is a QML `State` that is bound to the delegate (see below for the details on it)
MouseArea {
anchors.fill: background
onClicked: {
// Iterate through all other entries and close them
for (var entry = 0; entry < listView.count; ++entry) {
if(listView.contentItem.children[entry] !== gestureEntry) {
console.log("Hide other element");
listView.contentItem.children[entry].state = ""; // IT FAILS HERE (SOMETIMES)
}
}
// Change view of current entry
if(gestureEntry.state === "Details") {
gestureEntry.state = "";
console.log("Hiding details")
}
else {
gestureEntry.state = "Details";
console.log("Showing details");
}
}
}
与state
被委托的状态:
states: State {
name: "Details"
PropertyChanges { target: background; color: "white" }
PropertyChanges { target: gestureImage; width: 130; height: 130 } // Make picture bigger
PropertyChanges { target: gestureEntry; detailsOpacity: 1; x: 0; y: 0 } // Make details visible
PropertyChanges { target: gestureEntry; height: listView.height } // Fill the entire list area with the detailed view
}
我认为state
信息可以存储在ListModel
自身内部,从而可以遍历模型的内容(与委托的内容不同,这些内容始终存在),但是我不知道如何自动更新列表(以及当前可见/不可见委托)。从到目前为止的发现来看,由于ListView
无法主动监视其行为,似乎不可能做到这一点ListModel
。
确实是这样吗?如果是,那么是否有可能以其他方式解决此问题?
您为什么不使用自己的currentIndex
财产ListView
?只需像这样修改您的代表:
Item {
id: gestureEntry
...
state: ListView.isCurrentItem?"Details":""
...
MouseArea {
anchors.fill: background
onClicked: {
if(listView.currentIndex == index)
listView.currentIndex = -1
else
listView.currentIndex = index
}
}
}
编辑:以上解决方案的唯一问题是-加载后-中的一个条目ListView
已预先选择,这会自动触发该条目的详细视图。为了避免将以下内容添加到listView
:
Component.onCompleted: {
listView.currentIndex = -1;
}
这样可以确保不会预选任何条目。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句