QML ListView-更改除当前项目外的所有项目

拉巴列克桑达尔

我正在将Qt 5.7与结合使用,同时遵循Qt 4.8的教程(每个条目中没有可轻拂的内容)该方式存在的工作如下:QtQuick 2.0ListView

  1. 用户点击列表中的项目
  2. 显示项目的替代(详细)视图
  3. 用户必须Close在详细视图中单击按钮才能将进入状态重置为其默认的紧凑视图。

这会导致混乱,在某些情况下,如果用户单击所有项目,则所有项目都将以其完整视图显示。用户Close每次打开详细视图时都单击按钮并不是很方便。

我已经更改了当用户单击视图时要关闭的条目。我还试图防止这种混乱,并实现更多(杂项)流动行为:

  1. 用户点击列表中的项目
  2. 显示项目的替代视图
  3. 用户单击详细视图以将进入状态重置为其默认紧凑视图,或者
  4. 用户单击另一个条目,当前所有在详细视图中的条目都将重置为其紧凑视图

目前我通过循环我ListViewcontentItem.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

确实是这样吗?如果是,那么是否有可能以其他方式解决此问题?

来自Quivillic的Yoann Quenach

您为什么不使用自己的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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章