Skip to content

EButton

EButton 是一个带图标和动画效果的圆角按钮组件,支持多种状态和样式配置。

基本用法

qml
import "components" as Components

Components.ETheme {
    id: theme
}

// 导入图标字体
FontLoader {
    id: iconFont
    source: "qrc:/new/prefix1/fonts/fontawesome-free-6.7.2-desktop/otfs/Font Awesome 6 Free-Solid-900.otf"
}

Components.EButton {
    text: "提交"
    iconCharacter: "\uf1d8"  // Font Awesome 图标代码
    onClicked: console.log("按钮被点击")
}

属性

属性名类型默认值描述
textstring""按钮显示文本
iconCharacterstring""图标字符(支持 Font Awesome)
iconSizereal16图标大小
iconPositionstring"left"图标位置,可选值:"left", "right", "top", "bottom"
widthreal120按钮宽度
heightreal40按钮高度
radiusreal8按钮圆角半径
backgroundColorcolortheme.primaryColor按钮背景颜色
textColorcolortheme.onPrimaryColor按钮文本颜色
iconColorcolortheme.onPrimaryColor按钮图标颜色
borderWidthreal0边框宽度
borderColorcolor"transparent"边框颜色
disabledboolfalse是否禁用按钮
loadingboolfalse是否显示加载状态
shadowEnabledbooltrue是否启用阴影效果

方法

方法名参数返回值描述
setLoading(loading)loading: boolvoid设置按钮的加载状态
setDisabled(disabled)disabled: boolvoid设置按钮的禁用状态
setText(text)text: stringvoid设置按钮显示文本
setIcon(icon)icon: stringvoid设置按钮图标

信号

信号名参数描述
clicked按钮被点击时触发
hoveredhovered: bool鼠标悬停状态变化时触发
pressedpressed: bool按钮按下状态变化时触发

示例

不同样式的按钮

qml
ColumnLayout {
    spacing: 12
    
    // 主按钮
    Components.EButton {
        text: "主按钮"
        backgroundColor: theme.primaryColor
    }
    
    // 次要按钮
    Components.EButton {
        text: "次要按钮"
        backgroundColor: theme.secondaryColor
    }
    
    // 带图标的按钮
    Components.EButton {
        text: "带图标"
        iconCharacter: "\uf067"
        iconPosition: "right"
    }
    
    // 边框按钮
    Components.EButton {
        text: "边框按钮"
        backgroundColor: "transparent"
        borderWidth: 2
        borderColor: theme.primaryColor
        textColor: theme.primaryColor
        iconColor: theme.primaryColor
    }
    
    // 禁用按钮
    Components.EButton {
        text: "禁用按钮"
        disabled: true
    }
    
    // 加载状态按钮
    Components.EButton {
        text: "加载中"
        loading: true
    }
}

动态改变按钮状态

qml
Components.EButton {
    id: dynamicButton
    text: "点击我"
    iconCharacter: "\uf0a9"
    
    onClicked: {
        dynamicButton.setLoading(true)
        dynamicButton.setText("处理中...")
        
        // 模拟异步操作
        Timer {
            interval: 2000
            running: true
            onTriggered: {
                dynamicButton.setLoading(false)
                dynamicButton.setText("完成")
                
                // 3秒后恢复原状
                Timer {
                    interval: 3000
                    running: true
                    onTriggered: {
                        dynamicButton.setText("点击我")
                    }
                }
            }
        }
    }
}

注意事项

  • 确保已正确加载 Font Awesome 字体文件,否则图标可能无法显示
  • 按钮的动画效果会自动适应主题的颜色变化
  • 当按钮处于 loading 状态时,点击事件将被忽略
  • 建议在长文本按钮上使用适当的宽度或启用自动换行

使用 VitePress 构建