首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >QML - MouseArea/MouseEvent发行

QML - MouseArea/MouseEvent发行
EN

Stack Overflow用户
提问于 2013-09-26 15:16:49
回答 1查看 5K关注 0票数 3

下面的代码生成一个白色矩形,其中包含一个红色矩形和一个灰色矩形。每个矩形都有一个关联的MouseArea。当鼠标在里面点击时,灰色矩形变成蓝色。红色矩形在鼠标光标进入其中时打印控制台消息,在发出释放信号时打印另一条消息。

我想:

  1. 按下并按住灰色矩形内的鼠标按钮(变成蓝色)
  2. 将光标移出灰色/蓝色矩形,然后在红色矩形内移动,而不释放按钮并捕获输入的红色矩形信号。
  3. 释放按钮,使光标在红色矩形内,并捕获释放的红色矩形信号。

有可能吗?对于当前代码,只有在输入时未按下mopuse按钮时,才发出红色矩形的输入信号,只有在该矩形内按下按钮时才发出释放的信号。问题是,很明显,如果按下按钮,灰色/蓝色矩形就会控制鼠标事件。

这与我正在开发的应用程序中的场景相似,但很简单。

代码语言:javascript
复制
import QtQuick 2.0

Rectangle{
    color: "white"
    height: 210
    width: 500

    MouseArea{
      id: mainMa
      anchors.fill: parent
      hoverEnabled: true
      onReleased:{console.log("white-released")}
   }

   Column{
       spacing: 10
       Rectangle{
           color: "red"
           height: 100
           width: 500
          MouseArea{
             anchors.fill: parent
             hoverEnabled: true
             propagateComposedEvents: true
             onEntered:{console.log("red-enter")}
             onReleased:{console.log("red-released")}
          }
       }

       Rectangle{
           color: "#666666"
           height: 100
           width: 500
           MouseArea{
               id: ma
               anchors.fill: parent
               hoverEnabled: true
               onPressed: {parent.color = "blue"; console.log("grey pressed")}
               onReleased: {parent.color = "#666666"; console.log("grey released")}
           }
       }
   }

}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-26 17:31:49

我想你想要拖放操作。为此,在红色矩形中添加DropArea,在灰色矩形中添加活动拖动。

类似的东西(最小代码):

代码语言:javascript
复制
Rectangle {
    Column {
        Rectangle {
            id: redRect
            DropArea {
                anchors.fill: parent
                onEntered: { console.log("red-enter") }
                onDropped: { console.log("red-released") }
            }
        }
        Rectangle {
            id: greyRect
            Drag.active: mousearea.drag.active
            Drag.hotSpot.x: mousearea.mouseX
            Drag.hotSpot.y: mousearea.mouseY
            MouseArea {
                id: mousearea
                anchors.fill: parent
                onReleased: parent.Drag.drop()
                drag.target: parent
            }
        }
    }
}

如果不想移动灰色矩形,可以添加不可见的可拖动项:

代码语言:javascript
复制
    MouseArea {
    id: mousearea
    anchors.fill: parent
    onReleased: dargItem.Drag.drop()
    drag.target: dargItem
    Item {
        id: dargItem
        x: mousearea.mouseX
        y: mousearea.mouseY
        width: 1; height: 1
        Drag.active: mousearea.drag.active
        Drag.hotSpot.x: 1
        Drag.hotSpot.y: 1
    }                    
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19032143

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档