2016-06-28 1 views
2

나는 DropArea과 두 개의 요소를 가지고 있습니다. DropAreaDropArea이 이미 하나의 요소가 삭제 된 경우 삭제 이벤트를 거부하고, 다른 요소는 삭제를 허용하지 않습니다. QML : 드롭 동작을 거부하는 방법

DropArea { 
    property bool dropped: false 

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

그러나이 객체가 대신 DropArea

+0

무엇 Qt는 버전? – BaCaRoZzo

답변

2

항목이 dropped 속성을 확인, onReleased에 떨어 뜨리거나하지해야하는 경우가 제어해야합니다.

전체 예 :

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; 
      } 
     } 
    } 
} 
1

사용 drop.accept()에 떨어졌다 얻을 BTW 어쨌든, 작동하지 drop.accepted 것 같습니다.

property bool containsItem: false 
DropArea { 
    id: dropArea 
    anchors.fill: parent 
    onDropped: { 
     if(containsItem) 
      drop.accept(Qt.IgnoreAction) 
     else 
      drop.accept() 

     containsItem = true; 
    } 
} 

는 또한 onDropped 이벤트 핸들러 내부에 연결된 속성이 이미로 dropped 속성을 사용하여이 donot 다음과 같이 이상 수행 할 수 있습니다.

편집 : rect 인 경우 항목을 드래그하고 드롭되는 :

Rectangle { 
    id: rect 
    width: 40; height: 40 
    color: "red" 

    Drag.active: dragArea.drag.active 
    Drag.hotSpot.x: 20 
    Drag.hotSpot.y: 20 

    MouseArea { 
     id: dragArea 
     anchors.fill: parent 
     drag.target: parent 
     onReleased: if (rect.Drag.drop() !== Qt.IgnoreAction) { 
         console.log("Accepted!"); 
        } else { 
         console.log("Rejected!"); 
        } 
    } 
} 
+0

고마워,하지만'drop.accept (Qt.IgnoreAction)'여전히 작동하지 않습니다. 다음과 같이 : 'onDropped : { drop.accept (Qt.IgnoreAction) } ' – Jiu

+1

무시 또는 수락 액션은 드래그 된 아이템의'MouseArea' 핸들러의'onReleased' 핸들러 안에서 처리 될 수 있습니다. – astre