qt - 如何使用 QML 在父 MouseArea 中包含子鼠标悬停事件?

标签 qt qml qt5 qtquick2

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

Scenario

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

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 有疑问.
MouseArea (actionButton) 对于 entered 不能正常工作事件。当鼠标进入 Action 按钮时,外部MouseArea火灾 exited事件。

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

最佳答案

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

如果你想在你的父级中包含子级鼠标悬停事件 MouseArea ,让你成为 child MouseArea parent 的 child MouseArea :

MouseArea {
    id: parent

    MouseArea {
        id: child
    }
}

由于我有一个自定义 Widget将用作父 View 的类型,我最终得到 default属性是 MouseArea 的子级:
Item {
    default property alias children: mouseArea.data

    MouseArea {
        id: mouseArea
    }
}

关于qt - 如何使用 QML 在父 MouseArea 中包含子鼠标悬停事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18135262/

相关文章:

c++ - 如何在不导致内存泄漏的情况下使用 QSGGeometryNode 并确保正确清理

c++ - 如何在 QSortFilterProxyModel 中获取一个项目的行给 QString?

go - 如何为 “release” 创建一个 go 文件?

centos - 如何在 linux 中开始在 Qt5.8 上开发 OpenDDS?

c++ - 使用 wrld.js 时 QWebEngineView "Access-Control-Allow-Headers"错误

c - Gstreamer。对 `gst_video_overlay_set_window_handle' 的 undefined reference

android - Android 上的 Qt 应用程序(无需 Ministro)

qt - 计算两个日期之间的天数

c++ - 如何从 QPlainTextEdit 中读取特定行

qt - 什么Qt容器类用于排序列表?