gpt4 book ai didi

qt - 拖放事件后如何在ListView中检索ListModel的新排序列表

转载 作者:行者123 更新时间:2023-12-04 13:21:29 24 4
gpt4 key购买 nike

我有这个简单的 qml 代码

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
import QtQml.Models 2.2

ApplicationWindow {
visible: true
width: 300
height: 120
title: qsTr("Hello World")
Rectangle {
anchors.fill: parent;

ListView{
id: timeline
anchors.fill: parent
orientation: ListView.Horizontal
model: visualModel
delegate: timelineDelegate

moveDisplaced: Transition {
NumberAnimation{
properties: "x,y"
duration: 200
}
}

DelegateModel {
id: visualModel
model: timelineModel
delegate: timelineDelegate
}

Component {
id: timelineDelegate


MouseArea {
id: dragArea

width: 100; height: 100

property bool held: false

drag.target: held ? content : undefined
drag.axis: Drag.XAxis

onPressAndHold: held = true
onReleased: {
held = false
var listOnModel = "{";
for(var i = 0; i < timelineModel.count; i++){
listOnModel += timelineModel.get(i).colore + ", "
}
console.log("List: " + listOnModel + "}");
}

Rectangle {
id: content

anchors { horizontalCenter: parent.horizontalCenter; verticalCenter: parent.verticalCenter }
width: 100
height: 100

color: colore
opacity: dragArea.held ? 0.8 : 1.0

Text{
anchors.verticalCenter: parent.verticalCenter
anchors.horizontalCenter: parent.horizontalCenter
text: index
font.pixelSize: 20
}

Drag.active: dragArea.held
Drag.source: dragArea
Drag.hotSpot.x: width / 2
Drag.hotSpot.y: height / 2

states: State{
when: dragArea.held
ParentChange { target: content; parent: timeline }
AnchorChanges {
target: content
anchors { horizontalCenter: undefined; verticalCenter: undefined }
}
}
}

DropArea {
anchors.fill: parent
onEntered: {
visualModel.items.move( drag.source.DelegateModel.itemsIndex, dragArea.DelegateModel.itemsIndex);
}

}
}
}

ListModel {
id: timelineModel
// @disable-check M16
ListElement { colore: "blue" }
// @disable-check M16
ListElement { colore: "orange" }
// @disable-check M16
ListElement { colore: "green" }
}
}
}
}

这里我们有一个简单的彩色可拖动矩形列表。在每个矩形的中心显示实际 index ,该组件在模型内部。

enter image description here

可以看到,在 drop 事件之后,每个 item 的 index 并没有改变,模型中 item 的顺序还是一样的。有没有办法在拖放事件发生后检索列表的新顺序?

最佳答案

您不重新订购 ListModel ,但 items您的 DelegateModel .
所以你需要改用这个代码:

onReleased: {
held = false
var listOnModel = "{";
for(var i = 0; i < visualModel.items.count; i++){
listOnModel += visualModel.items.get(i).model.colore + ", "
}
console.log("List: " + listOnModel + "}");
}

关于qt - 拖放事件后如何在ListView中检索ListModel的新排序列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40992852/

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