Qml 实现仿前端的 Notification (悬浮出现页面上的关照消息) ...

打印 上一主题 下一主题

主题 857|帖子 857|积分 2571

【写在前面】

        常常打仗前端的朋友应该常常见到下面的控件:


        在前端中一样平常称它为 Notification 或 Message,但本质是一种东西,即:悬浮弹出式的消息提示框。
        这种组件一样平常具有以下特点:
        1、全局/局部体现:它不依靠于具体的页面元素,可以在整个页面的任意位置体现。
        2、主动消失:默认环境下,消息会在一定时间后主动消失,也可以设置为不主动消失。
        3、多种范例:支持多种范例的消息,如成功(Success)、警告(Warning)、错误(Error)和 消息(Message)等。
        4、可配置:可以自定义消息的体现位置、持续时间、内容等。
        然鹅 Qml 中并未提供雷同的组件,因此我便仿照前端实现了出来,并且更加简单易用。

【正文开始】

        先来看看 Qml Notification 效果图:

         实现起来相称简单,只须要 Column + Repeater 即可:
  1.     Column {
  2.         anchors.top: parent.top
  3.         anchors.topMargin: 10
  4.         anchors.horizontalCenter: parent.horizontalCenter
  5.         spacing: 10
  6.         Repeater {
  7.             id: repeater
  8.             model: ListModel {
  9.                 id: listModel
  10.             }
  11.             delegate: Rectangle {
  12.                 width: root.backgroundWidth
  13.                 height: __column.height + root.topMargin + root.bottomMargin
  14.                 radius: root.backgroundRadius
  15.                 color: root.backgroundColor
  16.                 clip: true
  17.                 Component.onCompleted: {
  18.                     __timer.interval = timeout;
  19.                     __timer.start();
  20.                 }
  21.                 NumberAnimation on height {
  22.                     id: __removeAniamtion
  23.                     to: 0
  24.                     running: false
  25.                     duration: 500
  26.                     alwaysRunToEnd: true
  27.                     onFinished: {
  28.                         listModel.remove(index);
  29.                     }
  30.                 }
  31.                 Timer {
  32.                     id: __timer
  33.                     onTriggered: {
  34.                         __removeAniamtion.start();
  35.                     }
  36.                 }
  37.                 Column {
  38.                     id: __column
  39.                     width: parent.width
  40.                     anchors.centerIn: parent
  41.                     spacing: root.titleSpacing
  42.                     Row {
  43.                         anchors.horizontalCenter: parent.horizontalCenter
  44.                         spacing: 5
  45.                         Text {
  46.                             id: __icon
  47.                             font.family: fontAwesome.name
  48.                             font.pointSize: root.titleFont.pointSize
  49.                             color: {
  50.                                 switch (type) {
  51.                                 case Notification.Success: return "green";
  52.                                 case Notification.Warning: return "orange";
  53.                                 case Notification.Message: return "gray";
  54.                                 case Notification.Error: return "red";
  55.                                 default: return "";
  56.                                 }
  57.                             }
  58.                             text: {
  59.                                 switch (type) {
  60.                                 case Notification.Success: return "\uf058";
  61.                                 case Notification.Warning: return "\uf071";
  62.                                 case Notification.Message: return "\uf05a";
  63.                                 case Notification.Error: return "\uf057";
  64.                                 default: return "";
  65.                                 }
  66.                             }
  67.                         }
  68.                         Text {
  69.                             id: __title
  70.                             font: root.titleFont
  71.                             color: root.titleColor
  72.                             text: title
  73.                             wrapMode: Text.WrapAnywhere
  74.                         }
  75.                     }
  76.                     Text {
  77.                         id: __message
  78.                         width: parent.width - 16
  79.                         anchors.horizontalCenter: parent.horizontalCenter
  80.                         font: root.messageFont
  81.                         color: root.messageColor
  82.                         text: message
  83.                         horizontalAlignment: Text.AlignHCenter
  84.                         wrapMode: Text.WrapAnywhere
  85.                     }
  86.                 }
  87.                 Text {
  88.                     anchors.right: parent.right
  89.                     anchors.top: parent.top
  90.                     anchors.margins: 6
  91.                     text: "×"
  92.                     font.bold: true
  93.                     MouseArea {
  94.                         anchors.fill: parent
  95.                         onClicked: {
  96.                             __timer.stop();
  97.                             __removeAniamtion.restart();
  98.                         }
  99.                     }
  100.                 }
  101.             }
  102.         }
  103.     }
