简体   繁体   English

鼠标按下时 qml 鼠标移动

[英]qml mouse move while mouse pressed

I have two rectangles and i want them to send mouseevent to each other while moving mouse with pressed mouse button.我有两个矩形,我希望它们在按下鼠标按钮移动鼠标时相互发送 mouseevent。 See my example看我的例子

Rectangle {
    x: 100
    width: 30
    height: 30
    color: "red"
    MouseArea {
        hoverEnabled: true
        propagateComposedEvents: true

        anchors.fill: parent
        onMouseXChanged: console.log("red changed" + mouseX)
    }
}

Rectangle {
    x: 130
    width: 30
    height: 30
    color: "green"
    MouseArea {
        hoverEnabled: true
        propagateComposedEvents: true

        anchors.fill: parent
        onMouseXChanged: console.log("green changed"+ mouseX)
    }
}

Everything works fine when i just move mouse between rectangles.当我只是在矩形之间移动鼠标时,一切正常。 But when i try to pres mouse button and then move - only one rectangle receive events.但是当我尝试按下鼠标按钮然后移动时 - 只有一个矩形接收事件。 Is there a way to make it work with button pressed?有没有办法让它在按下按钮的情况下工作?

PS Initial problem, of course, is not with rectangles. PS 最初的问题当然不是矩形。 Im trying to make combobox work quicker, when you need just one press to open popup and move cursor to item you want to select.我试图让组合框更快地工作,当您只需要按一下即可打开弹出窗口并将光标移动到您要选择的项目时。 But i can find a way to move press event from combobox input to popup.但是我可以找到一种方法将按下事件从组合框输入移动到弹出窗口。 I think example i showed above is the right one to understand the problem.我认为我上面展示的例子是理解问题的正确例子。

The problem is that once a button is pressed, the MouseArea holds the mouse events even if we move outside the area .问题是,一旦按下按钮,即使我们移动到区域外MouseArea也会保存鼠标事件。 Then the other MouseArea is not able to catch the mouse events.然后另一个MouseArea无法捕获鼠标事件。

The only solution I can imagine is to manage the change of position globally so that each MouseArea receives the positionChange signal from any other MouseArea and individually decides whether an action is needed or not (See mapFromItem for position mapping):我能想象到的唯一解决方案是全局管理位置的变化,以便每个MouseArea接收来自任何其他MouseAreapositionChange信号,并单独决定是否需要某个操作(请参阅mapFromItem以获取位置映射):

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3

ApplicationWindow {
    visible: true
    width: 500
    height: 500

    signal globalPositionChanged(var item, var position)

    Rectangle {
        id: rect1
        x: 100
        width: 30
        height: 30
        color: "red"
        MouseArea {
            hoverEnabled: true
            propagateComposedEvents: true
            anchors.fill: parent
            clip: true

            onPositionChanged: {
                globalPositionChanged(rect1, mouse)
            }

            Component.onCompleted: {
                globalPositionChanged.connect(handlePositionChange)
            }

            function handlePositionChange(item, position) {
                var localPos = toLocalePosition(rect1, item, position)
                if (localPos) {
                    // we are in the red rectangle
                    console.log("red", localPos.x, localPos.y)
                }
            }
        }
    }

    Rectangle {
        id: rect2
        x: 130
        width: 30
        height: 30
        color: "green"
        MouseArea {
            hoverEnabled: true
            propagateComposedEvents: true
            clip: true
            anchors.fill: parent

            onPositionChanged: {
                globalPositionChanged(rect2, mouse)
            }

            Component.onCompleted: {
                globalPositionChanged.connect(handlePositionChange)
            }

            function handlePositionChange(item, position) {
                var localPos = toLocalePosition(rect2, item, position)
                if (localPos) {
                    // we are in the green rectangle
                    console.log("green", localPos.x, localPos.y)
                }
            }

        }
    }

    function toLocalePosition(toItem, fromItem, position) {
        // return the local position if inside item, or null if outside
        var localPos = toItem.mapFromItem(fromItem, position.x, position.y)
        if (localPos.x >= 0
                && localPos.y >= 0
                && localPos.x <= toItem.width
                && localPos.y <= toItem.height) {
            return localPos
        }
        return null
    }
}

I'm not 100% convinced about this answer.我不是 100% 相信这个答案。 There are probably better ways to do it, but I think it fixes your problem.可能有更好的方法来做到这一点,但我认为它可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM