【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
                }
            }
        }
    }
相关推荐
by2099911 分钟前
从 malloc/free 到 new/delete:真正理解 C++ 中的内存管理与对象生命周期
c语言·c++·经验分享·动态内存管理
m0_3807438742 分钟前
Qt侧边栏布局的实现示例
开发语言·c++
汉克老师1 小时前
GESP2026年9月认证C++五级( 第三部分编程题(1、哥德巴赫猜想))精讲
c++·gesp·小学生·学c++编程
小灰灰搞电子1 小时前
C++ final 关键字详解:从虚函数到类继承的完整指南
c++·final
weixin_307779132 小时前
C++代码实现MATLAB中的pade函数功能
开发语言·c++·算法·matlab
神仙别闹2 小时前
基于C++实现的(控制台)学校开课查询系统
开发语言·c++
Dylan的码园2 小时前
A+B问题 进制处理【逻辑控制 运算符】
c++·算法
hetao17338373 小时前
2026-09-27~28 hetao1733837 的刷题记录
c++·算法
2601_962218613 小时前
C++的几种编译器的实现
开发语言·c++
多弗朗皮卡丘3 小时前
C++ stack和queue
开发语言·c++·deque·queue·stack·容器适配器