Qml 实现仿前端的 Notification (悬浮出现页面上的通知消息)

2024-08-23 08:28

本文主要是介绍Qml 实现仿前端的 Notification (悬浮出现页面上的通知消息),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

【写在前面】

        经常接触前端的朋友应该经常见到下面的控件:

        在前端中一般称它为 Notification 或 Message,但本质是一种东西,即:悬浮弹出式的消息提醒框

        这种组件一般具有以下特点:

        1、全局/局部显示:它不依赖于具体的页面元素,可以在整个页面的任意位置显示。

        2、自动消失:默认情况下,消息会在一定时间后自动消失,也可以设置为不自动消失。

        3、多种类型:支持多种类型的消息,如成功(Success)、警告(Warning)、错误(Error)和 消息(Message)等。

        4、可配置:可以自定义消息的显示位置、持续时间、内容等。

        然鹅 Qml 中并未提供类似的组件,因此我便仿照前端实现了出来,并且更加简单易用。


【正文开始】

        先来看看 Qml Notification 效果图:

         实现起来相当简单,只需要 Column + Repeater 即可:

    Column {anchors.top: parent.topanchors.topMargin: 10anchors.horizontalCenter: parent.horizontalCenterspacing: 10Repeater {id: repeatermodel: ListModel {id: listModel}delegate: Rectangle {width: root.backgroundWidthheight: __column.height + root.topMargin + root.bottomMarginradius: root.backgroundRadiuscolor: root.backgroundColorclip: trueComponent.onCompleted: {__timer.interval = timeout;__timer.start();}NumberAnimation on height {id: __removeAniamtionto: 0running: falseduration: 500alwaysRunToEnd: trueonFinished: {listModel.remove(index);}}Timer {id: __timeronTriggered: {__removeAniamtion.start();}}Column {id: __columnwidth: parent.widthanchors.centerIn: parentspacing: root.titleSpacingRow {anchors.horizontalCenter: parent.horizontalCenterspacing: 5Text {id: __iconfont.family: fontAwesome.namefont.pointSize: root.titleFont.pointSizecolor: {switch (type) {case Notification.Success: return "green";case Notification.Warning: return "orange";case Notification.Message: return "gray";case Notification.Error: return "red";default: return "";}}text: {switch (type) {case Notification.Success: return "\uf058";case Notification.Warning: return "\uf071";case Notification.Message: return "\uf05a";case Notification.Error: return "\uf057";default: return "";}}}Text {id: __titlefont: root.titleFontcolor: root.titleColortext: titlewrapMode: Text.WrapAnywhere}}Text {id: __messagewidth: parent.width - 16anchors.horizontalCenter: parent.horizontalCenterfont: root.messageFontcolor: root.messageColortext: messagehorizontalAlignment: Text.AlignHCenterwrapMode: Text.WrapAnywhere}}Text {anchors.right: parent.rightanchors.top: parent.topanchors.margins: 6text: "×"font.bold: trueMouseArea {anchors.fill: parentonClicked: {__timer.stop();__removeAniamtion.restart();}}}}}}

         然后使用 notify() 来添加通知消息:

    function notify(title, message, type = Notification.None, timeout = 3000) {listModel.append({title: title,message: message,type: type,timeout: timeout});}

        其中参数说明:

        title:标题,即通知顶端的标题。

        message:消息,即通知中间的内容。

        type:类型,即该通知的类型。

        timeout:超时,即该通知显示的时长,-1 则是无限。


【如何使用】

import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Window 2.15Window {width: 800height: 600visible: truetitle: qsTr("Notification Test")Notification {id: topNotificationz: 100backgroundWidth: 240anchors.top: parent.topanchors.horizontalCenter: parent.horizontalCentertitleFont.pointSize: 11messageFont.pointSize: 11}Column {anchors.centerIn: parentspacing: 10Row {spacing: 10Button {text: qsTr("成功")onClicked: {topNotification.notify(qsTr("成功"), qsTr("这是一条成功的提示消息"), Notification.Success);}}Button {text: qsTr("警告")onClicked: {topNotification.notify(qsTr("警告"), qsTr("这是一条警告的提示消息"), Notification.Warning);}}Button {text: qsTr("消息")onClicked: {topNotification.notify(qsTr("消息"), qsTr("这是一条消息的提示消息"), Notification.Message);}}Button {text: qsTr("错误")onClicked: {topNotification.notify(qsTr("错误"), qsTr("这是一条错误的提示消息"), Notification.Error);}}}}
}

        Notification 可放置在任意位置,然后设置字体背景等等即可。

        当然,这种方式是悬浮在当前页面的,如果想要悬浮在全局页面,则必须将其置于主窗口的顶部,具体方法如下:

import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Window 2.15Window {width: 800height: 600visible: truetitle: qsTr("Notification Test")Page { z: 1 }Page { z: 1 }Notification {id: topNotificationz: 100backgroundWidth: 240anchors.top: parent.topanchors.horizontalCenter: parent.horizontalCentertitleFont.pointSize: 11messageFont.pointSize: 11}
}

         需要保证其他页面 z-order 小于 Notification 组件。


