qt - QML:如何拒绝放置操作

标签 qt qml qtquick2

我有一个 DropArea 和两个元素。我希望 DropArea 如果 DropArea 已经有一个元素被放置,则拒绝放置事件,另一个元素不允许放置,除非第一个元素移出。

DropArea {
    property bool dropped: false

    onDropped: {
        drop.accepted = !dropped;
        dropped = true;
    }
    onExited: dropped = false
}

但看起来 drop.accepted 不起作用, 顺便说一句,无论如何要让对象被丢弃在 DropArea

最佳答案

您应该在 onReleased 中控制是否必须删除项目,检查 dropped 属性。

完整示例:

import QtQuick 2.5
import QtQuick.Window 2.2
import QtQuick.Controls 1.4

Window {
    id: win
    visible: true
    width: 800
    height: 600
    title: qsTr("Hello World")

    Repeater {
        model: 10
        Rectangle {
            id: rect
            width: 50
            height: 50
            z: mouseArea.drag.active ||  mouseArea.pressed ? 2 : 1
            color: Qt.rgba(Math.random(), Math.random(), Math.random(), 1)
            x: Math.random() * (win.width / 2 - 100)
            y: Math.random() * (win.height - 100)
            property point beginDrag
            property bool caught: false
            border { width:2; color: "white" }
            radius: 5
            Drag.active: mouseArea.drag.active

            Text {
                anchors.centerIn: parent
                text: index
                color: "white"
            }
            MouseArea {
                id: mouseArea
                anchors.fill: parent
                drag.target: parent
                onPressed: {
                    rect.beginDrag = Qt.point(rect.x, rect.y);
                }
                onReleased: {
                    if(!rect.caught || dragTarget.dropped) {
                        backAnimX.from = rect.x;
                        backAnimX.to = beginDrag.x;
                        backAnimY.from = rect.y;
                        backAnimY.to = beginDrag.y;
                        backAnim.start()
                    }

                    parent.Drag.drop()

                    console.log("MouseArea - containsDrag " + dragTarget.dropped)
                }

            }
            ParallelAnimation {
                id: backAnim
                SpringAnimation { id: backAnimX; target: rect; property: "x";
                                  duration: 500; spring: 2; damping: 0.2 }
                SpringAnimation { id: backAnimY; target: rect; property: "y";
                                  duration: 500; spring: 2; damping: 0.2 }
            }
        }
    }

    Rectangle {
        anchors {
            top: parent.top
            right:  parent.right
            bottom:  parent.bottom
        }
        width: parent.width / 2
        color: "gold"
        DropArea {
            id: dragTarget
            anchors.fill: parent

            property bool dropped: false

            onEntered: {
                console.log("onEntered " + containsDrag)
                drag.source.caught = true;
            }
            onExited: {
                console.log("onExited " + containsDrag)
                dropped = false;
            }
            onDropped:
            {
                console.log("onDropped");
                dropped = true;
            }
        }
    }
}

关于qt - QML:如何拒绝放置操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38068528/

相关文章:

c++ - 在 Qt Quick 2 中嵌入 QWidget 对象

JavaScript 控制台和 QML WebView

c++ - 发射信号不更新 GUI 中的值

javascript - 如何获取QML中项目的基类?

qt - 如何在 QT OpenGL Widget 上使用 OpenGL 函数?

qt - 在 QtCreator 中调试时如何查看 qDebug 消息

c++ - 是否可以为 qlistWidget 的每个项目添加隐藏值

c++ - 如何在 QML 中访问 C++ 类对象而不是在 QML 中创建单独的对象?

javascript - 引用错误 : Component is not defined - QML Dynamic object creation

c++ - 如何在 Windows 上切换到另一个应用程序(使用 C++、Qt)?