Skip to content

EAvatar

EAvatar 是一个用于显示用户头像的组件,支持多种尺寸和样式配置。

基本用法

qml
import "components" as Components

Components.ETheme {
    id: theme
}

Components.EAvatar {
    width: 80
    height: 80
    source: "https://example.com/avatar.jpg"
    borderWidth: 2
    borderColor: theme.primaryColor
}

属性

属性名类型默认值描述
sourcestring""头像图片的路径或URL
widthreal60头像宽度
heightreal60头像高度
radiusrealwidth/2头像圆角半径,默认为宽度的一半(圆形)
borderWidthreal0边框宽度
borderColorcolor"transparent"边框颜色
placeholderstring""图片加载失败时显示的占位符文本
placeholderColorcolortheme.textSecondaryColor占位符文本颜色
placeholderBackgroundColorcolortheme.surfaceVariantColor占位符背景颜色

方法

方法名参数返回值描述
loadImage(source)source: stringvoid动态加载新的头像图片
clear()void清除当前头像,显示占位符

信号

信号名参数描述
imageLoaded图片加载成功时触发
imageFailederror: string图片加载失败时触发,返回错误信息

示例

不同尺寸的头像

qml
RowLayout {
    spacing: 16
    
    Components.EAvatar {
        width: 40
        height: 40
        source: "https://example.com/avatar.jpg"
    }
    
    Components.EAvatar {
        width: 60
        height: 60
        source: "https://example.com/avatar.jpg"
    }
    
    Components.EAvatar {
        width: 100
        height: 100
        source: "https://example.com/avatar.jpg"
    }
}

带边框的头像

qml
Components.EAvatar {
    width: 80
    height: 80
    source: "https://example.com/avatar.jpg"
    borderWidth: 3
    borderColor: theme.accentColor
    radius: 10  // 方形圆角头像
}

带占位符的头像

qml
Components.EAvatar {
    width: 80
    height: 80
    source: "invalid-url.jpg"  // 无效的图片URL
    placeholder: "用户"
    placeholderColor: "white"
    placeholderBackgroundColor: theme.primaryColor
}

注意事项

  • 建议使用正方形图片作为头像源,以获得最佳显示效果
  • 支持本地路径和网络URL作为图片源
  • 占位符文本只在图片加载失败时显示
  • radius 设置为宽度的一半时,头像将显示为圆形

使用 VitePress 构建