QML MouseArea propagate events to buttons

2019-06-24 08:34发布

I'm a developing an app that has a menu similar to the Gmail/Inbox app menu, for Android. Basically, when you press the button to open the menu, it slides into view. The user can swipe it away or press the buttons on the menu. For the swipe I've used the code SwipeArea from kovrov with a small change to check the speed between press and release - if it's too slow, I don't consider it a swipe. I've also set the propagate property to true on the MouseArea, i.e. propagateComposedEvents: true and added this code for the clicked event:

onClicked: {
    mouse.accepted = false;
}

Now I have the following code:

Button {
    x: 10
    y: 50
    width: 200
    onClicked: {
        console.log("Button clicked!");
    }
}
SwipeArea {
    anchors.fill: parent

    onMove: {
        var newX = menu.x + x;
        if(newX <= menu.maxX && newX >= menu.minX) menu.x = newX;
    }
    onSwipe: {
        if(direction === "left" && timePassed <= menu.fastSwipe) root.hideMenu(menu.x, timePassed);
        else {
            var middle = menu.width/2;
            var end = menu.x + menu.width;
            if(end > middle) root.showMenu(menu.x);
            else if(end <= middle) root.hideMenu(menu.x);
        }
    }
}

If the Button is rendered on top (code below SwipeArea) it works but then the swipe won't work on top of the Button. If the Button is replaced with a MouseArea the event is correctly triggered. It seems the event is not being propagated? Is there a way to propagate the event to the Button? Or do I have to create my own buttons (Rectangle with MouseArea, for example)?

Thank you for your time.

0条回答
登录 后发表回答