Qml实用技巧:在可视元素之前半透明覆盖一个可视元素,阻止鼠标透(界面)传(防止点击到被遮挡的按钮)

简介: Qml实用技巧:在可视元素之前半透明覆盖一个可视元素,阻止鼠标透(界面)传(防止点击到被遮挡的按钮)

需求

       需要一个半遮挡的界面,遮挡住原来的界面,因为把用来被遮挡的界面写成了组件,所以将其设置为需要遮挡的界面的子对象

BUG

       每次点击后面和前面的按钮都可以触发,如下:

Item {
    Rectangle {
        width: 200;
        height: 200;
        color: "yellow";
        Button {
            anchors.top: parent.top;
            text: "bottom";
            onClicked: {
                console.log("onClicked bottom");
            }
        }
        // 本人把遮挡的部分当作组件写成单独的文件了,用的时候当作需要被遮挡的子对象
        Rectangle {
            anchors.fill: parent;
            color: "#80000000"
            Button {
                text:"top";
                anchors.bottom: parent.bottom;
                onClicked: {
                    console.log("onClicked top");
                }
            }
        }
    }
}

图片.png

解决方法一(笔者推荐方法)

       改变MouseArea范围,如下:

Item {
    Rectangle {
        width: 200;
        height: 200;
        color: "yellow";
        Button {
            anchors.top: parent.top;
            text: "bottom";
            onClicked: {
                console.log("onClicked bottom");
            }
        }
        // 本人把遮挡的部分当作组件写成单独的文件了,用的时候当作需要被遮挡的子对象
        Rectangle {
            anchors.fill: parent;
            color: "#80000000"
            MouseArea {                 // 新增代码
                anchors.fill: parent;   // 新增代码
                Button {
                    text:"top";
                    anchors.bottom: parent.bottom;
                    onClicked: {
                        console.log("onClicked top");
                    }
                }
            }                           // 新增代码
        }
    }
}

图片.png

解决方法二

       使底层disable,这里特别要注意,如果父元素被disable那么子元素就会disable(单独设置enable也无效),所以不能使用父子关系

Item {
    Rectangle {
        id: bottomButton;
        width: 200;
        height: 200;
        color: "yellow";
        Button {
            anchors.top: parent.top;
            text: "bottom";
            onClicked: {
                console.log("onClicked bottom");
                bottomButton.enabled = false;
            }
        }
        // 本人把遮挡的部分当作组件写成单独的文件了,用的时候当作需要被遮挡的子对象
    }
    Rectangle {
        anchors.fill: bottomButton;
        color: "#80000000"
//        MouseArea {                 // 新增代码
//            anchors.fill: parent;   // 新增代码
            Button {
                text:"top";
                anchors.bottom: parent.bottom;
                onClicked: {
                    console.log("onClicked top");
                }
            }
//        }                           // 新增代码
    }
}

图片.png






相关文章
|
Linux API Windows
linux系统中利用QT实现蓝牙功能的方法
linux系统中利用QT实现蓝牙功能的方法
1514 0
|
XML 物联网 API
Android Ble蓝牙App(五)数据操作
Android Ble蓝牙App(五)数据操作
2511 0
|
Ubuntu
ubuntu 替换清华源遇到的问题-不能更新,无法拉取 https 源解决
ubuntu 替换清华源遇到的问题-不能更新,无法拉取 https 源解决
7893 0
ubuntu 替换清华源遇到的问题-不能更新,无法拉取 https 源解决
|
数据可视化 Linux Go
Docker容器命令行可视化工具-Lazydocker
Lazydocker-专为懒人设计的容器命令行可视化交互式工具
2248 0
Docker容器命令行可视化工具-Lazydocker
|
机器学习/深度学习 存储 并行计算
深度学习之声纹识别
基于深度学习的声纹识别(Speaker Recognition)是一种通过分析和识别人的声音特征来确认身份的技术。
3704 2
|
存储 开发者
GitHub CLI - 将GitHub带入命令行
本文介绍了 GitHub CLI(命令行界面)的基本概念及其主要功能,重点讲解如何通过命令行管理 GitHub 上的问题和拉取请求。文章详细列出了与问题和拉取请求相关的常用命令,包括列出、查看、创建和检查状态等操作,旨在帮助开发者提高工作效率,优化日常开发流程。
1228 2
GitHub CLI - 将GitHub带入命令行
|
Ubuntu Linux C语言
用C写蓝牙通讯程序:扫描、读取、发送
在Linux下,通过bluez 蓝牙库可以用C语言轻松实现蓝牙通信。在ubuntu下可以用 apt-get install libbluetooth-dev 安装该库。下面是几个简单示例。
2812 0
|
Shell Go
通过安装GVM 安装GO 操作步骤
通过安装GVM 安装GO 操作步骤
1223 0
|
监控 Ubuntu Linux
使用`dbus-monitor`命令深入了解DBus通信
`dbus-monitor`是Linux下用于观察DBus消息的命令行工具,有助于理解和调试应用程序间的通信。安装该工具可通过发行版的包管理器,如`apt-get`、`dnf`或`pacman`。基本用法是运行`dbus-monitor`监控系统总线,使用`--session`监控会话总线,配合过滤器如`sender`、`interface`等精细化查看特定消息。当面临DBus相关问题时,此工具能辅助排查和诊断。
|
前端开发 C++ Windows
C++生成QML代码与QML里面集成QWidget
这篇文章介绍了如何在C++中生成QML代码,以及如何在QML中集成QWidget,包括使用Qt Widgets嵌入到QML界面中的技术示例。
988 0