qt - 从 Listview 拖动到放置区域
问题描述
我在此处遵循此示例使用 ListView 拖放来创建库存 UI。但是,我已将组件分解为主要部分、Listview 和 droparea。一切正常,除了我无法获得ReferenceError: listView is not defined
. 请帮忙
主要区域
import QtQuick 2.15
import QtQuick.Controls 2.15
Page{
id:dragRect
Rectangle {
id: root
width: 400
height: 400
ListViewElem{
}
DropAreaElem{}
}
}
ListViewElem
import QtQuick 2.15
ListViewElem {
id: listView
width: parent.width / 2
height: parent.height
property int dragItemIndex: -1
model: ListModel {
Component.onCompleted: {
for (var i = 0; i < 10; ++i) {
append({value: i});
}
}
}
delegate: Item {
id: delegateItem
width: listView.width
height: 50
Rectangle {
id: dragRect2
width: listView.width
height: 50
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
color: "salmon"
border.color: Qt.darker(color)
Text {
anchors.centerIn: parent
text: modelData
}
MouseArea {
id: mouseArea
anchors.fill: parent
drag.target: dragRect2
drag.onActiveChanged: {
if (mouseArea.drag.active) {
listView.dragItemIndex = index;
}
dragRect2.Drag.drop();
}
}
states: [
State {
when: dragRect2.Drag.active
ParentChange {
target: dragRect2
parent: root
}
AnchorChanges {
target: dragRect2
anchors.horizontalCenter: undefined
anchors.verticalCenter: undefined
}
}
]
Drag.active: mouseArea.drag.active
Drag.hotSpot.x: dragRect2.width / 2
Drag.hotSpot.y: dragRect2.height / 2
}
}
}
DropAreaElem
import QtQuick 2.15
Rectangle {
width: parent.width / 2
height: parent.height
anchors.right: parent.right
color: "#aaff0011"
DropArea {
id: dropArea
anchors.fill: parent
onDropped: {
listView.model.remove(listView.dragItemIndex);
listView.dragItemIndex = -1;
}
}
}
解决方案
您应该id
向您的组件提供
import QtQuick 2.15
import QtQuick.Controls 2.15
Page{
id:dragRect
Rectangle {
id: root
width: 400
height: 400
ListViewElem{
id: listView
}
DropAreaElem{
id: dropArea
}
}
}
推荐阅读
- mysql - 如何将 SQL CASE 语句与另一个字段的 DISTINCT 结合起来
- scroll - 在 AMP 页面上使用跟踪代码管理器滚动事件无法跟踪百分比
- continuous-integration - GitHub Actions:Prettier 仅在 windows-latest 上发现错误
- python - 创建包含构造函数参数中的字典的类的多个实例
- selenium - 有什么方法可以为一项测试设置“noReset”Appium 功能吗?
- flutter - 如何在颤振应用程序中将 setData 转换为事务?
- azure-data-factory - 我如何读取和存储来自 Web 活动(发布)的回复 - Azure datafactory
- c# - C# windows 客户端应用程序 datagridview 绑定到带有 linq 查询的数据行数组
- oracle - Pl sql如何将2个输出值保存在另一个过程中的2个不同变量中
- c# - 如何在提交后刷新 DataAnnotationsValidator 的绿色轮廓