QML: 尽管拖动属性处于活动状态,但 onDragStarted/finished 未被调用

6
在下面的例子中,我期望当一个矩形被拖动时调用onDragStarted/onDragFinished,但是只有drag.onActiveChanged(鼠标区域的)和Drag.onActiveChanged(矩形的)被调用。当将Drag.dragType设置为Drag.Automatic时,我得到了预期的输出,但是此时我看不到矩形了。我在Mac(El Capitan)上使用Qt 5.5。
import QtQuick 2.5
import QtQuick.Window 2.2

Window {
  visible: true

  width: 100
  height: 200

  ListModel {
    id: testModel
    ListElement { name: "red";   value: "#f00" }
    ListElement { name: "green"; value: "#0f0" }
    ListElement { name: "blue";  value: "#00f" }
  }

  Component {
    id: rect
    Rectangle {

      Drag.active: mouseArea.drag.active
      Drag.hotSpot.x: width / 2
      Drag.hotSpot.y: height / 2
      //Drag.dragType: Drag.Automatic

      Drag.onActiveChanged: {
        console.log("Active changed..")
      }

      Drag.onDragStarted: {
        console.log("Drag started..")
      }

      Drag.onDragFinished: {
        console.log("Drag finished!")
      }

      MouseArea {
        id: mouseArea

        anchors.fill: parent
        hoverEnabled: true
        drag.target: parent

        drag.onActiveChanged: {
          console.log("Drag prop became active..")
        }

        onClicked: {
          colorButtonClicked(buttonName, buttonColor);
        }
      }

      width: 80
      height: 20
      radius: 6
      color: model.value
    }
  }

  Column {
    spacing: 3
    anchors.centerIn: parent
    Repeater {
      model: testModel
      delegate: rect
    }
  }
}

1
无法在我的Mac上测试,但在我的Win7机器上可以正常工作。看起来像是Mac实现的一个缺陷。 - BaCaRoZzo
确认在El Capitan、Qt 5.5.1版本中,onDragStarted / onDragFinished函数没有被调用。 - spring
1个回答

6

根据信号名称,我对同样的问题感到困惑,但是查看文档后发现,只有使用Drag.Automatic或显式调用startDrag时,信号才会起作用。

dragStarted()

当使用startDrag()方法启动拖动或使用dragType属性自动启动拖动时,将触发此信号。

dragFinished(DropAction action)

当拖动完成且使用startDrag()方法启动拖动或使用dragType属性自动启动拖动时,将触发此信号。

使用Drag.Automatic时出现的另一个问题似乎已在Qt 5.6.1中得到解决。


谢谢你的回答,我得在我的Macbook上升级Qt,并且再次进行测试。 - Nils

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接