QML 解决button中添加了MouseArea,点击事件就不响应了的问题

摘要:
ToolBar{RowLayout{anchors.fill:parentToolButton{id:toolbuttontext:qsTr("")onClicked:console.log("˂")}Label{text:"Title"elide:Label.ElideRighthorizontalAlignment:Qt.AlignHCenterverticalAlignment:Qt.Ali
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: trueonEntered: {
                    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: trueonEntered: {
                    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: trueonEntered: {
                    console.log("enter toolbutton")
                }
                onExited: {
                    console.log("exit toolbutton")
                }
                onClicked:
                    console.log("< toolbutton")
            }
            MouseArea {   //放布局外无效
                anchors.fill: toolbutton2   //事件无效
                hoverEnabled: trueonEntered: {
                    console.log("enter toolbutton2")
                }
                onExited: {
                    console.log("exit toolbutton2")
                }
                onClicked:
                    console.log("< toolbutton2")
            }
            MouseArea {   //放布局外无效
anchors.fill: parent   //事件有效
                hoverEnabled: trueonEntered: {
                    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: trueonEntered: {
                    console.log("enter toolbutton")
                }
                onExited: {
                    console.log("exit toolbutton")
                }
                onClicked:
                    console.log("< toolbutton")
            }
            MouseArea {   //放布局外无效
                anchors.fill: parent   //事件有效
                hoverEnabled: trueonEntered: {
                    console.log("enter")
                }
                onExited: {
                    console.log("exit")
                }
                onClicked:
                    console.log("<")
            }
            MouseArea {   //放布局外无效
                anchors.fill: toolbutton2   //事件有效
                hoverEnabled: trueonEntered: {
                    console.log("enter toolbutton2")
                }
                onExited: {
                    console.log("exit toolbutton2")
                }
                onClicked:
                    console.log("< toolbutton2")
            }
        }
    }

免责声明:文章转载自《QML 解决button中添加了MouseArea,点击事件就不响应了的问题》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇《QT Creator快速入门》第十一章(一):图形视图linux系统tomcat部署SpringBoot+vue前后端分离项目正式部署下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

QML动态创建自定义组件

一、动态加载和实例化对象:createComponent 例子1: ①、创建本地的QML文件,注意文件第一个字母要大写 ZStation.qml import QtQuick 2.5 Rectangle{ property string mName: "station" signal entered(string objName); sig...

qml focus相关

Item的属性: read-only activeFocus : bool This property indicates whether the item has active focus. An item with active focus will receive keyboard input, or is a FocusScope ancestor...

Qt Widgets、QML、Qt Quick的区别

一、QML 与 Qt Quick的区别 1.1 从概念上区分 为了更精确地对两者进行说明,先看助手对 QML 的描述: QML is a user interface specification and programming language. QML 是一种用户界面规范和标记语言,允许开发人员和设计师创建高性能、流畅的动画和视觉吸引人的应用程序。 文...

QML基本可视化元素--Text

一个Text项目可以显示纯文本或者富文本 1.     可以使用Html标记:text: “<b>HELLO</b>” 2.     宽度和高度(width, height):没有指定,会自己尝试需要多大的空间并依此来设置。 3.     换行:wrapMode属性,没有设置,则单行 4.     省略:elide属性。宽度不足显示...

Hbuilder快捷键

headier 头部导航 mbody  内容区域 mtab     底部导航 mlist      创建列表 mform    创建表单元素 cmd + shift + f 格式化代码 Shift+回车    换行(产生<br />标签) Ctrl+回车    换行编辑(代码中产生新的一行,光标调到新的行) Ctrl+d    删除一行  ...

vue按enter键刷新页面 使用@submit.native.prevent阻止表单默认提交,添加在form标签上

情景描述:当使用@keyup.enter.native来使输入搜索内容后,按下enter键就触发搜索。会发现一个奇怪的现象:当按下enter键时,先是根据输入的内容搜索了一次,紧接着又全局刷新了一次页面。 原因分析:当一个表单下,如果只有一个文本框时,按下回车将会触发表单的提交事件。 原代码如下: <el-form :inline="true" :...