【问题标题】:How to disable drag&drop for first Item of a ListView in QML如何在 QML 中禁用 ListView 的第一个项目的拖放
【发布时间】:2017-04-10 12:18:43
【问题描述】:

我有一个 QML 项目,我可以在其中拖放ListView 中的矩形。 我想禁用ListView 的第一项(矩形)的拖放功能。

这是一个例子:

Rectangle {
    visible: true
    width: 1000; height: 1000
    ListView {
        id: root
        width: parent.width; height: parent.height

        model: DelegateModel {

            id: visualModel
            model: myModel
            model: ListModel {
                id: colorModel
                ListElement { someData }
                ...

            }

            delegate: MouseArea {

                property int visualIndex: DelegateModel.itemsIndex

                id: delegateRoot
                cursorShape: Qt.PointingHandCursor
                width: root.width; height: 100
                drag.target: icon
                drag.axis: Drag.YAxis
                drag.minimumY: 0
                property bool current: false

                Rectangle {
                  blablaData

                 //Something like : if firstItem, disable drag&drop
                }

                onClicked: {
                    delegateRoot.current = !delegateRoot.current;
                    if(current) {
                        delegateRoot.height = 300
                    }
                    else {
                        delegateRoot.height = 100
                    }
                }

                Rectangle {
                   id: container
                   anchors.top: icon.bottom
                   width: root.width-5
                   height: delegateRoot.height - icon.height
                   clip: true
                   border.color: "#81BEF7"
                   Behavior on implicitHeight {
                       PropertyAnimation { duration: 100 }
                   }
                   Text {
                       anchors.fill: parent
                       anchors.margins: 10
                       horizontalAlignment: Text.AlignLeft
                       verticalAlignment: Text.AlignVCenter
                       text: size
                   }
                 }

                DropArea {
                    anchors { fill: parent; margins: 15 }

                    onEntered: {                   
                          visualModel.items.move(drag.source.visualIndex, delegateRoot.visualIndex)               
                    }
                 }
             }
          }
    }
}

你知道怎么做吗? 非常感谢!

编辑:为我的示例添加了一些功能

【问题讨论】:

    标签: qt drag-and-drop qml qtquick2


    【解决方案1】:

    在委托根项目中,尝试:

    enabled: index ? true : false
    

    【讨论】:

    • 您好,感谢您的回复。您的回答正确禁用了我的第一个矩形的拖放。但是我的矩形也有一些自定义属性(onClicked 等),所以如果enabled = false,我不能再点击它了。有什么建议吗?非常感谢!
    • 一旦您使用 javascript 强制设置属性,您将失去 QML 的声明性功能。您能否用您实际编写的内容更新您的代码?
    • 我刚刚更新了我的代码。事实上,我正在做一个“手风琴”项目。所以当我点击矩形时,它会显示一个子菜单(在我的例子中是Rectangle "container")。
    【解决方案2】:

    我找到了一种简单的方法,您可以将它用于您想要的项目(不仅仅是第一个)。

    我需要通过在每个项目上使用并将isDraggable 之类的布尔值设置为true 或false 来更改delegateRoot 中的drag.target 和drag.axis,然后像这样使用它:

    drag.target: isDraggable ? content : undefined
    drag.axis: isDraggable ? Drag.YAxis : Drag.None
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-02
      • 2016-07-26
      • 2016-03-26
      • 2017-04-06
      • 1970-01-01
      • 2015-06-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多