zoukankan      html  css  js  c++  java
  • QML 解决button中添加了MouseArea,点击事件就不响应了的问题

    ToolBar {
            RowLayout {
                anchors.fill: parent
                ToolButton {
                    id: toolbutton
                    text: qsTr("")
                    onClicked: console.log("<")
                }
                Label {
                    text: "Title"
                    elide: Label.ElideRight
                    horizontalAlignment: Qt.AlignHCenter
                    verticalAlignment: Qt.AlignVCenter
                    Layout.fillWidth: true
                }
                ToolButton {
                    text: qsTr("")
                    onClicked: console.log(":")
                }
                MouseArea {   //放布局外无效
                    anchors.fill: parent   //充满整个布局,导致覆盖了按钮自身的点击事件,点击按钮无效
                    hoverEnabled: true
                    onEntered: {
                        console.log("enter")
                    }
                    onExited: {
                        console.log("exit")
                    }
                }
            }
        }

    修改后:

    ToolBar {
            RowLayout {
                anchors.fill: parent
                ToolButton {
                    id: toolbutton
                    text: qsTr("")
                    onClicked: console.log("<")
                }
                Label {
                    text: "Title"
                    elide: Label.ElideRight
                    horizontalAlignment: Qt.AlignHCenter
                    verticalAlignment: Qt.AlignVCenter
                    Layout.fillWidth: true
                }
                ToolButton {
                    text: qsTr("")
                    onClicked: console.log(":")
                }
                MouseArea {   //放布局外无效
                    anchors.fill: toolbutton   //充满左边的工具按钮,成为了它的专属按钮事件
                    hoverEnabled: true
                    onEntered: {
                        console.log("enter")
                    }
                    onExited: {
                        console.log("exit")
                    }
                    onClicked: {
                console.log(
    "<") //点击事件有效
             } } } }

    其他:

    ToolBar {
            RowLayout {
                anchors.fill: parent
                ToolButton {
                    id: toolbutton
                    text: qsTr("")
                    onClicked:
                        console.log("<")
                }
                Label {
                    text: "Title"
                    elide: Label.ElideRight
                    horizontalAlignment: Qt.AlignHCenter
                    verticalAlignment: Qt.AlignVCenter
                    Layout.fillWidth: true
                }
                ToolButton {
                    id: toolbutton2
                    text: qsTr("")
                    onClicked:
                        console.log(":")
                }
                MouseArea {   //放布局外无效
                    anchors.fill: toolbutton   //事件无效
                    hoverEnabled: true
                    onEntered: {
                        console.log("enter toolbutton")
                    }
                    onExited: {
                        console.log("exit toolbutton")
                    }
                    onClicked:
                        console.log("< toolbutton")
                }
                MouseArea {   //放布局外无效
                    anchors.fill: toolbutton2   //事件无效
                    hoverEnabled: true
                    onEntered: {
                        console.log("enter toolbutton2")
                    }
                    onExited: {
                        console.log("exit toolbutton2")
                    }
                    onClicked:
                        console.log("< toolbutton2")
                }
                MouseArea {   //放布局外无效
                    anchors.fill: parent   //事件有效
                    hoverEnabled: true
                    onEntered: {
                        console.log("enter")
                    }
                    onExited: {
                        console.log("exit")
                    }
                    onClicked:
                        console.log("<")
                }
            }
        }
    ToolBar {
            RowLayout {
                anchors.fill: parent
                ToolButton {
                    id: toolbutton
                    text: qsTr("")
                    onClicked:
                        console.log("<")
                }
                Label {
                    text: "Title"
                    elide: Label.ElideRight
                    horizontalAlignment: Qt.AlignHCenter
                    verticalAlignment: Qt.AlignVCenter
                    Layout.fillWidth: true
                }
                ToolButton {
                    id: toolbutton2
                    text: qsTr("")
                    onClicked:
                        console.log(":")
                }
                MouseArea {   //放布局外无效
                    anchors.fill: toolbutton   //事件无效
                    hoverEnabled: true
                    onEntered: {
                        console.log("enter toolbutton")
                    }
                    onExited: {
                        console.log("exit toolbutton")
                    }
                    onClicked:
                        console.log("< toolbutton")
                }
                MouseArea {   //放布局外无效
                    anchors.fill: parent   //事件有效
                    hoverEnabled: true
                    onEntered: {
                        console.log("enter")
                    }
                    onExited: {
                        console.log("exit")
                    }
                    onClicked:
                        console.log("<")
                }
                MouseArea {   //放布局外无效
                    anchors.fill: toolbutton2   //事件有效
                    hoverEnabled: true
                    onEntered: {
                        console.log("enter toolbutton2")
                    }
                    onExited: {
                        console.log("exit toolbutton2")
                    }
                    onClicked:
                        console.log("< toolbutton2")
                }
            }
        }
  • 相关阅读:
    张晓涵组《课程设计》结题报告
    20145218张晓涵小组课程设计中期检查
    wireshark使用简介
    20145218张晓涵 web安全基础实践
    20145218张晓涵_Web基础
    2017-2018-1 20155333 《信息安全系统设计基础》第十四周学习总结
    第十六周课堂测试
    2017-2018-1 20155333 《信息安全系统设计基础》第十三周学习总结
    2017-2018-1 20155333 《信息安全系统设计基础》实验五通讯协议设计
    2017-2018-1 20155333 《信息安全系统设计基础》第十一周学习总结
  • 原文地址:https://www.cnblogs.com/tingtaishou/p/14816863.html
Copyright © 2011-2022 走看看