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

相关文章

C++中unordered_set哈希集合的实现

《C++中unordered_set哈希集合的实现》std::unordered_set是C++标准库中的无序关联容器,基于哈希表实现,具有元素唯一性和无序性特点,本文就来详细的介绍一下unorder... 目录一、概述二、头文件与命名空间三、常用方法与示例1. 构造与析构2. 迭代器与遍历3. 容量相关4

C++中悬垂引用(Dangling Reference) 的实现

《C++中悬垂引用(DanglingReference)的实现》C++中的悬垂引用指引用绑定的对象被销毁后引用仍存在的情况,会导致访问无效内存,下面就来详细的介绍一下产生的原因以及如何避免,感兴趣... 目录悬垂引用的产生原因1. 引用绑定到局部变量,变量超出作用域后销毁2. 引用绑定到动态分配的对象,对象

SpringBoot基于注解实现数据库字段回填的完整方案

《SpringBoot基于注解实现数据库字段回填的完整方案》这篇文章主要为大家详细介绍了SpringBoot如何基于注解实现数据库字段回填的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以了解... 目录数据库表pom.XMLRelationFieldRelationFieldMapping基础的一些代

Java HashMap的底层实现原理深度解析

《JavaHashMap的底层实现原理深度解析》HashMap基于数组+链表+红黑树结构,通过哈希算法和扩容机制优化性能,负载因子与树化阈值平衡效率,是Java开发必备的高效数据结构,本文给大家介绍... 目录一、概述:HashMap的宏观结构二、核心数据结构解析1. 数组(桶数组)2. 链表节点(Node

Java AOP面向切面编程的概念和实现方式

《JavaAOP面向切面编程的概念和实现方式》AOP是面向切面编程,通过动态代理将横切关注点(如日志、事务)与核心业务逻辑分离,提升代码复用性和可维护性,本文给大家介绍JavaAOP面向切面编程的概... 目录一、AOP 是什么?二、AOP 的核心概念与实现方式核心概念实现方式三、Spring AOP 的关

vite搭建vue3项目的搭建步骤

《vite搭建vue3项目的搭建步骤》本文主要介绍了vite搭建vue3项目的搭建步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录1.确保Nodejs环境2.使用vite-cli工具3.进入项目安装依赖1.确保Nodejs环境

Nginx搭建前端本地预览环境的完整步骤教学

《Nginx搭建前端本地预览环境的完整步骤教学》这篇文章主要为大家详细介绍了Nginx搭建前端本地预览环境的完整步骤教学,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录项目目录结构核心配置文件:nginx.conf脚本化操作:nginx.shnpm 脚本集成总结:对前端的意义很多

Python实现字典转字符串的五种方法

《Python实现字典转字符串的五种方法》本文介绍了在Python中如何将字典数据结构转换为字符串格式的多种方法,首先可以通过内置的str()函数进行简单转换;其次利用ison.dumps()函数能够... 目录1、使用json模块的dumps方法:2、使用str方法:3、使用循环和字符串拼接:4、使用字符

Linux下利用select实现串口数据读取过程

《Linux下利用select实现串口数据读取过程》文章介绍Linux中使用select、poll或epoll实现串口数据读取,通过I/O多路复用机制在数据到达时触发读取,避免持续轮询,示例代码展示设... 目录示例代码(使用select实现)代码解释总结在 linux 系统里,我们可以借助 select、

Linux挂载linux/Windows共享目录实现方式

《Linux挂载linux/Windows共享目录实现方式》:本文主要介绍Linux挂载linux/Windows共享目录实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录文件共享协议linux环境作为服务端(NFS)在服务器端安装 NFS创建要共享的目录修改 NFS 配