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")
            }
        }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM