HusAvatar 头像

January 26, 2026 · View on GitHub

← 返回主目录

← 返回本类别目录

HusAvatar 头像

用来代表用户或事物,支持图片、图标或字符展示。

  • 模块 { HuskarUI.Basic }

  • 继承自 { Control }


支持的代理:


支持的属性:

属性名类型默认值描述
sizeint30头像大小
iconSourceint丨string0头像图标(来自 HusIcon)或图标链接
imageSourceurl''头像图像
imageMipmapboolfalse是否开启层级映射
textSourcestring''头像文本
textFontfont-文本字体(文本头像时生效)
textSizeenumHusAvatar.Size_Fixed文本大小模式(来自 HusAvatar)
textGapint4文本距离两侧单位像素(文本头像时生效)
colorBgcolor-背景颜色
colorIconcolor'white'图标颜色(图标头像时生效)
colorTextcolor'white'文本颜色(文本头像时生效)
radiusBgHusRadius-背景圆角

注意 [iconSource/imageSource/textSource]只需提供一种即可


代码演示

示例 1 - 基本

通过 size 属性设置大小。

通过 radiusBg 属性设置圆角大小(默认为size一半, 即圆形)。

import QtQuick
import HuskarUI.Basic

Column {
    spacing: 10

    Row {
        spacing: 10

        HusAvatar {
            size: 100
            iconSource: HusIcon.UserOutlined
        }

        HusAvatar {
            size: 80
            iconSource: HusIcon.UserOutlined
        }

        HusAvatar {
            size: 60
            iconSource: HusIcon.UserOutlined
        }

        HusAvatar {
            size: 40
            iconSource: HusIcon.UserOutlined
        }

        HusAvatar {
            size: 20
            iconSource: HusIcon.UserOutlined
        }
    }

    Row {
        spacing: 10

        HusAvatar {
            size: 100
            iconSource: HusIcon.UserOutlined
            radiusBg.all: 6
        }

        HusAvatar {
            size: 80
            iconSource: HusIcon.UserOutlined
            radiusBg.all: 6
        }

        HusAvatar {
            size: 60
            iconSource: HusIcon.UserOutlined
            radiusBg.all: 6
        }

        HusAvatar {
            size: 40
            iconSource: HusIcon.UserOutlined
            radiusBg.all: 6
        }

        HusAvatar {
            size: 20
            iconSource: HusIcon.UserOutlined
            radiusBg.all: 6
        }
    }
}

示例 2 - 类型

支持三种类型:图片、图标以及字符型头像。

import QtQuick
import HuskarUI.Basic

Row {
    spacing: 10

    HusAvatar {
        anchors.verticalCenter: parent.verticalCenter
        iconSource: HusIcon.UserOutlined
    }

    HusAvatar {
        anchors.verticalCenter: parent.verticalCenter
        textSource: 'U'
    }

    HusAvatar {
        anchors.verticalCenter: parent.verticalCenter
        textSource: 'USER'
    }

    HusAvatar {
        anchors.verticalCenter: parent.verticalCenter
        imageSource: 'https://avatars.githubusercontent.com/u/33405710?v=4'
    }

    HusAvatar {
        anchors.verticalCenter: parent.verticalCenter
        textSource: 'U'
        colorText: '#F56A00'
        colorBg: '#FDE3CF'
    }

    HusAvatar {
        anchors.verticalCenter: parent.verticalCenter
        iconSource: HusIcon.UserOutlined
        colorBg: '#87D068'
    }
}

示例 3 - 自动调整字符型头像大小

通过 textSize 属性设置文本大小调整模式,支持的大小:

  • 固定大小(默认) { HusAvatar.Size_Fixed }

  • 自动计算大小 { HusAvatar.Size_Auto }

通过 textGap 属性设置字符距离左右两侧边界单位像素。

import QtQuick
import HuskarUI.Basic

Row {
    spacing: 10

    HusAvatar {
        anchors.verticalCenter: parent.verticalCenter
        size: 40
        textSource: changeButton.userList[changeButton.index]
        colorBg: changeButton.colorList[changeButton.index]
        textSize: HusAvatar.Size_Fixed
    }

    HusAvatar {
        anchors.verticalCenter: parent.verticalCenter
        size: 40
        textSource: changeButton.userList[changeButton.index]
        colorBg: changeButton.colorList[changeButton.index]
        textSize: HusAvatar.Size_Auto
    }

    HusButton {
        id: changeButton
        anchors.verticalCenter: parent.verticalCenter
        text: qsTr('ChangeUser')
        onClicked: {
            index = (index + 1) % 4;
        }
        property int index: 0
        property var userList: ['U', 'Lucy', 'Tom', 'Edward']
        property var colorList: ['#f56a00', '#7265e6', '#ffbf00', '#00a2ae']
    }
}