29

我想在 QML 中实现以下场景。

设想


这是ListView元素的示例/简化委托:

Component {
    Item {
         id: container
         MouseArea {
         anchors.fill: parent
         hoverEnabled: true

         onClicked: {
             container.ListView.view.currentIndex = index
             container.forceActiveFocus();
         }
         onEntered: {
             actionList.state = "SHOW";
             myItem.state = "HOVER"
         }
         onExited: {
             actionList.state = "HIDE";
             myItem.state = "NORMAL"
         }
         Rectangle {
             id: myItem
             color: "gray"
             anchors.fill: parent
             Row {
                 id: actionList
                 spacing: 5; anchors.fill: parent
                 Image {
                     id: helpAction
                     source: ""    //Some image address
                     width: 16; height: 16; fillMode: Image.PreserveAspectFit
                     states: [
                         State {
                             name: "NORMAL"
                             PropertyChanges { target: helpAction; opacity: 0.7 }
                         },
                         State {
                             name: "HOVER"
                             PropertyChanges { target: helpAction; opacity: 1.0 }
                         }
                     ]
                     MouseArea {
                         hoverEnabled: true
                         anchors.fill: parent

                         onEntered: {
                             parent.state = "HOVER";
                         }
                         onExited: {
                             parent.state = "NORMAL";
                         }
                     }
                     states: [
                         State {
                             name: "SHOW"
                             PropertyChanges { target: actionList; visible: false }
                         },
                         State {
                             name: "HIDE"
                             PropertyChanges { target: actionList; visible: true }
                         }
                     ]
                 }

                 //Other action buttons...

                 states: [
                     // `NORMAL` and `HOVER` states definition here...
                 ]
             }
         }
    }
}

但我有一个问题MouseArea
内部(actionButton)对于事件MouseArea不能正常工作。entered当鼠标进入动作按钮时,外部MouseArea触发exited事件。

我的代码有什么错误吗?更一般地说,我如何在 QML 中实现这样的场景?

4

4 回答 4

33

我遇到了同样的问题,并在QtQuick 5.0 文档中找到MouseArea了答案。这个问题的答案其实很简单。

如果您想在您的父级中包含子级鼠标悬停事件,请MouseArea让您MouseArea的子级成为父级的子级MouseArea

MouseArea {
    id: parent

    MouseArea {
        id: child
    }
}

由于我有一个Widget将用作父视图的自定义类型,因此我最终将default属性作为子视图MouseArea

Item {
    default property alias children: mouseArea.data

    MouseArea {
        id: mouseArea
    }
}
于 2014-01-28T16:07:32.830 回答
5

我尝试了一些事情,但似乎不可能MouseArea同时将鼠标悬停在两个上。和似乎仅在您有点击事件时才有效preventStealingpropagateComposedEvents但是从内部MouseArea你可以触发entered()另一个的信号。像这样的东西:

import QtQuick 2.1

Rectangle {
    width: 500
    height: 500

    Rectangle {
        width:300
        height: 300
        color: "red"

        MouseArea {
            id: big
            anchors.fill: parent
            hoverEnabled:true
            onEntered: {
                console.log("ENTERED BIG mousearea");
            }
            onExited: {
                console.log("EXITED BIG mousearea");
            }
        }

        Rectangle {
            anchors.centerIn: parent
            height: 100
            width: 100
            color: "green"

            MouseArea {
                anchors.fill: parent
                hoverEnabled:true
                onEntered: {
                    console.log("ENTERED small mousearea");
                    big.entered();
                }
                onExited: {
                    console.log("EXITED small mousearea");
                    big.exited();
                }
            }
        }
    }
}

问题是exited()来自容器的信号将在再次回调MouseArea之前被调用。entered()因此,您可能需要“延迟”状态更改,exited()以确保您确实想要隐藏您的操作按钮。另一种解决方案是保存当前鼠标位置并仅在exited()使用鼠标在其边框之一上调用时隐藏按钮。

于 2013-08-09T11:54:08.553 回答
5

为视图中元素的每个状态创建状态,然后您可以使用 if 语句或 case 语句来更改这些属性。换句话说,尽量不要将您的元素设置为在 MouseArea 上工作,而是在属性上工作并将 Elements 属性设置为处理设置的属性我希望这会有所帮助,如果不是这里是示例:

编辑我添加了透明的颜色。如果没有鼠标怎么办。如果我使用的是图像,我会使用不透明度然后添加一堆行为但这是一个工作

例子

import QtQuick 2.0
Rectangle {
    width: 360
    height: 360
    property string state1:"OutMouse"
    property string state2: "OutMouse"
    property string state3: "OutMouse"
    property string state4: "OutMouse"
    Rectangle{
        id:blueRec
        width: parent.width
        height: parent.height / 6
        color: state1 === "InMouse" ? "blue" : "green"
        MouseArea{
            anchors.fill: blueRec
            hoverEnabled: true
            onEntered: state1 = "InMouse"
            onExited: {
                if (state1 === state2 || state3 || state4){
                    state1 = "InMouse"
                }
                if(state1 !== state2 || state3 || state4)
                {
                    state1 = "OutMouse"
                }
            }
        }
        Text {
            text: state1=== "InMouse"? qsTr("foo") :"bar"
            anchors.centerIn: blueRec
        }
        Row{
            width: parent.width
            height: parent.height / 4

            spacing: 2
            anchors{
                left: parent.left
                verticalCenter:  blueRec.verticalCenter
                leftMargin: blueRec.width / 12
            }
            Rectangle{
                id: rec1
                height: parent.height;
                width: height
                color: {
                    if  ( state3 === "InMouse")
                        return "gray"
                    if (state1 === "OutMouse")
                        return "transparent"
                    else
                        return "white"}
                MouseArea{
                    id: rec1M
                    anchors.fill: parent
                    hoverEnabled: true
                    onEntered:{
                        state1 = "InMouse"
                        state2 = "InMouse"
                    }
                    onExited: state2 = "OutMouse"
                }
            }

            Rectangle{
                id: rec2
                height: parent.height ;
                width: height
                color: {
                    if  (state3 === "InMouse")
                        return "gray"
                    if (state1 === "OutMouse")
                        return "transparent"
                    else
                        return "white"
                }
                MouseArea{
                    id: rec2M
                    anchors.fill: parent
                    hoverEnabled: true
                    onEntered:{
                        state1 = "InMouse"
                        state3 = "InMouse"
                    }
                    onExited: state3 = "OutMouse"
                }
            }

            Rectangle{
                id: rec3
                height: parent.height;
                width: height
                color:{
                    if  (state4 === "InMouse")
                        return "gray"
                    if (state1 === "OutMouse")
                        return "transparent"
                    else
                        return "white"
                }
                MouseArea{
                    id:  rec3M
                    anchors.fill: parent
                    hoverEnabled: true
                    onEntered:{
                        state4 = "InMouse"
                        state1 = "InMouse"
                    }
                    onExited: state4 = "OutMouse"
                }
            }
        }
    }
}
于 2013-09-28T19:30:25.720 回答
1

试试这个:

  • 向鼠标输入时发出的内部区域添加一个信号。
  • 将信号连接到外部区域。
  • 该信号使外部区域进入悬停状态。

两者上的鼠标退出仍将取消悬停状态。当您将鼠标从控件上移开时,它应该可以正常工作而无需任何额外代码

于 2013-08-23T01:57:14.387 回答