【结语】

        最后:项目链接(多多star呀..⭐_⭐):

        Github 地址:

QmlControls/Notification at master · mengps/QmlControls · GitHubQtQml 控件 & 实用工具. Contribute to mengps/QmlControls development by creating an account on GitHub.icon-default.png?t=N7T8https://github.com/mengps/QmlControls/tree/master/Notification        CSDN 的:

https://download.csdn.net/download/u011283226/89662116icon-default.png?t=N7T8https://download.csdn.net/download/u011283226/89662116

这篇关于Qml 实现仿前端的 Notification (悬浮出现页面上的通知消息)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1098861

相关文章

SpringKafka消息发布之KafkaTemplate与事务支持功能

《SpringKafka消息发布之KafkaTemplate与事务支持功能》通过本文介绍的基本用法、序列化选项、事务支持、错误处理和性能优化技术,开发者可以构建高效可靠的Kafka消息发布系统,事务支... 目录引言一、KafkaTemplate基础二、消息序列化三、事务支持机制四、错误处理与重试五、性能优

SpringIntegration消息路由之Router的条件路由与过滤功能

《SpringIntegration消息路由之Router的条件路由与过滤功能》本文详细介绍了Router的基础概念、条件路由实现、基于消息头的路由、动态路由与路由表、消息过滤与选择性路由以及错误处理... 目录引言一、Router基础概念二、条件路由实现三、基于消息头的路由四、动态路由与路由表五、消息过滤

Springboot处理跨域的实现方式(附Demo)

《Springboot处理跨域的实现方式(附Demo)》:本文主要介绍Springboot处理跨域的实现方式(附Demo),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不... 目录Springboot处理跨域的方式1. 基本知识2. @CrossOrigin3. 全局跨域设置4.

Spring Boot 3.4.3 基于 Spring WebFlux 实现 SSE 功能(代码示例)

《SpringBoot3.4.3基于SpringWebFlux实现SSE功能(代码示例)》SpringBoot3.4.3结合SpringWebFlux实现SSE功能,为实时数据推送提供... 目录1. SSE 简介1.1 什么是 SSE?1.2 SSE 的优点1.3 适用场景2. Spring WebFlu

基于SpringBoot实现文件秒传功能

《基于SpringBoot实现文件秒传功能》在开发Web应用时,文件上传是一个常见需求,然而,当用户需要上传大文件或相同文件多次时,会造成带宽浪费和服务器存储冗余,此时可以使用文件秒传技术通过识别重复... 目录前言文件秒传原理代码实现1. 创建项目基础结构2. 创建上传存储代码3. 创建Result类4.

SpringBoot日志配置SLF4J和Logback的方法实现

《SpringBoot日志配置SLF4J和Logback的方法实现》日志记录是不可或缺的一部分,本文主要介绍了SpringBoot日志配置SLF4J和Logback的方法实现,文中通过示例代码介绍的非... 目录一、前言二、案例一:初识日志三、案例二:使用Lombok输出日志四、案例三:配置Logback一

Python如何使用__slots__实现节省内存和性能优化

《Python如何使用__slots__实现节省内存和性能优化》你有想过,一个小小的__slots__能让你的Python类内存消耗直接减半吗,没错,今天咱们要聊的就是这个让人眼前一亮的技巧,感兴趣的... 目录背景:内存吃得满满的类__slots__:你的内存管理小助手举个大概的例子:看看效果如何?1.

Python+PyQt5实现多屏幕协同播放功能

《Python+PyQt5实现多屏幕协同播放功能》在现代会议展示、数字广告、展览展示等场景中,多屏幕协同播放已成为刚需,下面我们就来看看如何利用Python和PyQt5开发一套功能强大的跨屏播控系统吧... 目录一、项目概述:突破传统播放限制二、核心技术解析2.1 多屏管理机制2.2 播放引擎设计2.3 专

Python实现无痛修改第三方库源码的方法详解

《Python实现无痛修改第三方库源码的方法详解》很多时候,我们下载的第三方库是不会有需求不满足的情况,但也有极少的情况,第三方库没有兼顾到需求,本文将介绍几个修改源码的操作,大家可以根据需求进行选择... 目录需求不符合模拟示例 1. 修改源文件2. 继承修改3. 猴子补丁4. 追踪局部变量需求不符合很

idea中创建新类时自动添加注释的实现

《idea中创建新类时自动添加注释的实现》在每次使用idea创建一个新类时,过了一段时间发现看不懂这个类是用来干嘛的,为了解决这个问题,我们可以设置在创建一个新类时自动添加注释,帮助我们理解这个类的用... 目录前言:详细操作:步骤一:点击上方的 文件(File),点击&nbmyHIgsp;设置(Setti