QML TabBar 和 TabButton 组件

TabBar 和 TabButton 是 QML 中用于创建选项卡式界面的组件,通常与 SwipeView 或 StackLayout 配合使用,实现内容切换功能。

TabBar 组件

基本属性

属性 类型 默认值 说明
currentIndex int 0 当前选中选项卡的索引
contentItem Item 自动创建 包含选项卡按钮的项
position enumeration TabBar.Header 选项卡栏的位置
spacing real 0 选项卡之间的间距
implicitContentWidth real 自动计算 隐式内容宽度
implicitContentHeight real 自动计算 隐式内容高度

Position 枚举值

说明
TabBar.Header 顶部位置(默认)
TabBar.Footer 底部位置

TabButton 组件

基本属性

属性 类型 默认值 说明
text string "" 选项卡按钮显示的文本
icon.source url "" 选项卡按钮的图标
icon.width real -1 图标宽度
icon.height real -1 图标高度
icon.color color "transparent" 图标颜色
display enumeration Button.TextBesideIcon 图标和文本的显示方式
checked bool false 是否选中状态
autoExclusive bool true 是否自动互斥

Display 枚举值

说明
Button.IconOnly 只显示图标
Button.TextOnly 只显示文本
Button.TextUnderIcon 文本在图标下方
Button.TextBesideIcon 文本在图标旁边

常用方法

TabBar 方法

方法 参数 返回值 说明
itemAt(index) index: int Item 返回指定索引的选项卡按钮
setCurrentIndex(index) index: int - 设置当前选中的选项卡

TabButton 方法

继承自 AbstractButton 的方法:

方法 参数 返回值 说明
toggle() - - 切换选中状态

常用信号

TabBar 信号

信号 参数 说明
currentIndexChanged() - 当前索引改变时触发

TabButton 信号

继承自 AbstractButton 的信号:

信号 参数 说明
clicked() - 点击时触发
toggled() - 选中状态改变时触发

使用示例

基本选项卡示例

qml

复制代码
import QtQuick 2.15
import QtQuick.Controls 2.15

Column {
    TabBar {
        id: tabBar
        width: parent.width
        
        TabButton { text: "首页" }
        TabButton { text: "发现" }
        TabButton { text: "消息" }
        TabButton { text: "我的" }
    }
    
    StackLayout {
        width: parent.width
        height: 200
        currentIndex: tabBar.currentIndex
        
        Item { Label { text: "首页内容"; anchors.centerIn: parent } }
        Item { Label { text: "发现内容"; anchors.centerIn: parent } }
        Item { Label { text: "消息内容"; anchors.centerIn: parent } }
        Item { Label { text: "我的内容"; anchors.centerIn: parent } }
    }
}

带图标的选项卡

qml

复制代码
TabBar {
    id: tabBar
    width: parent.width
    
    TabButton {
        text: "首页"
        icon.source: "home.png"
        display: Button.TextUnderIcon
    }
    
    TabButton {
        text: "设置"
        icon.source: "settings.png"
        display: Button.TextUnderIcon
    }
}

自定义样式选项卡

qml

复制代码
TabBar {
    id: tabBar
    width: parent.width
    
    background: Rectangle {
        color: "#F5F5F5"
    }
    
    TabButton {
        text: "首页"
        background: Rectangle {
            color: tabBar.currentIndex === 0 ? "#21BE2B" : "transparent"
            radius: 5
        }
    }
    
    TabButton {
        text: "发现"
        background: Rectangle {
            color: tabBar.currentIndex === 1 ? "#21BE2B" : "transparent"
            radius: 5
        }
    }
}

与 SwipeView 配合使用

qml

复制代码
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Window 2.15
import QtQuick.Dialogs 1.3
import QtQuick.Layouts 1.15

ApplicationWindow {
    width: 800
    height: 600
    visible: true
    title: "demo"

    Column {
        anchors.fill: parent
        TabBar {
            id: tabBar
            width: parent.width
            currentIndex: swipeView.currentIndex

            TabButton { text: "视图1" }
            TabButton { text: "视图2" }
            TabButton { text: "视图3" }
        }

        SwipeView {
            id: swipeView
            width: parent.width
            height: 200
            currentIndex: tabBar.currentIndex

            Item { Rectangle { color: "red"; anchors.fill: parent } }
            Item { Rectangle { color: "green"; anchors.fill: parent } }
            Item { Rectangle { color: "blue"; anchors.fill: parent } }
        }
    }
}

