HusNotification 通知提醒框

January 26, 2026 · View on GitHub

← 返回主目录

← 返回本类别目录

HusNotification 通知提醒框

全局/页面内展示通知提醒信息。

  • 模块 { HuskarUI.Basic }

  • 继承自 { Item }


支持的代理:

  • messageDelegate: Component 通知消息代理:

    • parent.index: int 通知索引

    • parent.key: string 通知键

    • parent.message: string 通知消息文本

  • descriptionDelegate: Component 通知描述代理:

    • parent.index: int 通知索引

    • parent.key: string 通知键

    • parent.description: string 通知描述文本


支持的属性:

属性名类型默认值描述
animationEnabledboolHusTheme.animationEnabled是否开启动画
positionenumHusNotification.Position_Top通知显示的位置(来自HusNotification)
pauseOnHoverbooltrue是否悬浮暂停超时
showProgressboolfalse是否显示进度条
stackModebooltrue堆叠模式(超过stackThreshold自动堆叠)
stackThresholdint5堆叠阈值
defaultIconSizeint20默认图标大小
maxNotificationWidthint300通知框最大宽度
spacingint10通知之间的间隔
showCloseButtonboolfalse是否显示关闭按钮
topMarginint12通知距离顶端的距离
bgTopPaddingint12背景上部填充
bgBottomPaddingint12背景下部填充
bgLeftPaddingint12背景左部填充
bgRightPaddingint12背景右部填充
colorMessagecolor-通知消息文本颜色
colorDescriptioncolor-通知描述文本颜色
colorBgcolor-背景颜色
colorBgShadowcolor-背景阴影颜色
radiusBgHusRadius-背景半径
messageFontfont-通知消息字体
messageSpacingint8通知文本和图标之间的间隔
descriptionFontfont-通知描述字体
descriptionSpacingint10消息和描述之间的间隔

{notification}支持的属性:

属性名类型可选/必选描述
keystring可选通知键
loadingsting可选是否加载中
messagestring可选通知文本
typeint可选通知类型(来自 HusNotification)
durationint可选通知持续时间(默认4500ms)
iconSizeint可选通知图标大小
iconSourceint可选通知图标
colorIconcolor可选通知图标颜色

支持的函数:

  • info(message: string, description: string, duration = 4500) 弹出一条 info 通知。

  • success(message: string, description: string, duration = 4500) 弹出一条 success 通知。

  • error(message: string, description: string, duration = 4500) 弹出一条 error 通知。

  • warning(message: string, description: string, duration = 4500) 弹出一条 warning 通知。

  • loading(message: string, description: string, duration = 4500) 弹出一条 loading 通知。

  • open(object: var) 弹出一条通知体为 {object} 的通知。

  • close(key: string) 关闭一条通知键为 key 的通知。

  • clear() 清空所有通知。

  • getNotification(key: string): object 获取通知键为 key 的通知对象 object

  • setProperty(key: string, property: string, value: var) 设置通知键为 key 的属性 property 的值为 value


支持的信号:

  • closed(key: string) 通知关闭时发出

    • key 通知键

代码演示

示例 1 - 基本用法

一般通知,注意 推荐通过将 parent 设置为窗口覆盖层 Overlay.overlay 从而将其置于顶层。

import QtQuick
import QtQuick.Controls.Basic
import HuskarUI.Basic

Item {
    width: parent.width

    HusNotification {
        id: notification1
        parent: Overlay.overlay
        anchors.fill: parent
        anchors.topMargin: captionBar.height
        position: HusNotification.Position_TopLeft
    }

    HusNotification {
        id: notification2
        parent: Overlay.overlay
        anchors.fill: parent
        anchors.topMargin: captionBar.height
        position: HusNotification.Position_TopRight
    }

    HusNotification {
        id: notification3
        parent: Overlay.overlay
        anchors.fill: parent
        anchors.topMargin: captionBar.height
        position: HusNotification.Position_BottomLeft
    }

    HusNotification {
        id: notification4
        parent: Overlay.overlay
        anchors.fill: parent
        anchors.topMargin: captionBar.height
        position: HusNotification.Position_BottomRight
    }

    HusNotification {
        id: notification5
        parent: Overlay.overlay
        anchors.fill: parent
        anchors.topMargin: captionBar.height
        position: HusNotification.Position_Top
    }

    HusNotification {
        id: notification6
        parent: Overlay.overlay
        anchors.fill: parent
        anchors.topMargin: captionBar.height
        position: HusNotification.Position_Bottom
    }

    Grid {
        rows: 3
        columns: 2
        spacing: 10

        HusIconButton {
            type: HusButton.Type_Primary
            iconSource: HusIcon.RadiusUpleftOutlined
            text: 'TopLeft'
            onClicked: {
                notification1.info('Notification TopLeft', 'Hello, HuskarUI!');
            }
        }

        HusIconButton {
            type: HusButton.Type_Primary
            iconSource: HusIcon.RadiusUprightOutlined
            text: 'TopRight'
            onClicked: {
                notification2.info('Notification TopRight', 'Hello, HuskarUI!');
            }
        }

        HusIconButton {
            type: HusButton.Type_Primary
            iconSource: HusIcon.RadiusBottomleftOutlined
            text: 'BottomLeft'
            onClicked: {
                notification3.info('Notification BottomLeft', 'Hello, HuskarUI!');
            }
        }

        HusIconButton {
            type: HusButton.Type_Primary
            iconSource: HusIcon.RadiusBottomrightOutlined
            text: 'BottomRight'
            onClicked: {
                notification4.info('Notification BottomRight', 'Hello, HuskarUI!');
            }
        }

        HusIconButton {
            type: HusButton.Type_Primary
            iconSource: HusIcon.BorderTopOutlined
            text: 'Top'
            onClicked: {
                notification5.info('Notification Top', 'Hello, HuskarUI!');
            }
        }

        HusIconButton {
            type: HusButton.Type_Primary
            iconSource: HusIcon.BorderBottomOutlined
            text: 'Bottom'
            onClicked: {
                notification6.info('Notification Bottom', 'Hello, HuskarUI!');
            }
        }
    }
}

