Creating a Custom TabBar with Dynamic Styling in QML


This is a demo showing how to customize tab bar and tab button on QML and Qt 6. You can find the full project here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import QtQuick
import QtQuick.Controls.Universal
import QtQuick.Layouts

Window {
width: 880
height: 620
visible: true
title: qsTr("Custom TabBar")

ColumnLayout{
anchors.fill: parent
spacing: 10
anchors.margins: 20

TabBar {
id: tabBar
Layout.alignment: Qt.AlignHCenter
Layout.preferredWidth: 250
background: Rectangle {
color: "lightgray"
radius: 10
}

TabButton {
id: noteTab
text: qsTr("Notes")
implicitHeight: 30
contentItem: Text {
text: parent.text
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
color: parent.checked ? "black" : "white"
font.bold: true
font.pointSize: 14
font.family: "Roboto"
padding: 0
}
background: Rectangle {
color: noteTab.checked ? "white" : "lightgray"
border.color: noteTab.checked ? "green" : "lightgray"
border.width: 2
radius: 10
}
}

TabButton {
id: archiveTab
text: qsTr("Archive")
implicitHeight: 30
contentItem: Text {
text: parent.text
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
color: parent.checked ? "black" : "white"
font.bold: true
font.pointSize: 14
font.family: "Roboto"
}
background: Rectangle {
color: archiveTab.checked ? "white" : "lightgray"
border.color: archiveTab.checked ? "green" : "lightgray"
border.width: 2
radius: 10
}
}

TabButton {
id: deletedTab
text: qsTr("Deleted")
implicitHeight: 30
contentItem: Text {
text: parent.text
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
color: parent.checked ? "black" : "white"
font.bold: true
font.pointSize: 14
font.family: "Roboto"
}
background: Rectangle {
color: deletedTab.checked ? "white" : "lightgray"
border.color: deletedTab.checked ? "green" : "lightgray"
border.width: 2
radius: 10
}
}
}

StackLayout {
width: parent.width
currentIndex: tabBar.currentIndex

Item {
id: homeTab

Text {
text: qsTr("Notes")
}
}
Item {
id: discoverTab
Text {
text: qsTr("Archive")
}
}
Item {
id: trashTab
Text {
text: qsTr("Deleted")
}
}
}
}
}
Author

Kelvin Cervan

Posted on

2024-12-16

Updated on

2025-03-24

Licensed under