首页 > 其他分享 >Qml 实现仿前端的 Notification (悬浮出现页面上的通知消息)

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

时间:2024-09-12 20:26:22浏览次数:1  
标签:__ anchors parent Notification qsTr Qml root 页面

【写在前面】

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

image

image

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

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

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

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

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

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

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


【正文开始】

先来看看 Qml Notification 效果图:

image

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

    Column {
        anchors.top: parent.top
        anchors.topMargin: 10
        anchors.horizontalCenter: parent.horizontalCenter
        spacing: 10

        Repeater {
            id: repeater
            model: ListModel {
                id: listModel
            }
            delegate: Rectangle {
                width: root.backgroundWidth
                height: __column.height + root.topMargin + root.bottomMargin
                radius: root.backgroundRadius
                color: root.backgroundColor
                clip: true

                Component.onCompleted: {
                    __timer.interval = timeout;
                    __timer.start();
                }

                NumberAnimation on height {
                    id: __removeAniamtion
                    to: 0
                    running: false
                    duration: 500
                    alwaysRunToEnd: true
                    onFinished: {
                        listModel.remove(index);
                    }
                }

                Timer {
                    id: __timer
                    onTriggered: {
                        __removeAniamtion.start();
                    }
                }

                Column {
                    id: __column
                    width: parent.width
                    anchors.centerIn: parent
                    spacing: root.titleSpacing

                    Row {
                        anchors.horizontalCenter: parent.horizontalCenter
                        spacing: 5

                        Text {
                            id: __icon
                            font.family: fontAwesome.name
                            font.pointSize: root.titleFont.pointSize
                            color: {
                                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: __title
                            font: root.titleFont
                            color: root.titleColor
                            text: title
                            wrapMode: Text.WrapAnywhere
                        }
                    }

                    Text {
                        id: __message
                        width: parent.width - 16
                        anchors.horizontalCenter: parent.horizontalCenter
                        font: root.messageFont
                        color: root.messageColor
                        text: message
                        horizontalAlignment: Text.AlignHCenter
                        wrapMode: Text.WrapAnywhere
                    }
                }

                Text {
                    anchors.right: parent.right
                    anchors.top: parent.top
                    anchors.margins: 6
                    text: "×"
                    font.bold: true

                    MouseArea {
                        anchors.fill: parent
                        onClicked: {
                            __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.15

Window {
    width: 800
    height: 600
    visible: true
    title: qsTr("Notification Test")

    Notification {
        id: topNotification
        z: 100
        backgroundWidth: 240
        anchors.top: parent.top
        anchors.horizontalCenter: parent.horizontalCenter
        titleFont.pointSize: 11
        messageFont.pointSize: 11
    }

    Column {
        anchors.centerIn: parent
        spacing: 10

        Row {
            spacing: 10

            Button {
                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.15

Window {
    width: 800
    height: 600
    visible: true
    title: qsTr("Notification Test")

    Page { z: 1 }

    Page { z: 1 }

    Notification {
        id: topNotification
        z: 100
        backgroundWidth: 240
        anchors.top: parent.top
        anchors.horizontalCenter: parent.horizontalCenter
        titleFont.pointSize: 11
        messageFont.pointSize: 11
    }
}

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


【结语】

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

Github 地址:https://github.com/mengps/QmlControls/tree/master/Notification

标签:__,anchors,parent,Notification,qsTr,Qml,root,页面
From: https://www.cnblogs.com/mengps/p/18411003

相关文章

  • pdf删除一页怎么删除?5种方法详细讲解,pdf删除页面实用技巧分享!
    pdf删除一页怎么删除?从pdf文档中删除某页是一项非常实用的技术,特别是在需要编辑pdf文件时。在某些情况下,您可能需要删除页面以保护机密信息、去除不必要的内容,或者为了压缩pdf文件的大小。因此,掌握有效且简单的删除页面的方法非常重要。本指南将为您提供五种实用的方式,并附上详......
  • 面对后台管理系统,全是表格类型的页面,如何快速开发
    项目开始当我们拿到设计稿或者原型图时,看到如下图展示的页面。我们就要想如何能够减少工作量,做出可复用的组件。既然每个页面都长得差不多,那我们观察可以发现,这个页面分成四个部分,【搜索部分、新增部分、表格展示部分、分页部分】由于本人使用的vue3+ts+element-plus+s......
  • pbootcms模板后台登录页面在哪里修改
    在PBootCMS中,如果你想修改后台登录页面的内容,比如文字和链接,可以通过编辑相应的HTML文件来实现。以下是具体的步骤:修改后台登录页面备份文件:在修改任何文件之前,务必先备份相关文件,以防万一操作失误可以恢复。找到登录页面文件:打开你的PBootCMS安装目录,找到apps/admin......
  • HarmonyOS开发之Swiper页面布局
    在HarmonyOSNEXT中使用Swiper组件进行页面布局时,为了提供更好的用户体验,我们可以实现一些自定义的动画效果以及自定义指示器。以下是两个具体的实现方案:场景一:Swiper页面支持自定义动画要实现Swiper页面支持自定义动画,我们需要设置Swiper组件的属性,并添加相应的事件处理程序来控制......
  • pbootcms后台公司信息的内容如何调用到前台页面上
    {pboot:companyname}公司名称{pboot:companyaddress}公司地址{pboot:companypostcode}邮政编码{pboot:companycontact}联系人{pboot:companymobile}联系手机{pboot:companyphone}联系电话{pboot:companyfax}传真号码{pboot:companyemail}联系邮箱......
  • html+css网页设计 旅游 雪花旅行社5个页面
    html+css网页设计旅游雪花旅行社5个页面网页作品代码简单,可使用任意HTML辑软件(如:Dreamweaver、HBuilder、Vscode、Sublime、Webstorm、Text、Notepad++等任意html编辑软件进行运行及修改编辑等操作)。获取源码1,访问该网站https://download.csdn.net/download/qq_42......
  • 微信小程序开发系列7----页面配置--WXML的include用法
       传递变量   模板不能引用 ......
  • 五星级可视化页面(04):城市鸟瞰地图,恢宏大气。
    今天继续分享五星级可视化大屏界面,本期分享城市3D鸟瞰图的,非常的恢宏大气。  ......
  • 五星级可视化页面(15):各类医疗场景下大屏页面
    可视化大屏在医疗领域有许多重要的价值和应用:1.数据监控和实时展示:可视化大屏可以用于监控医疗设备、患者数据、手术过程等,实时展示医疗数据的变化和趋势,帮助医护人员及时发现异常情况并做出相应的处理。2.医院运营管理:可视化大屏可以展示医院的运营数据,包括门诊量、......
  • HTML静态网页作业(HTML+CSS+JS)——蜡笔小新动漫网页设计制作(5个页面)
    ......