高级用法

动态添加选项卡

qml

复制代码
Column {
    Button {
        text: "添加选项卡"
        onClicked: {
            var tab = tabButtonComponent.createObject(tabBar, {text: "标签 " + (tabBar.count + 1)})
            swipeView.addItem(swipeItemComponent.createObject(swipeView))
        }
    }
    
    TabBar {
        id: tabBar
        width: parent.width
        property int count: 0
    }
    
    SwipeView {
        id: swipeView
        width: parent.width
        height: 200
        currentIndex: tabBar.currentIndex
    }
    
    Component {
        id: tabButtonComponent
        TabButton {
            onClicked: tabBar.currentIndex = index
            Component.onCompleted: tabBar.count++
        }
    }
    
    Component {
        id: swipeItemComponent
        Item {
            Rectangle {
                anchors.fill: parent
                color: Qt.rgba(Math.random(), Math.random(), Math.random(), 1)
                Label {
                    anchors.centerIn: parent
                    text: "内容 " + (swipeView.count)
                }
            }
        }
    }
}

可关闭的选项卡

qml

复制代码
TabBar {
    id: tabBar
    width: parent.width
    
    Repeater {
        model: ["首页", "发现", "消息"]
        delegate: TabButton {
            text: modelData
            rightPadding: closeButton.width + 10
            
            Button {
                id: closeButton
                anchors.right: parent.right
                anchors.verticalCenter: parent.verticalCenter
                width: 20
                height: 20
                text: "×"
                flat: true
                visible: tabBar.count > 1
                onClicked: {
                    // 这里需要实现关闭逻辑
                    console.log("关闭选项卡:", index)
                }
            }
        }
    }
}

响应式选项卡

qml

复制代码
TabBar {
    id: tabBar
    width: parent.width
    
    TabButton {
        text: "首页"
        width: Math.max(100, tabBar.width / 4)
    }
    
    TabButton {
        text: "发现"
        width: Math.max(100, tabBar.width / 4)
    }
    
    TabButton {
        text: "消息"
        width: Math.max(100, tabBar.width / 4)
    }
    
    TabButton {
        text: "我的"
        width: Math.max(100, tabBar.width / 4)
    }
}

注意事项

  1. TabBar 通常与 StackLayout 或 SwipeView 配合使用

  2. 可以通过 currentIndex 属性同步 TabBar 和内容视图

  3. 使用 position 属性可以控制 TabBar 显示在顶部或底部

  4. 可以通过自定义 background 和 contentItem 实现复杂的样式

  5. TabButton 继承自 AbstractButton,支持所有按钮的属性和信号

相关推荐
千疑千寻~5 天前
【QML】C++访问QML控件
c++·qml
Wallace Zhang6 天前
PySide6 + QML - Charts07 - 使用checkbox选择需要显示的曲线
vscode·pyside6·qml
千疑千寻~6 天前
【QML】自定义控件
qml
Wallace Zhang21 天前
PySide6 + QML - 调试日志01 -告别打印log中文乱码,快速且简单地解决
qt·pyside6·qml
江公望1 个月前
Qt QHostInfo::lookupHost()函数,10分钟讲清楚
开发语言·qt·qml
江公望1 个月前
Qt告警clazy-detaching-temporary浅谈
qt·qml
Hi202402171 个月前
为QML程序添加启动Logo:提升用户体验
windows·qt·ui·人机交互·qml·启动logo
紫荆鱼1 个月前
PCL实战项目-软件界面搭建RibbonUI
qt·pcl·用户界面·qml·点云处理
江公望1 个月前
装了新的QtCreator17,没有用Qt5.12自带的QtCreator4,导致QtCreator17无法找到Qt5.12帮助文档
qt·qml
奔跑吧 android1 个月前
【Qt】【1. 版本特性介绍】
qt·cpp·qml