当前位置:网站首页>QML custom tabbar
QML custom tabbar
2022-07-29 05:23:00 【Shangguan Hongzhu】
QML customized TabBar
Customize one vertical TabBar, That is, vertical TabBar.QT default TabBar They are all level , And customize it Tab The background and font colors of items are troublesome . Let's customize a vertical with the following effect today TabBar
:
1、 Default TabBar
default TabBar The implementation is as follows , Is written by a TabBar
Add a StackLayout
Combination to achieve ,StackLayout
Is a stack layout , The so-called stack layout simply means that only one stack element in the layout can be displayed at the same time , Stack layout can be used currentIndex
To control which tab is displayed .TabButton
Default and TabBar
In combination with , As TabBar
Each independent click button exists in .
TabBar {
id: bar
width: parent.width
TabButton {
text: qsTr("Home")
}
TabButton {
text: qsTr("Discover")
}
}
StackLayout {
width: parent.width
anchors.top: bar.bottom
anchors.bottom: parent.bottom
currentIndex: bar.currentIndex
Rectangle {
color: "red"
}
Rectangle {
color: "yellow"
}
}
The above default state effect is as follows :
2、 Customized ideas
Use a ListView
To complete . Actually consult TabBar
Source code can be found , The source code itself uses a ListView
As one of it contentItem
term .
ListView
There is one orientation
Property is used to indicate whether the list is vertical or horizontal , Here we set it to ListView.Vertical
Vertical ,ListView.Vertical
It is also its default value , If we want to use horizontal TabBar
You can change this value to ListView.Horizontal
.
We mainly customize ListView
Agent for delegate
, It is generally designated as a ItemDelegate
that will do .
ItemDelegate
There are two items that can be customized. One is the background color background
, We designate a Rectangle
, And then set its color
attribute . The other is its content item contentItem
, Here we use a Rectangle
, As a container , You can set the background color , At the same time, it can contain a Text
, Used to display our text data . in addition , If you want to change the gesture of the mouse , Or add some click event processing , Can be in ItemDelegate
Add one next MouseArea
, Fill the entire parent ( namely ItemDelegate
).
There is a key problem here , How to do it when clicking tab The switch of ?
ItemDelegate
Triggered when clicked clicked
The signal , stay onClicked
In the slot function of ListView
Of currentIndex
Property is assigned to index
, among index
yes delegate
Default properties of agents , Indicates the current index value , The code is as follows
onClicked: control.currentIndex = index
3、 The overall architecture
custom TabBar
The overall code architecture is as follows :
delegate: ItemDelegate {
text: modelData
onClicked: control.currentIndex = index
MouseArea {
anchors.fill: parent
acceptedButtons: Qt.NoButton
cursorShape: Qt.PointingHandCursor
}
contentItem: Rectangle {
anchors.fill: parent
Text {
anchors.fill: parent
text: tabDelegate.text
}
}
background: Rectangle {
color: tabBackgroundColor
}
}
The complete implementation can search official account through wechat :“ Shangguan Hongzhu ”, Or scan the QR code below , Pay attention to and reply to :“TabBar”, Get resource links . If you have any questions, you can also leave a message for consultation .
边栏推荐
- webgl1.0下texture2D和texture2DProj区别
- Rolabelimg to data format data
- 玩家访问网站自动弹窗加QQ群方法以及详细代码
- "Invisible Bridge" built in the free trade economy: domestic products and Chinese AI power
- AI应用第一课:C语言支付宝刷脸登录
- 365天挑战LeetCode1000题——Day 037 元素和小于等于阈值的正方形的最大边长 + 满足条件的子序列数目
- 小白高薪捷径-Qt开发游戏—贪吃蛇
- Deadlock analysis using jstack, jconsole, and jvisualvm
- MySQL sorts the queried result set according to the specified sequence
- Ros1 dead chicken data is stored in txt and SQLite
猜你喜欢
ARFoundation从零开始5-AR图像跟踪
The method and detailed code of automatically pop-up and QQ group when players visit the website
ARFoundation入门教程7-url动态加载图像跟踪库
研发效能生态完整图谱&DevOps工具选型必看
Open source Huizhi creates the future | the openeuler sub forum of 2022 open atom global open source summit was successfully held
Unity3d - the object is too far away to see
Deep learning brush a bunch of tricks of SOTA
2022年SPSSPRO认证杯数学建模B题第二阶段方案及赛后总结
CMU15-213 Shell Lab实验记录
The representation of time series analysis: is the era of learning coming?
随机推荐
Handwritten student management system
研发效能生态完整图谱&DevOps工具选型必看
51万奖池邀你参战!第二届阿里云ECS CloudBuild开发者大赛来袭
Qml类型:State 状态
Teardown's method of lifting the time limit
How to add a map to the legendary server
Ros1 dead chicken data is stored in txt and SQLite
Numpy Foundation
AI应用第一课:C语言支付宝刷脸登录
NumPy基础
365天挑战LeetCode1000题——Day 036 二叉树剪枝 + 子数组和排序后的区间和 + 删除最短的子数组使剩余数组有序
传奇如何一台服务器配置多个版本微端更新
Arfoundation starts from scratch 3- create an arfoundation project
Mysql多对多关系,分组拼接把多个数据查询到一条数据上
关于thymeleaf的配置与使用
How rimworld uploads creative workshops through steamcmd
OCCT学习002-----环境搭建
Open source Huizhi creates the future | the openeuler sub forum of 2022 open atom global open source summit was successfully held
京东云联合Forrester咨询发布混合云报告 云原生成为驱动产业发展新引擎
ARFoundation入门教程7-url动态加载图像跟踪库