【问题标题】:QML: onDragStarted / finished not called even though the drag property is activeQML:即使拖动属性处于活动状态,也不会调用 onDragStarted / finished
【发布时间】:2016-03-13 13:13:13
【问题描述】:

在以下示例中,我希望在拖动一个矩形时调用 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
    }
  }
}

【问题讨论】:

  • 无法在我的 Mac 上测试它,但它在我的 Win7 机器上运行良好。闻起来像 Mac 实现错误。
  • 在这里确认 - El Capitain,Qt 5.5.1 - onDragStarted / onDragFinished 没有被调用

标签: drag-and-drop qml draggable qt-quick


【解决方案1】:

根据我对信号名称的假设,我对同样的问题感到困惑,但查看documentation 表明信号仅在使用Drag.Automatic 或显式调用startDrag 时才有效。

dragStarted()

当使用 startDrag() 方法开始拖动或使用 dragType 属性自动开始拖动时,会发出此信号。

dragFinished(DropAction 动作)

当拖动完成并且使用 startDrag() 方法开始拖动或使用 dragType 属性自动开始拖动时,会发出此信号。

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

【讨论】:

  • 感谢您的回答,我必须在我的 Macbook 上升级 Qt 并在某个时候再次测试它。
猜你喜欢
  • 1970-01-01
  • 2021-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多