QML:即使拖动属性处于活动状态,也不会调用 onDragStarted/finished [英] QML: onDragStarted / finished not called even though the drag property is active

查看:34
本文介绍了QML:即使拖动属性处于活动状态,也不会调用 onDragStarted/finished的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下示例中,我希望在拖动一个矩形时调用 onDragStarted/onDragFinished.然而,只有 drag.onActiveChanged(mouseArea 的)和 Drag.onActiveChanged(矩形的)被调用.将 Drag.dragType 设置为 Drag.Automatic 时,我得到了预期的输出,但是我再也看不到矩形了.我在 Mac (El Capitan) 上使用 Qt 5.5.

In the following example I would expect that onDragStarted / onDragFinished are called when one rectangle is dragged. However only drag.onActiveChanged (of the mouseArea) and Drag.onActiveChanged (of the rectangle are called). I get the expected output when setting Drag.dragType to Drag.Automatic but then I don't see the rectangle anymore. I am using Qt 5.5 on a Mac (El Capitan).

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

推荐答案

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

I was puzzled about the same problem based on my assumptions from the signal names, but a look at the documentation showed that the signals will only work when using Drag.Automatic or explicitly calling startDrag.

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

dragStarted()

This signal is emitted when a drag is started with the startDrag() method or when it is started automatically using the dragType property.

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

This signal is emitted when a drag finishes and the drag was started with the startDrag() method or started automatically using the dragType property.

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

The other issue seen when using Drag.Automatic appears to be fixed in Qt 5.6.1

这篇关于QML:即使拖动属性处于活动状态,也不会调用 onDragStarted/finished的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