HusCarousel 走马灯

February 25, 2026 · View on GitHub

← 返回主目录

← 返回本类别目录

HusCarousel 走马灯

一组轮播的区域。

  • 模块 { HuskarUI.Basic }

  • 继承自 { Control }


支持的代理:

  • contentDelegate: Component 内容代理,代理可访问属性:

    • index: int 内容项索引

    • model: var 内容项数据

  • indicatorDelegate: Component 指示器代理,代理可访问属性:

    • index: int 指示器索引

    • model: var 指示器数据

  • prevDelegate: Component 向前箭头代理

  • nextDelegate: Component 向后箭头代理


支持的属性:

属性名类型默认值描述
animationEnabledboolHusTheme.animationEnabled是否开启动画
initModelarray[]初始数据模型
currentIndexint-1当前索引
positionenumHusCarousel.Position_Bottom滚动的方向和指示器的位置(来自 HusCarousel)
speedint500切换动效的时间(毫秒)
infinitebooltrue是否无限滚动
autoplayboolfalse是否自动切换
autoplaySpeedint3000自动切换的时间(毫秒)
draggablebooltrue是否启用拖拽切换
showIndicatorbooltrue是否显示指示器
indicatorSpacingint6指示器间隔
showArrowboolfalse是否显示箭头

支持的函数:

  • switchTo(index: int, animated: bool = true)

    • index 要切换的目标处索引
    • animated 是否使用切换动效
  • switchToPrev() 切换到前一页

  • switchToNext() 切换到后一页

  • int getSuitableIndicatorWidth(contentWidth: int, indicatorMaxWidth: int = 18) 获取合适的指示器宽度

    • contentWidth 轮播内容的宽度
    • indicatorMaxWidth 指示器最大宽度

代码演示

示例 1 - 基本

最简单的用法。

通过 infinite 属性设置是否无限滚动。

import QtQuick
import HuskarUI.Basic

Column {
    spacing: 10
    width: parent.width

    Row {
        spacing: 10

        HusText { text: qsTr('无限滚动') }

        HusSwitch {
            id: infiniteSwitch
            checked: true
        }
    }

    HusCarousel {
        id: carousel1
        width: parent.width
        height: 200
        infinite: infiniteSwitch.checked
        initModel: [
            { label: '1' },
            { label: '2' },
            { label: '3' },
            { label: '4' },
        ]
        contentDelegate: Rectangle {
            width: carousel1.width
            height: carousel1.height
            color: '#364d79'

            HusText {
                anchors.centerIn: parent
                text: model.label
                color: 'white'
                font.pixelSize: 16
                font.bold: true
            }
        }
    }
}

示例 2 - 位置

通过 position 属性设置滚动的方向和指示器的位置,支持的位置:

  • 水平滚动,指示器在上方,{ HusCarousel.Position_Top }

  • 水平滚动,指示器在下方{ HusCarousel.Position_Bottom }

  • 垂直滚动,指示器在左方{ HusCarousel.Position_Left }

  • 垂直滚动,指示器在右方{ HusCarousel.Position_Right }

import QtQuick
import HuskarUI.Basic

Column {
    spacing: 10
    width: parent.width

    HusRadioBlock {
        id: positionBlock
        initCheckedIndex: 1
        model: [
            { label: qsTr('上'), value: HusCarousel.Position_Top },
            { label: qsTr('下'), value: HusCarousel.Position_Bottom },
            { label: qsTr('左'), value: HusCarousel.Position_Left },
            { label: qsTr('右'), value: HusCarousel.Position_Right }
        ]
    }

    HusCarousel {
        id: carousel2
        width: parent.width
        height: 200
        position: positionBlock.currentCheckedValue
        initModel: [
            { label: '1' },
            { label: '2' },
            { label: '3' },
            { label: '4' },
        ]
        contentDelegate: Rectangle {
            width: carousel2.width
            height: carousel2.height
            color: '#364d79'

            HusText {
                anchors.centerIn: parent
                text: model.label
                color: 'white'
                font.pixelSize: 16
                font.bold: true
            }
        }
    }
}

示例 3 - 自动切换

通过 autoplay 属性设置是否自动切换。

通过 autoplaySpeed 属性设置自动切换时间。

import QtQuick
import HuskarUI.Basic

Column {
    spacing: 10
    width: parent.width

    HusCarousel {
        id: carousel4
        width: parent.width
        height: 200
        autoplay: true
        initModel: [
            { label: '1' },
            { label: '2' },
            { label: '3' },
            { label: '4' },
        ]
        contentDelegate: Rectangle {
            width: carousel4.width
            height: carousel4.height
            color: '#364d79'

            HusText {
                anchors.centerIn: parent
                text: model.label
                color: 'white'
                font.pixelSize: 16
                font.bold: true
            }
        }
    }
}

示例 4 - 切换箭头

通过 showArrow 属性设置是否显示切换箭头。

import QtQuick
import HuskarUI.Basic

Column {
    spacing: 10
    width: parent.width

    HusCarousel {
        id: carousel5
        width: parent.width
        height: 200
        showArrow: true
        initModel: [
            { label: '1' },
            { label: '2' },
            { label: '3' },
            { label: '4' },
        ]
        contentDelegate: Rectangle {
            width: carousel5.width
            height: carousel5.height
            color: '#364d79'

            HusText {
                anchors.centerIn: parent
                text: model.label
                color: 'white'
                font.pixelSize: 16
                font.bold: true
            }
        }
    }
}