示例 2 - 自动关闭的延时

自定义通知框自动关闭的延时,通过 duration 属性设置持续时间,默认 4.5s。

import QtQuick
import QtQuick.Controls.Basic
import HuskarUI.Basic

Item {
    width: parent.width
    HusNotification {
        id: notification7
        parent: Overlay.overlay
        anchors.fill: parent
        anchors.topMargin: captionBar.height
        position: HusNotification.Position_TopRight
    }

    HusButton {
        type: HusButton.Type_Primary
        text: 'Open the notification box'
        onClicked: {
            notification7.info('Notification Title', 'I will never close automatically. This is a purposely very very long description that has many many characters and words.', 99999999);
        }
    }
}

示例 3 - 其他提示通知类型

包括成功、信息、失败、警告。

import QtQuick
import QtQuick.Controls.Basic
import HuskarUI.Basic

Row {
    width: parent.width
    spacing: 10

    HusNotification {
        id: notification8
        parent: Overlay.overlay
        anchors.fill: parent
        anchors.topMargin: captionBar.height
        position: HusNotification.Position_TopRight
    }

    HusButton {
        text: 'Success'
        onClicked: {
            notification8.success('Notification Title', 'This is a success notification!');
        }
    }

    HusButton {
        text: 'Info'
        onClicked: {
            notification8.info('Notification Title', 'This is a info notification!');
        }
    }

    HusButton {
        text: 'Warning'
        onClicked: {
            notification8.warning('Notification Title', 'This is a warning notification!');
        }
    }

    HusButton {
        text: 'Error'
        onClicked: {
            notification8.error('Notification Title', 'This is an error notification!');
        }
    }
}

示例 4 - 显示进度条

通过 showProgress 属性设置是否显示进度条。

import QtQuick
import QtQuick.Controls.Basic
import HuskarUI.Basic

Row {
    width: parent.width
    spacing: 10

    HusNotification {
        id: notification9
        parent: Overlay.overlay
        anchors.fill: parent
        anchors.topMargin: captionBar.height
        position: HusNotification.Position_TopRight
        showProgress: true
    }

    HusButton {
        text: 'Show progress notification'
        onClicked: {
            notification9.info('Notification Title', 'This is a show progress notification!');
        }
    }
}

示例 5 - 堆叠

通过 stackMode 属性设置是否开启堆叠模式。

通过 stackThreshold 属性设置堆叠阈值。

import QtQuick
import QtQuick.Controls.Basic
import HuskarUI.Basic

Row {
    width: parent.width
    spacing: 10

    HusNotification {
        id: notification10
        parent: Overlay.overlay
        anchors.fill: parent
        anchors.topMargin: captionBar.height
        position: HusNotification.Position_TopRight
        stackMode: stackSwitch.checked
        stackThreshold: stackThresholdInput.value
    }

    HusButton {
        anchors.verticalCenter: parent.verticalCenter
        text: 'Open the notification box'
        type: HusButton.Type_Primary
        onClicked: {
            notification10.open({
                                    message: 'Notification Title',
                                    description: 'This is the content of the notification. This is the content of the notification. This is the content of the notification.',
                                    duration: 99999999
                                });
        }
    }

    HusText {
        text: 'Enabled stackMode:'
        anchors.verticalCenter: parent.verticalCenter
    }

    HusSwitch {
        id: stackSwitch
        anchors.verticalCenter: parent.verticalCenter
        checked: true
    }

    HusInputNumber {
        id: stackThresholdInput
        width: 200
        anchors.verticalCenter: parent.verticalCenter
        prefix: 'Threshold: '
        value: 5
        min: 1
        max: 10
    }
}