首页 > 代码库 > QML MouseArea中的hoverEnabled属性影响onEntered, onExited

QML MouseArea中的hoverEnabled属性影响onEntered, onExited

        学习过程,记录一下。

        这段时间学习Quick2在自己定义一个按钮时想做一个效果,鼠标悬停在按钮上时按钮背景显示一种颜色,鼠标离开时回复原样。

        刚开始是代码是这样的:

Rectangle{
            color:"green"
            width: 28
            height: 28

            MouseArea{
                anchors.fill:parent

                onEntered: {
                    parent.color="red";
                }

                onExited: {
                    parent.color="green";
                }
             }
        }

        onEntered事件死活不执行,后来看了帮助文档这样写

        hoverEnabled : bool

        This property affects the containsMouse property and the onEntered, onExited and onPositionChanged signals.

     默认hoverEnabled属性是false,结果代码加上hoverEnabled:true,结果就出来了。

Rectangle{
            color:"green"
            width: 28
            height: 28

            MouseArea{
                anchors.fill:parent
                hoverEnabled: true;

                onEntered: {
                    parent.color="red";
                }

                onExited: {
                    parent.color="green";
                }
             }
        }

        英语是死穴,学起来也费劲,加油!!

QML MouseArea中的hoverEnabled属性影响onEntered, onExited