当前位置:   article > 正文

Qt基于Qml实现底部导航栏(TabBar)_qml tabbar 美化

qml tabbar 美化

演示效果:

 

 1.主要使用Tabbar控件实现

2.使用SVG图片进行颜色填充

3.使用QtGraphicalEffects库中的ColorOverlay修改SVG颜色

完整QML源码

BaseTabBar.qml

  1. import QtQuick 2.12
  2. import QtQuick.Controls 2.12
  3. import QtQuick.Layouts 1.12
  4. import QtGraphicalEffects 1.12
  5. TabBar {
  6. property alias myModel: idTabbarModel
  7. property int lastIndex: 0
  8. id: idTabbar
  9. currentIndex: 0
  10. ListModel {id: idTabbarModel}
  11. Repeater {
  12. id: idRepeaterControl
  13. model: idTabbarModel
  14. TabButton {
  15. property alias imageSource: image.source //图像
  16. property alias textColor: text.color //文本
  17. height: idTabbar.height
  18. contentItem:Text{
  19. id: text
  20. text: modelText
  21. font.pixelSize: 24
  22. horizontalAlignment: Text.AlignHCenter
  23. verticalAlignment: Text.AlignBottom
  24. color: (model.index === idTabbar.currentIndex) ? modelColorG : modelColor
  25. }
  26. background:Image{
  27. id: image
  28. width: 48
  29. height: 48
  30. anchors.horizontalCenter: parent.horizontalCenter
  31. source: (model.index === idTabbar.currentIndex) ? modelSrcG : modelSrc
  32. ColorOverlay{
  33. anchors.fill: image
  34. source: image
  35. color: "red"
  36. }
  37. }
  38. //鼠标经过事件处理
  39. onHoveredChanged: {
  40. if (model.index !== idTabbar.currentIndex){
  41. hovered ? text.color = modelColorG : text.color = modelColor
  42. hovered ? image.source = modelSrcG : image.source = modelSrc
  43. }
  44. }
  45. //点击事件处理
  46. onClicked: {
  47. idRepeaterControl.itemAt(idTabbar.lastIndex).imageSource = myModel.get(idTabbar.lastIndex).modelSrc;
  48. idRepeaterControl.itemAt(idTabbar.lastIndex).textColor = modelColor;
  49. image.source = modelSrcG;
  50. text.color = modelColorG;
  51. idTabbar.lastIndex = model.index;
  52. }
  53. }
  54. }
  55. }

main.qml

  1. import QtQuick 2.7
  2. import QtQuick.Controls 2.0
  3. import QtQuick.Layouts 1.0
  4. import QtQuick.Window 2.2
  5. ApplicationWindow {
  6. id: frmWindow
  7. visible: true
  8. width: 600
  9. height: 800
  10. title: qsTr("Qt基于Qml实现底部导航栏(TabBar)")
  11. footer:Rectangle{
  12. color:"blue"
  13. height:80
  14. BaseTabBar{
  15. id: bar
  16. height: 80
  17. width: parent.width
  18. anchors.bottom: parent.bottom
  19. Component.onCompleted: {
  20. myModel.append({ "modelText": "消息", "modelColor": "red", "modelColorG": "#148014",
  21. "modelSrc": "qrc:/images/Chat_MsgRecord.svg", "modelSrcG": "qrc:/images/Chat_MsgRecordG.svg"})
  22. myModel.append({ "modelText": "联系人", "modelColor": "red", "modelColorG": "#148014",
  23. "modelSrc": "qrc:/images/Chat_FriendManager.svg", "modelSrcG": "qrc:/images/Chat_FriendManagerG.svg"})
  24. myModel.append({ "modelText": "发现", "modelColor": "red", "modelColorG": "#148014",
  25. "modelSrc": "qrc:/images/Mobile_Find.svg", "modelSrcG": "qrc:/images/Mobile_FindG.svg"})
  26. myModel.append({ "modelText": "我", "modelColor": "red", "modelColorG": "#148014",
  27. "modelSrc": "qrc:/images/Main_P2PChat.svg", "modelSrcG": "qrc:/images/Main_P2PChatG.svg"})
  28. }
  29. }
  30. }
  31. SwipeView {
  32. id: view
  33. height: frmWindow.height - 120
  34. width: parent.width
  35. currentIndex: bar.currentIndex
  36. anchors.top: topbar.bottom
  37. Rectangle{
  38. color:"red"
  39. Text {
  40. text: qsTr("消息")
  41. color:"white"
  42. font.pixelSize: 30
  43. anchors.centerIn: parent
  44. }
  45. }
  46. Rectangle{
  47. color:"green"
  48. Text {
  49. text: qsTr("联系人")
  50. color:"white"
  51. font.pixelSize: 30
  52. anchors.centerIn: parent
  53. }
  54. }
  55. Rectangle{
  56. color:"blue"
  57. Text {
  58. text: qsTr("发现")
  59. color:"white"
  60. font.pixelSize: 30
  61. anchors.centerIn: parent
  62. }
  63. }
  64. Rectangle{
  65. color:"yellow"
  66. Text {
  67. text: qsTr("我")
  68. color:"red"
  69. font.pixelSize: 30
  70. anchors.centerIn: parent
  71. }
  72. }
  73. }
  74. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/806071
推荐阅读
相关标签
  

闽ICP备14008679号