【Qt QML】TabBar的用法

Qt Quick中的TabBar提供了一个基于选项卡的导航模型。TabBar由TabButton控件填充,并且可以与任何提供currentIndex属性的布局或容器控件一起使用,例如StackLayout或SwipeView。

cpp 复制代码
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts

Window {
    id: win
    width: 640
    height: 480
    visible: true
    title: qsTr("Tab Page")

    ListModel {
        id: pageModel
        ListElement{
            pageName : "Home"
            pageColor : "steelblue"
        }
        ListElement{
            pageName : "FirstPage"
            pageColor : "tomato"
        }
        ListElement{
            pageName : "SecondPage"
            pageColor : "slateblue"
        }
    }

    TabBar {
        id: tabBar
        width: parent.width
        height: 50
        Repeater {
            model: pageModel
            TabButton {
                text:  pageName
                height: parent.height
                background: Rectangle {
                    implicitHeight: parent.height
                    color: pageColor
                }
            }
        }
    }

    StackLayout {
        anchors.top: tabBar.bottom
        height: parent.height
        width: parent.width
        currentIndex: tabBar.currentIndex
        Repeater {
            model: pageModel
            Rectangle {
                id: homeTab
                width: parent.width
                height: parent.height
                color: pageColor
                Text {
                    id: pageText
                    text: pageName
                    anchors.centerIn: parent
                }
            }
        }
    }
相关推荐
六bring个六1 分钟前
文件系统交互实现
开发语言·c++·qt·交互
小山菌17 分钟前
mac中加载C++动态库文件
开发语言·c++·macos
疯狂学习GIS23 分钟前
Windows配置VS Code详细流程
c++·学术工作效率
__BMGT()24 分钟前
C++ QT图片查看器
前端·c++·qt
Echo``41 分钟前
1:OpenCV—图像基础
c++·图像处理·人工智能·opencv·算法·计算机视觉·视觉检测
ALex_zry1 小时前
Ubuntu 20.04 C++开发环境搭建指南(2025版)
linux·c++·ubuntu
_F_y3 小时前
list简单模拟实现
c++·list
前进的程序员3 小时前
C++ 在 Windows 和 Linux 平台上的开发差异及常见问题
linux·c++·windows
daiwoliyunshang4 小时前
哈希表实现(1):
数据结构·c++
pystraf4 小时前
模板分享:网络最小费用流
c++·算法·图论·网络流