gpt4 book ai didi

qt - 在 SwipeDelegate 上禁用滑动多个项目

转载 作者:行者123 更新时间:2023-12-04 13:07:55 25 4
gpt4 key购买 nike

默认情况下,SwipeDelegate 允许滑动多个项目,如下图所示:

enter image description here

如您所见,多次滑动项目一次打开,我一次只想要一个打开的项目,这意味着如果您打开项目#1,通过打开项目#2,项目#1 应该关闭。我怎样才能做到这一点?

带有 SwipeDelegate 的 ListView 的示例代码:

ListView {
id: listView
anchors.fill: parent

delegate: SwipeDelegate {
id: delegate

text: modelData
width: parent.width

swipe.right: Rectangle {
width: parent.width
height: parent.height

Label {
text: qsTr("SOME ACTION BUTTON")

padding: 20
anchors.fill: parent
}

}
}

model: ListModel {
id: listModel
ListElement { text: "Lorem ipsum dolor sit amet" }
ListElement { text: "Curabitur sit amet risus" }
ListElement { text: "Suspendisse vehicula nisi" }
ListElement { text: "Mauris imperdiet libero" }
ListElement { text: "Sed vitae dui aliquet augue" }
ListElement { text: "Praesent in elit eu nulla" }
ListElement { text: "Etiam vitae magna" }
ListElement { text: "Pellentesque eget elit euismod" }
ListElement { text: "Nulla at enim porta" }
ListElement { text: "Fusce tincidunt odio" }
ListElement { text: "Ut non ex a ligula molestie" }
ListElement { text: "Nam vitae justo scelerisque" }
ListElement { text: "Vestibulum pulvinar tellus" }
ListElement { text: "Quisque dignissim leo sed gravida" }
}


ScrollIndicator.vertical: ScrollIndicator { }
}

最佳答案

受 ButtonGroup 答案的启发,这是我不需要修改代表的解决方案:

SwipeDelegateGroup.qml

import QtQuick 2.9
import QtQuick.Controls 2.2

Item {
id: swipeGroup

property ListView listView: parent
QtObject {
id: d
property var delegates: swipeGroup.listView.contentItem.children
property var delegateCache: []

onDelegatesChanged: {
for (var i = 0; i < d.delegates.length; i++) {
var thisItem = d.delegates[i];
if (!thisItem.hasOwnProperty("swipe")) {
continue;
}
if (d.delegateCache.indexOf(thisItem) < 0) {
d.delegateCache.push(thisItem);

thisItem.Component.destruction.connect(function() {
d.delegateCache.splice(d.delegateCache.indexOf(thisItem), 1)
})

thisItem.swipe.opened.connect(function() {
for (var j = 0; j < d.delegates.length; j++) {
var otherItem = d.delegates[j];
if (thisItem === otherItem) {
continue;
}
if (!otherItem.hasOwnProperty("swipe")) {
continue;
}
otherItem.swipe.close();
}
})
}
}
}
}
}


然后像这样把它放到 ListView 中:
ListView {

SwipeDelegateGroup {}

delegate: SwipeDelegate {
...
}
}

关于qt - 在 SwipeDelegate 上禁用滑动多个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46399099/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com