2016-03-07 174 views
1

我已經有QML ListView拖動項目。當我拿東西並移動鼠標時(很多時候)項目回到初始位置,然後回到實際位置等。 在Linux和Windows上發生了Qt 5.5.1。 下面是有問題的示例代碼。嘗試從左到右拖動項目並查看輸出日誌。有時它的大量輸入/離開放置區域。qt拖動時拖動快速拖放項目抖動

import QtQuick 2.5 
    import QtQuick.Window 2.2 
    import QtQuick.Layouts 1.2 

    Window { 
     visible: true 
     width: Screen.width 
     height: Screen.height 
     property int num:150 
     Row{ 
      anchors.fill: parent 
      ColumnLayout{ 
       id:col1 
       width: parent.width/2 
       height: parent.height 
       DropArea{ 
        anchors.fill: parent 
        onEntered: { 
         console.log("entered:"+drag.source) 
        } 
        onExited: { 
         console.log("exited:"+drag.source) 
        } 

     } 
       ListView{ 
        spacing: 2 
        model:num 
        anchors.fill: parent 
        delegate: Rectangle{ 
         width: parent.width/2 
         height: width 
         color:"green" 
        } 

     } 
      } 
      ColumnLayout{ 
       id:col2 
       width: parent.width/2 
       height: parent.height 

     ListView{ 
        anchors.fill: parent 
        spacing: 2 
        model:num 
        delegate: Rectangle{ 
         id:restItem 
         property point beginDrag 
         property int maxDragX: 96 
         width: parent.width/2 
         height: width 
         color:"red" 
         Drag.active: mouseArea.drag.active 
         MouseArea { 
          id: mouseArea 
          anchors.fill: parent 
          drag{ 
           target: restItem 
           axis: Drag.XAxis 
           smoothed: true 
           threshold: width/3 
           maximumX: 0 
           minimumX: -maxDragX 

        } 
          preventStealing: true 
          onPressed: { 
           restItem.beginDrag = Qt.point(restItem.x, restItem.y); 
          } 
          onReleased: { 
           backAnimX.from = restItem.x; 
           backAnimX.to = beginDrag.x; 
           backAnimY.from = restItem.y; 
           backAnimY.to = beginDrag.y; 
           backAnim.start() 
          } 
         } 
         ParallelAnimation { 
          id: backAnim 
          alwaysRunToEnd: true 
          running: false 
          SpringAnimation { id: backAnimX; target: restItem; property: "x"; duration: 500; spring: 2; damping: 0.2 } 
          SpringAnimation { id: backAnimY; target: restItem; property: "y"; duration: 500; spring: 2; damping: 0.2 } 
         } 
        } 

     } 
      } 
     } 
    } 
+0

我正在處理您的代碼,並且只有在單擊並嘗試移動該項目時仍然移動的情況下才會看到此行爲。如果該項目完成其移動或動畫,那麼我不會觀察您解釋的問題。 – Tarod

回答

1

我發表了評論,但我想與您分享也有可能的解決方案的答案。

在這種情況下,您可以啓用或禁用MouseArea以避免該問題。

想法是在onPressed插槽中禁用鼠標區域,並在動畫停止時啓用它,並將其命名爲onStopped插槽。

... 

       delegate: Rectangle{ 
        id:restItem 
        property point beginDrag 
        property int maxDragX: 96 
        width: parent.width/2 
        height: width 
        color:"red" 
        Drag.active: mouseArea.drag.active 
        MouseArea { 
         id: mouseArea 
         anchors.fill: parent 
         drag{ 
          target: restItem 
          axis: Drag.XAxis 
          smoothed: true 
          threshold: width/3 
          maximumX: 0 
          minimumX: -maxDragX 

         } 
         preventStealing: true 
         onPressed: { 
          mouseArea.enabled = false; 
          restItem.beginDrag = Qt.point(restItem.x, restItem.y); 
         } 
         onReleased: { 
          backAnimX.from = restItem.x; 
          backAnimX.to = beginDrag.x; 
          backAnimY.from = restItem.y; 
          backAnimY.to = beginDrag.y; 
          backAnim.start() 
         } 
        } 
        ParallelAnimation { 
         id: backAnim 
         alwaysRunToEnd: true 
         running: false 
         SpringAnimation { id: backAnimX; target: restItem; property: "x"; duration: 500; spring: 2; damping: 0.2 } 
         SpringAnimation { id: backAnimY; target: restItem; property: "y"; duration: 500; spring: 2; damping: 0.2 } 
         onStopped: { 
          mouseArea.enabled = true; 
         } 
        } 
       } 
... 
+1

謝謝你的回答,這是有效的。 – swex