当前位置:网站首页>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 .
边栏推荐
- SM整合原来这么简单,步骤清晰(详细)
- The representation of time series analysis: is the era of learning coming?
- How rimworld uploads creative workshops through steamcmd
- The latest tank battle 2022 - full development notes-3
- Qml类型:MouseArea
- D3d Shader Instruction
- scikit-learn——机器学习应用开发的步骤和理解
- [from_bilibili_DR_CAN][【Advanced控制理论】9_状态观测器设计][学习记录]
- 6.3 references
- 2022年SPSSPRO认证杯数学建模B题第二阶段方案及赛后总结
猜你喜欢

Arfoundation starts from scratch 3- create an arfoundation project

直播预告|如何通过“智能边缘安全”提升企业免疫力?

三层项目的架构分析及构造方法的参数名称注入

How to add traffic statistics codes to the legendary Development Zone website

How mongodb inserts, deletes and updates documents

TCP three handshakes and four waves

OCCT学习003-----MFC单文档工程

预约中,2022京东云产业融合新品发布会线上开启

365天挑战LeetCode1000题——Day 036 二叉树剪枝 + 子数组和排序后的区间和 + 删除最短的子数组使剩余数组有序

C语言函数实现输出I love you
随机推荐
Qml类型:State 状态
Getting started with arfoundation tutorial 10- plane detection and placement
传奇服务端如何添加地图
Unity3d - the object is too far away to see
Learn the first program of database
2022年泰迪杯数据挖掘挑战赛C题方案及赛后总结
Xiaolu Inn - Trailer
This article takes you to understand the implementation of surround notification @around and final notification @after
7.2-function-overloading
数千个数据库、遍布全国的物理机,京东物流全量上云实录 | 卓越技术团队访谈录
直播预告|如何通过“智能边缘安全”提升企业免疫力?
关于thymeleaf的配置与使用
SM整合原来这么简单,步骤清晰(详细)
Soft link & hard link
365天挑战LeetCode1000题——Day 042 数组序号转换 + 相对名次 离散化处理
关于servlet中实现网站的页面跳转
开区网站打开自动播放音乐的添加跟修改教程
Webrtc audio anti weak network technology (Part 2)
MySQL sorts the queried result set according to the specified sequence
JS (foreach) return cannot end the function solution