QML中含DelegateModelGroups的DelegateModel拖拽功能异常求助
QML GridView拖拽元素异常求助
参考《Moving item in QML GridView》的拖拽实现代码,在GridView中拖拽图标时出现以下错误:
file:///home/christoforos/Videos/launchpadPlasmaMod/contents/ui/AllApps.qml:151:11: QML MyDelegateModel (parent or ancestor of DelegateModelGroup): move: from index out of rangefile:///home/christoforos/Videos/launchpadPlasmaMod/contents/ui/AllApps.qml:260: TypeError: Cannot read property 'visualIndex' of null
我的GridView使用的是包含DelegateModelGroups的DelegateModel,相关代码如下:
MyDelegateModel.qml
import QtQuick 2.15 import QtQuick.Controls 2.15 import QtQml.Models 2.4 DelegateModel { property int from: 0 property int to: 10 groups: [ DelegateModelGroup { id: newItems name: "new" includeByDefault: true onCountChanged: { newItems.setGroups(from, to - from, ["new","visible"]); } }, DelegateModelGroup { id: visibleItems name: "visible" } ] filterOnGroup: "visible" }
GridView代码
GridView { id: mainGrid cellWidth: 195 cellHeight: 155 anchors.fill: parent property int lastChangedIndex: -1 property int xAxis: 0 property int yAxis: 0 displaced: Transition { NumberAnimation { properties: "x,y" easing.type: Easing.OutQuad } } model:MyDelegateModel{ id:visualModel from:index*35 to:Math.min(from + 35, appsModel.count) model:appsModel delegate: MouseArea{ id: delegateRoot property bool held: false property int visualIndex:index width: mainGrid.cellWidth-45; height: mainGrid.cellHeight-45; drag.target: held ? box : undefined onClicked:{ executable.exec("$(gtk-launch " + appsModel.get(index).display + ")") ; console.log(appsModel.get(index).display) } pressAndHoldInterval : 200 onPressAndHold: { held = true box.opacity = 0.5 box.z = 10 } onReleased: { if (held === true) { held = false box.opacity = 1 box.Drag.drop() box.z = 1 } else { //action on release } } Kirigami.Icon { id: box parent: mainGrid x: delegateRoot.x; y: delegateRoot.y-10; width: delegateRoot.width; height:delegateRoot.height; source: appicon Text { anchors.top: parent.bottom anchors.horizontalCenter:parent.horizontalCenter text: name font.pixelSize: 12 color:"black" } Drag.active: delegateRoot.drag.active Drag.source: delegateRoot Drag.hotSpot.x: 36 Drag.hotSpot.y: 36 SequentialAnimation on rotation { NumberAnimation { to: 4; duration: 60 } NumberAnimation { to: -4; duration: 120 } NumberAnimation { to: 0; duration: 60 } running: delegateRoot.held loops: Animation.Infinite; alwaysRunToEnd: true } states: [ State { when: box.Drag.active ParentChange { target: box parent: mainGrid } AnchorChanges { target: box anchors.horizontalCenter: undefined anchors.verticalCenter: undefined } } ] } DropArea { id: dropArea anchors { fill: parent margins: 15 } signal trigger Timer { id: dropZoneTimer interval: 1000 onTriggered: { dropArea.trigger() } } onTrigger: { visualModel.items.move(drag.source.visualIndex, delegateRoot.visualIndex) mainGrid.lastChangedIndex = delegateRoot.visualIndex } onEntered: { if (drag.source.visualIndex !== delegateRoot.visualIndex) { dropZoneTimer.start() } } onExited: { dropZoneTimer.stop() mainGrid.lastChangedIndex = -1 } onDropped: { if (mainGrid.lastChangedIndex !== delegateRoot.visualIndex) { visualModel.items.move(drag.source.visualIndex, delegateRoot.visualIndex) } console.log("drag from",drag.source.visualIndex,"drop to",delegateRoot.visualIndex ) } } } } }
已调试一整天仍未解决问题,恳请各位提供帮助。
内容的提问来源于stack exchange,提问作者cgiannakidis
相关产品推荐
相关产品推荐