复制代码
         然后使用 notify() 来添加关照消息:
  1.     function notify(title, message, type = Notification.None, timeout = 3000) {
  2.         listModel.append({
  3.                              title: title,
  4.                              message: message,
  5.                              type: type,
  6.                              timeout: timeout
  7.                          });
  8.     }
复制代码
        其中参数说明:
        title:标题,即关照顶端的标题。
        message:消息,即关照中间的内容。
        type:范例,即该关照的范例。
        timeout:超时,即该关照体现的时长,-1 则是无穷。

【怎样使用】

  1. import QtQuick 2.15
  2. import QtQuick.Controls 2.15
  3. import QtQuick.Window 2.15
  4. Window {
  5.     width: 800
  6.     height: 600
  7.     visible: true
  8.     title: qsTr("Notification Test")
  9.     Notification {
  10.         id: topNotification
  11.         z: 100
  12.         backgroundWidth: 240
  13.         anchors.top: parent.top
  14.         anchors.horizontalCenter: parent.horizontalCenter
  15.         titleFont.pointSize: 11
  16.         messageFont.pointSize: 11
  17.     }
  18.     Column {
  19.         anchors.centerIn: parent
  20.         spacing: 10
  21.         Row {
  22.             spacing: 10
  23.             Button {
  24.                 text: qsTr("成功")
  25.                 onClicked: {
  26.                     topNotification.notify(qsTr("成功"), qsTr("这是一条成功的提示消息"), Notification.Success);
  27.                 }
  28.             }
  29.             Button {
  30.                 text: qsTr("警告")
  31.                 onClicked: {
  32.                     topNotification.notify(qsTr("警告"), qsTr("这是一条警告的提示消息"), Notification.Warning);
  33.                 }
  34.             }
  35.             Button {
  36.                 text: qsTr("消息")
  37.                 onClicked: {
  38.                     topNotification.notify(qsTr("消息"), qsTr("这是一条消息的提示消息"), Notification.Message);
  39.                 }
  40.             }
  41.             Button {
  42.                 text: qsTr("错误")
  43.                 onClicked: {
  44.                     topNotification.notify(qsTr("错误"), qsTr("这是一条错误的提示消息"), Notification.Error);
  45.                 }
  46.             }
  47.         }
  48.     }
  49. }
复制代码
        Notification 可放置在任意位置,然后设置字体配景等等即可。
        固然,这种方式是悬浮在当前页面的,如果想要悬浮在全局页面,则必须将其置于主窗口的顶部,具体方法如下:
  1. import QtQuick 2.15
  2. import QtQuick.Controls 2.15
  3. import QtQuick.Window 2.15
  4. Window {
  5.     width: 800
  6.     height: 600
  7.     visible: true
  8.     title: qsTr("Notification Test")
  9.     Page { z: 1 }
  10.     Page { z: 1 }
  11.     Notification {
  12.         id: topNotification
  13.         z: 100
  14.         backgroundWidth: 240
  15.         anchors.top: parent.top
  16.         anchors.horizontalCenter: parent.horizontalCenter
  17.         titleFont.pointSize: 11
  18.         messageFont.pointSize: 11
  19.     }
  20. }
复制代码
         须要包管其他页面 z-order 小于 Notification 组件。

【结语】

        末了:项目链接(多多star呀..⭐_⭐):
        Github 地址:
QmlControls/Notification at master · mengps/QmlControls · GitHubQtQml 控件 & 实用工具. Contribute to mengps/QmlControls development by creating an account on GitHub.
https://github.com/mengps/QmlControls/tree/master/Notification        CSDN 的:
https://download.csdn.net/download/u011283226/89662116
https://download.csdn.net/download/u011283226/89662116

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

何小豆儿在此

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表